[英]Trying to find the minimum number within Scanner user input in Java
I'm coding a test averager in Java.我在 Java 中编写了一个测试平均器。 I have to use do while, while, if loops.
我必须使用 do while、while、if 循环。
I have the user inputting test scores, asking for a negative number to leave the while loop.我让用户输入测试分数,要求输入负数以离开 while 循环。
I have to display the number of scores entered, the highest, the lowest, and the average.我必须显示输入的分数、最高分数、最低分数和平均分数。
However, I am having problems displaying the lowest number as it as popping up as 0.但是,我在显示最小数字时遇到问题,因为它弹出为 0。
For input of 95, 93, 92, 91, and -1:对于 95、93、92、91 和 -1 的输入:
The number of scores entered: 4 The Highest: 95 The Lowest: 0 The average is: 92输入分数:4 最高:95 最低:0 平均为:92
My code:我的代码:
import java.util.*;
public class Lab7
{
public static void main(String[] args)
{
System.out.println("This program computes the average of");
System.out.println("a list of (nonnegative) exam scores.");
int sum;
int numberOfTests;
int testInput;
String answer;
int minimum = 0;
int maximum = 0;
Scanner keyboard = new Scanner (System.in);
do
{
System.out.println();
System.out.println("Enter all the scores to be averaged.");
System.out.println("Enter a negative number after");
System.out.println("you have entered all the scores.");
sum = 0;
numberOfTests = 0;
testInput = keyboard.nextInt();
while (testInput >= 0)
{
sum = sum + testInput;
numberOfTests++;
if (testInput < minimum)
{
minimum = testInput;
}
if (testInput > maximum)
{
maximum = testInput;
}
testInput = keyboard.nextInt();
}
if (numberOfTests > 0)
{
System.out.println("The number of scores entered: " + numberOfTests);
System.out.println("The Highest: " + maximum);
System.out.println("The Lowest: " + minimum);
System.out.println("The average is: " +
(sum / numberOfTests));
}
else
{
System.out.println("No scores to average.");
}
System.out.println("Want to average another exam?");
System.out.println("Enter yes or no.");
answer = keyboard.next();
}
while (answer.equalsIgnoreCase("yes"));
}
}
You initialise minimum
with 0
, so for positive inputs it will always be the smallest value.您使用
0
初始化minimum
,因此对于正输入,它始终是最小值。 That means if (testInput < minimum)
will always be false
.这意味着
if (testInput < minimum)
将始终为false
。 To fix this, initialise it with Integer.MAX_VALUE
instead.要解决此问题,请改为使用
Integer.MAX_VALUE
对其进行初始化。
In general, it is a good idea to start a maximum value with the lowest possible value , like Integer.MIN_VALUE
or Double.MIN_VALUE
or in your case with 0
, since there are no negative scores.通常,最好从可能的最小值开始最大值,例如
Integer.MIN_VALUE
或Double.MIN_VALUE
或在您的情况下使用0
,因为没有负分数。 Furthermore, the minimum should be initialised with the largest possible value , like described above.此外,最小值应该用最大的可能值初始化,如上所述。
That way, your checks will always work as intended.这样,您的支票将始终按预期工作。
This is due to this line int minimum = 0;
这是由于这一行
int minimum = 0;
as your input scores are greater than 0 thus your this condition will never be satisfied if (testInput < minimum)
and the value of minimum
won't never-ever changes and will remain as 0 .由于您的输入分数大于0 ,因此
if (testInput < minimum)
并且minimum
的值永远不会改变并且将保持为0 ,则永远不会满足您的此条件。 Hence define it as minimum = Integer.MAX_VALUE
因此将其定义为
minimum = Integer.MAX_VALUE
Following is your code in a working state and I also made some optimizations which will help you in a better way of writing code:以下是您在工作 state 中的代码,我还进行了一些优化,这将帮助您更好地编写代码:
public class Solution{
public static void main(String[] args)
{
int sum = 0, numberOfTests = 0, testInput , minimum = Integer.MAX_VALUE , maximum = Integer.MIN_VALUE;
System.out.println("This program computes the average of \na list of (nonnegative) exam scores. \n");
Scanner keyboard = new Scanner (System.in);
do
{
System.out.println();
System.out.println("Enter all the scores to be averaged.");
System.out.println("Enter a negative number after \nYou have entered all the scores.");
testInput = keyboard.nextInt();
while (testInput >= 0){
sum += testInput;
numberOfTests++;
if (testInput < minimum) minimum = testInput;
if (testInput > maximum) maximum = testInput;
testInput = keyboard.nextInt();
}
if (numberOfTests > 0)
{
System.out.println("The number of scores entered: " + numberOfTests);
System.out.println("The Highest: " + maximum);
System.out.println("The Lowest: " + minimum);
System.out.println("The average is: " +
((float)sum / numberOfTests));
}
else
System.out.println("No scores to average.");
System.out.println("Want to average another exam?");
System.out.println("Enter yes or no.");
}
while (keyboard.nextLine().trim().equalsIgnoreCase("yes"));
keyboard.close();
}
}
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.