Reputation: 11
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);
}
}
Upvotes: 0
Views: 2134
Reputation: 369
I am also not much experienced in java but would like to share input. As your question is not very clear so as per my understanding I was able to reach here:
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;
int temp = c-sum;
int result = (temp==0)?sum:(temp>0)?negate(sum,c):add(sum,c);
System.out.println("The sum of entered numbers is " + sum);
}
static int negate(int sum, int c){
System.out.println("Your answer is wrong.");
while(sum != c){
sum++;
}
return sum;
}
static int add(int sum, int c){
System.out.println("Your answer is wrong.");
while(sum != c){
sum--;
}
return sum;
}
}
If this answers please let me know.
Upvotes: 0
Reputation: 341
How about ternary operator:
int x=10;
int y=25;
int z =36;
System.out.println("Sum is:" + (((x+y)==z) ? "valid" : "invalid"));
OR
use a boolean variable as a flag. Initialize flag as true. If sum is correct (inside your first while) set flag as false. Add && flag
in your second while, and then set flag as false inside it.
Upvotes: -1
Reputation: 44210
Your code loops "infinitely" (though, technically, not really) because of this:
while(sum != c){
sum++;
System.out.println("Your answer is wrong.");
}
You keep incrementing sum
, but provided it's already over the expected value it will just keep counting up to infinity. (In practice, it will actually cause integer overflow, and start counting from negative numbers and finally reach the correct value after quite some time)
What you want to do is "fake" a correct answer so the while loop can terminate after just one iteration:
while(sum != c){
sum = c; // while loop condition will be false next time
System.out.println("Your answer is wrong.");
}
Don't worry if you don't really get the logic of this. It's a bad task and no one would usually write code like this.
Upvotes: 3
Reputation: 1695
While I personally think the assignment is quite dumb this is a code that will do what is asked of you...
// Read values for a, b and c first.
// Save a backup of c
c2 = c;
// subtract a and b from c.
c -= a;
c -= b;
// Check if c is 0
while (c == 0)
{
// In that case make sure we break the loop
c++;
// Also print something nice.
System.out.println("Correct result");
}
// Do the same again with our backup value
c2 -= a;
c2 -= b;
// Check if it is not 0 (so wrong result)
while (c2 != 0)
{
// Set it to 0 to break.
c2 = 0;
// Print something nice.
System.out.println("Incorrect result");
}
Upvotes: 0