mardi 17 mars 2015

Exiting loop with numeric input from user


Vote count:

0




may I ask what are some proposed solutions to this issue? When the user enters -1, this will be included into the total, which is not what we want. I am trying to do this without the use of an if control structure.



int numberToAdd = 0, total = 0;
Scanner input = new Scanner(System.in);

while (numberToAdd != -1) {
System.out.println("Number to add? -1 to quit");
numberToAdd = input.nextInt();
total = total + numberToAdd;
}
System.out.println("Total: = " + total);


I am aware that I can instead use strings and use an exit code that is a string, whilst parsing String inputs as an integer, however I am trying to accomplish that with an integer (in this case, -1).


Thanks



asked 1 min ago







Exiting loop with numeric input from user

Aucun commentaire:

Enregistrer un commentaire