public static void main (String [] args)
{
// declare variables, capture input
String input, name = JOptionPane.showInputDialog("Please " +
"enter your first and last name.");
double testScore1, testScore2, testScore3, average;
// capture input, cast, and validate input
input = JOptionPane.showInputDialog("What is the score " +
"of your first test?");
testScore1 = Double.parseDouble(input);
while (testScore1 < 1 || testScore1 > 100)
{
input = JOptionPane.showInputDialog("This test score is not " +
"between 1 and 100. \nPlease enter a test score in " +
"this range:");
testScore1 = Double.parseDouble(input);
}
input = JOptionPane.showInputDialog("What is the score " +
"of your second test?");
testScore2 = Double.parseDouble(input);
while (testScore2 < 1 || testScore2 > 100)
{
input = JOptionPane.showInputDialog("This test score is not " +
"between 1 and 100. \nPlease enter a test score in " +
"this range:");
testScore2 = Double.parseDouble(input);
}
input = JOptionPane.showInputDialog("What is the score " +
"of your third test?");
testScore3 = Double.parseDouble(input);
while (testScore3 < 1 || testScore3 > 100)
{
input = JOptionPane.showInputDialog("This test score is not " +
"between 1 and 100. \nPlease enter a test score in " +
"this range:");
testScore3 = Double.parseDouble(input);
}
// calculate average and display output
average = (testScore1 + testScore2 + testScore3)/3;
JOptionPane.showMessageDialog(null, name + ", your average score is: " + average);
}
首先,我是一个初学者程序员。我的术语和行话相当缺乏,所以请耐心等待。
我正在编写一个程序来捕获 3 个测试分数,然后使用 while 循环验证它们(必须在 1-100 范围内)。然后平均测试分数,输出显示平均值。很简单的东西。
如果可能的话,我想找到一种方法来捕获测试分数的数量,然后从那里捕获每个实际分数。例如,程序询问“平均计算了多少个测试?”,然后取该数字并使其与程序提示“请输入测试分数(1):”或类似内容的次数相同. 因此,为了更清楚起见,如果用户输入 4 作为测试次数,那么输入分数的提示将显示 4 次。
我觉得上面的代码是多余的,因为每个分数都使用了一个 while 循环,并且因为该程序仅适用于 3 个分数而受到限制。非常感谢任何帮助,并随时批评代码中的任何其他内容。