Can someone help with this and constructively criticize.. I have also included the program I wrote which is very basic but I'm trying to solidify my knowledge on if else statements and loops.
I want to write a program that:
- takes the user's input
- Both integers must be greater than 0.
- accepts two integers and then calculates the sum of the two
- Displays the sum on the screen.
- If any of these conditions are not met, an error message is displayed.
- Also the user gets 10 chances to enter the integers before the program breaks/stops.
- On the 9th try, the program lets the user know (with a different but appropriate error message) that this is the last try
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
int sumArray[] = {1,2,3,4,5,6,7,8,9,10};
for (int i=0; i<sumArray.length; i++) {
Scanner sum = new Scanner(System.in);
System.out.println("Enter more than one number to calculate the sum. Number must be > 0. ");
int a = sum.nextInt();
int b = sum.nextInt();
int combo = a+b;
System.out.println(combo);
if (combo == a || combo < a || combo == b || combo < b) {
System.out.println("Error - Try again! You must enter" +
" more than two numbers AND each number entered must be > 0");
}
else {
System.out.println("Thank you!");
break;
}
}
}
}