samedi 28 février 2015

Using an if statement with a while loop?



I'm trying to do this question:


Design a program that reads in a list of numbers terminated by -9999 and prints out the sum entered.


I am amending the program so that only values between 1 and 100 are summed, and as you can see I've tried to implement an if statement. But, nothing has changed and this part has stumped me. I've been stuck on it for ages, code follows:



import java.util.Scanner;

public class Summing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Number, Sum = 0;

System.out.println("Enter a list of whole numbers, finished by -9999");
Number = sc.nextInt();

if (Number > 0 || Number < 100);
while (Number != -9999) {
Sum = Sum + Number;
Number = sc.nextInt();
}

System.out.println("Sum is " + Sum);
}
}



Aucun commentaire:

Enregistrer un commentaire