My teacher wants us to make a code that will add numbers using Scanner. We should also determine if the answer is correct or wrong. But I can't use break and if-else statement since I was told not to use it. So, my problem in my code is that when I entered the numbers and put their sum(when I put a wrong answer), if I answered greater than the real sum, the sum I inputted will be subtracted to the real sum. And when I answered lesser than the real sum, the output is infinite. I already did what I can do. Thank you in advance.
package additionwhileloop;
import java.util.*;
public class AdditionWhileLoop {
public static void main(String[] args) {
Scanner program = new Scanner(System.in);
int a, b, c, sum;
System.out.println("Enter the first value:");
a = program.nextInt();
System.out.println("Enter the second value:");
b = program.nextInt();
System.out.println("The sum of two numbers is: ");
c = program.nextInt();
sum = a + b;
while(sum == c){
sum++;
System.out.println("Your answer is correct.");
}
sum = a + b;
while(sum != c){
sum++;
System.out.println("Your answer is wrong.");
}
c = a + b;
System.out.println("The sum of entered numbers is " + c);
}
}
ifseems like a pretty dumb requirement.while. However, it is possible what you want to achieve but in a very non-optimized way.