如何从用户输入中找到最大数字和最小数字? (while循环)

时间:2016-02-29 23:51:51

标签: java while-loop max min

不确定从哪里开始,我能够使用用户的输入找到平均值,但似乎无法计算输入数字中的最大数字或最小数字。有可用的方法吗?任何帮助都会很棒。谢谢。

编辑:有点想通了。然而答案总是似乎是最大的数字= 2.1478 ...或最小的数字= -2.1478 ...

Scanner input = new Scanner(System.in);


System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;

double total=0;
int count = 1;
while (count < random+1) {




    System.out.println("Enter grade " + count + ":");

    double somenumber = input.nextDouble();
    total+=somenumber;

    count++;

    if (somenumber > max){
        max = somenumber;
    }
if (somenumber < min){
    min = somenumber;
}


}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Largest number is " + max);
System.out.println("Smallest number is" + min);



        }

}

4 个答案:

答案 0 :(得分:1)

您想要一些看起来像

的代码
double min = Double.MAX_VALUE;
double max = 0.00;  // assume use positive numbers

while (count < random+1) {

System.out.println("Enter grade " + count + ":");

double somenumber = input.nextDouble();
total+=somenumber;

count++;

max = Math.max (somenumber, max);
min = Math.min (somenumber, min);

}

答案 1 :(得分:0)

编辑已更新为使用Double而现在使用原始循环条件。

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double smallest = Double.POSITIVE_INFINITY; //Initialize to largest int possible
        double largest = Double.NEGATIVE_INFINITY; //Initialize to smallest int possible
        System.out.println("Enter the number of grades: ");
        double random = input.nextDouble();
        int count = 1;

        while (count < random + 1) {
            System.out.print("Input Number: ");
            Double x = input.nextDouble();
            if (x < smallest) {
                smallest = x;
            }
            if (x > largest) {
                largest = x;
            }
            count++;
        }
        System.out.println("Smallest: " + smallest + ", Largest: " + largest);
    }
}

示例运行:

  

运行:
  输入成绩数:
  5
  输入数量:2.5454
  输入数量:3.454
  输入数字:-100.54
  输入数量:5687
  输入数字:579873.5654
  最小:-100.54,最大:579873.5654
  建立成功(总时间:15秒)

答案 2 :(得分:-2)

感谢大家的提示。但是,当我运行我的程序时。我无法获得最大值和最小值的正确答案。

编辑:已解决。谢谢你们所有人的贡献。 γ-33

Scanner input = new Scanner(System.in);


System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;

double total=0;
int count = 1;
while (count < random+1) {




    System.out.println("Enter grade " + count + ":");

    double somenumber = input.nextDouble();
    total+=somenumber;

    count++;

    if (somenumber > max){
        max = somenumber;
    }
if (somenumber < min){
    min = somenumber;
}


}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Largest number is " + max);
System.out.println("Smallest number is" + min);



        }

}

最大的数字是2.14748最小的数字是-2.147478 ...那是什么打印的...我是否发生了关节错误?

答案 3 :(得分:-3)

上面的循环......

double max = 0;
double min = Double.MIN_VALUE;

设置了一些数字之后的循环...

if (someNumber > max){
  max = someNumber;
}
if (someNumber < min){
  min = someNumber;
}

Np很高兴它有帮助:)你应该考虑使用for循环而不是while循环,并在循环之外声明someNumber,如下所示:

double someNumber;
for (int i = 1; i <= random; i++){
  //...
}

为了使它更干净,你可以用for循环替换i&lt; = random

i <= readNumGrades(input);
private double  readNumGrades(Scanner in){
  System.out.println("Enter the number of grades");
  return in.nextDouble();
}

所以你不再需要变量count和random