[英]How do I find the largest number and the smallest number from the user input? (while loop)
Not sure where to begin honestly, I was able to find average using the user's input but can't seem to figure out the largest number or the smallest number from the numbers entered.老实说,不确定从哪里开始,我能够使用用户的输入找到平均值,但似乎无法从输入的数字中找出最大的数字或最小的数字。 Is there a method I can use?
有我可以使用的方法吗? Any help would be great.
任何帮助都会很棒。 Thank you.
谢谢你。
EDIT: Somewhat figured it out.编辑:有点想通了。 However the answers always seem to be largest number = 2.1478... or Smallest number = -2.1478...
然而,答案似乎总是最大数 = 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);
}
} }
You want some code that looks like你想要一些看起来像的代码
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);
}
EDIT Updated to use Double
instead and now uses your original loop condition.编辑更新为使用
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);
}
}
Sample run:示例运行:
run:
跑步:
Enter the number of grades:输入等级数:
55
Input Number: 2.5454输入编号:2.5454
Input Number: 3.454输入数:3.454
Input Number: -100.54输入编号:-100.54
Input Number: 5687输入编号:5687
Input Number: 579873.5654输入号码:579873.5654
Smallest: -100.54, Largest: 579873.5654最小:-100.54,最大:579873.5654
BUILD SUCCESSFUL (total time: 15 seconds)构建成功(总时间:15 秒)
Thank you for the tips everyone.谢谢大家的提示。 However, when I ran my program.
但是,当我运行我的程序时。 I wasn't able to get the proper answers for the max and min value.
我无法得到最大值和最小值的正确答案。
EDIT: SOLVED.编辑:解决了。 THANK YOU ALL WHO CONTRIBUTED.
感谢所有贡献者。 <33
<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);
}
} }
Largest number is 2.14748 Smallest number is -2.147478...That's what was printed...Am I making a arthimetic error?最大的数字是 2.14748 最小的数字是 -2.147478...这就是打印出来的...我犯了一个算术错误吗?
Above while loop...以上 while 循环...
double max = 0;
double min = Double.MIN_VALUE;
In loop after you set some number...在设置一些数字后循环......
if (someNumber > max){
max = someNumber;
}
if (someNumber < min){
min = someNumber;
}
Np glad it helped :) you should consider using a for loop instead of a while loop and declare someNumber outside of the loop like so: Np 很高兴它有帮助:) 你应该考虑使用 for 循环而不是 while 循环并在循环外声明 someNumber ,如下所示:
double someNumber;
for (int i = 1; i <= random; i++){
//...
}
To make it even cleaner you could replace i <= random in the for loop with为了使它更干净,您可以将 for 循环中的 i <= random 替换为
i <= readNumGrades(input);
private double readNumGrades(Scanner in){
System.out.println("Enter the number of grades");
return in.nextDouble();
}
So you no longer need the variables count and random所以你不再需要变量 count 和 random
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.