0

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);
    } 

}
3
  • 1
    "Welcome to CS101: Here you'll learn how to write completely non-idiomatic gibberish." This is a terrible task and your teacher shouldn't have given it to you. Commented Feb 22, 2018 at 12:31
  • I'm not sure you understood the assignment correctly, because "We should also determine if the answer is correct or wrong" without if seems like a pretty dumb requirement. Commented Feb 22, 2018 at 12:31
  • Why your teacher insisted on using only while. However, it is possible what you want to achieve but in a very non-optimized way. Commented Feb 22, 2018 at 12:33

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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");
}

Comments

0

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.

3 Comments

Thank you for answering my question and also for the code but, I don't know how to make a flowchart of this and how to defend the code since I am only a Grade 9 student..=)
Let me make one for you. I ll paste here as image
Thank you, but you don't really need to do it. I should be the one to do it.
-1

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.

1 Comment

This follows the letter of the law, rather than the spirit of the law. I doubt they'd be impressed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.