gpt4 book ai didi

java - 努力从我的计数中排除某些数字?

转载 作者:行者123 更新时间:2023-11-30 10:36:44 25 4
gpt4 key购买 nike

我的程序的目的是计算用户输入的测试分数的平均值、最大值和最小值。

这是我一直遇到的问题:我的程序被构建为拒绝输入低于零或高于 100 的测试分数,它确实如此,但例如,如果用户输入 108,它会说“错误:不是有效的考试分数。”但即使分数被拒绝,它仍会将该分数添加到计数中,并且 108 将是最大值。如果输入了无效分数,我需要将其完全排除在程序之外。任何人都可以解释如何解决这个问题吗?

这是我的代码。

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class hw {

public static void main(String[] args) {

int maxGrade = Integer.MIN_VALUE;
int minGrade = Integer.MAX_VALUE;
int count = 0;
int total = 0;
final int SENTINEL = -1;
int score;

Scanner scan = new Scanner(System.in);

System.out.println("To calculate the class average, enter each test
score.");
System.out.println("When you are finished, enter a -1.");

System.out.print("Enter the first test score > ");
score = scan.nextInt();


while (score != SENTINEL)
{
total += score;
//add one to the count
count++;
//calculate the maximum and the minimum
if (score > maxGrade) {
maxGrade = score;
}
if (score < minGrade) {
minGrade = score;
}

if (score < 0 || score> 100)
System.out.print("ERROR: Not a valid test score. ");

System.out.println(" Enter the next test score > ");
score = scan.nextInt();

}
if (count != 0) {
DecimalFormat oneDecimalPlace = new DecimalFormat("0.0");

System.out.println("\nThe class average is "
+ oneDecimalPlace.format((double) (total) / count));
System.out.println("The minimum value is " + minGrade);
System.out.println("The maximum value is " + maxGrade);
} else {
System.out.println("\nNo grades were entered");
}

}
}

最佳答案

只有当 0 < input < 100 时,你才必须添加到总数,计算最大值和最小值

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class hw {

public static void main(String[] args) {

int maxGrade = Integer.MIN_VALUE;
int minGrade = Integer.MAX_VALUE;
int count = 0;
int total = 0;
final int SENTINEL = -1;
int score;

Scanner scan = new Scanner(System.in);

System.out.println("To calculate the class average, enter each test score.");
System.out.println("When you are finished, enter a -1.");

System.out.print("Enter the first test score > ");
score = scan.nextInt();


while (score != SENTINEL)
{

if (score < 0 || score> 100)
{
System.out.print("ERROR: Not a valid test score. ");
}
else
{
//add one to the count
count++;
//calculate the maximum and the minimum
if (score > maxGrade) {
maxGrade = score;
}
if (score < minGrade) {
minGrade = score;
}
total += score;
}
System.out.println(" Enter the next test score > ");
score = scan.nextInt();

}
if (count != 0) {
DecimalFormat oneDecimalPlace = new DecimalFormat("0.0");

System.out.println("\nThe class average is "
+ oneDecimalPlace.format((double) (total) / count));
System.out.println("The minimum value is " + minGrade);
System.out.println("The maximum value is " + maxGrade);
} else {
System.out.println("\nNo grades were entered");
}

}
}

关于java - 努力从我的计数中排除某些数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40457346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com