我一直在研究这个程序,它应该取一个用户输入的整数列表,程序会说明输入数字的最大值,最小值和平均值。平均部分工作正常,但由于某种原因,当程序达到最大和最小部分时它只是跳过它们。
/*
Program that takes a list of numbers entered by the user and
states the largest, smallest, and average of these numbers.
*/
package main1;
import java.util.Scanner;
/**
*
* @author M.Jalil
*/
public class Main1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int next; // next number entered by user
double min = 100; // initialized min
double max = 0; // initilized max
double sum; //sum of numbers entered by user
int numbers; // amount of numbers entered by user
double average; //calculated average of numbers entered by users
System.out.println("Enter a list of positive numbers.");
System.out.println("Enter a negative number when you're done.");
Scanner kb = new Scanner(System.in);
next = kb.nextInt();
numbers = 0;
sum = 0;
//finds the sum of all numbers and amount of numbers entered
while (next >= 0) {
sum = sum + next;
numbers++;
next = kb.nextInt();
int[] list = new int[numbers]; // an array list of the numbers entered
list[numbers] = next;
// checks if number entered is bigger than the max number and assigns the larger number to max
if (list[numbers] > max) {
max = list[numbers];
System.out.println("Max number: " + max); // displays maximum number ||**ignored??**||
}
//checks if number entered is less than the min number and assigns the smaller number to min
if (list[numbers] < min) {
min = list[numbers];
System.out.println("Min number: " + min); //displays minimum number ||**ignored??**||
}
}
average = sum / numbers; // calculates average
System.out.println("The average is: " + average);
}
}
我已经标记了被忽略的部分打印行,并且没有按照预期显示在屏幕上。任何人都可以看看这个并帮助我吗?我觉得问题可能与我的参数有关,但我不确定是怎么回事。提前感谢任何信息或建议。
答案 0 :(得分:0)
程序的某些部分未被“忽略” - if语句中的条件必须在运行时等于false