vendredi 24 avril 2020

Java Sum of numbers until string is entered

i've just started java programming and was wondering on how to approach or solve this problem i'm faced with.

I have to write a program that asks a user for a number and continually sums the numbers inputted and print the result. This program stops when the user enters "END"

I just can't seem to think of a solution to this problem, any help or guidance throughout this problem would be much appreciated and would really help me understand problems like this. This is the best i could do

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

while (true) {
    System.out.print("Enter a number: ");
    int x = scan.nextInt();
    System.out.print("Enter a number: ");
    int y = scan.nextInt();

    int sum = x + y;


    System.out.println("Sum is now: " + sum);   

}   


}
    }   

The output is supposed to look like this:

Enter a number: 5

Sum is now: 5

Enter a number: 10

Sum is now: 15

Enter a number: END

Aucun commentaire:

Enregistrer un commentaire