我是Java新手,我被要求完成学校的某些任务。
最后程序应打印出以下内容(用户输入的数字):
然而,我能够提出'1号'和'2号'的解决方案。 现在我需要帮助解决'3号'问题。
以下是我写的代码:
public static void main(String[] args) {
System.out.println("Please enter integer numbers, end with a negative number: ");
Scanner numbers = new Scanner(System.in);
int positive = numbers.nextInt();
while (positive >= 0){
System.out.println( "\nEnter a number: ");
positive = numbers.nextInt();
}
System.out.println( "\nYou are done with entering number: ");
}
最后但并非最不重要的,这是我的第一天,这是我第一次在这里寻求帮助。希望我能够给我留下好印象,我迫不及待地等待回复。再次抱歉让代码处理不好。
答案 0 :(得分:1)
public static void main(String[] args){
int total = 0; // sum
int largest = Integer.MIN_VALUE; // highest
int lowest = Integer.MAX_VALUE; // lowest
int count = 0; // how many nums enters
int average; // average
int number = 0;
Scanner scanner = new Scanner(System.in);
while(true){
if (number < 0) break;
System.out.println("Enter a number:");
int number = scanner.nextInt();
// for every number input, increase count by one
count++; // freebie code - complete the reamining [your code here]'s
// for every number increase total by number
[your code here]
// if number is greater than largest, largest equals number
[your code here]
// if number is less than lowest, lowest = number
[your code here]
}
average = total / count;
System.out.println("Total input numbers: " + count);
System.out.println("Sum of input number: " + sum);
System.out.println("Highest number: " + highest);
System.out.println("Lowest numer: " + lowest);
System.out.println("Average: " + average);
}