在Java中,是否可以使用某种while循环(或任何东西)来确定输入提示的数量? publicstaticvoidmain(字符串[]args) { //声明变量,捕获输入 字符串输入,name=JOptionPane.showInputDialog(“请”+ “输入您的名字和姓氏。”); 双测试分数1,测试分数2,测试分数3,平均; //捕获输入、强制转换和验证输入 input=JOptionPane.showInputDialog(“分数是多少”+ “你第一次考试的成绩如何?”); testScore1=Double.parseDouble(输入); 而(testScore1100) { input=JOptionPane.showInputDialog(“此测试分数不是”+ “介于1和100之间。\n请在中输入测试分数”+ “此范围:”; testScore1=Double.parseDouble(输入); } input=JOptionPane.showInputDialog(“分数是多少”+ “你的第二次测试结果如何?”; testScore2=Double.parseDouble(输入); 而(testScore2100) { input=JOptionPane.showInputDialog(“此测试分数不是”+ “介于1和100之间。\n请在中输入测试分数”+ “此范围:”; testScore2=Double.parseDouble(输入); } input=JOptionPane.showInputDialog(“分数是多少”+ “你的第三次测试结果如何?”; testScore3=Double.parseDouble(输入); 而(testScore3100) { input=JOptionPane.showInputDialog(“此测试分数不是”+ “介于1和100之间。\n请在中输入测试分数”+ “此范围:”; testScore3=Double.parseDouble(输入); } //计算平均值并显示输出 平均值=(testScore1+testScore2+testScore3)/3; showMessageDialog(null,name+),您的平均分数为:“+average”; }
首先,我是一个初学者程序员。我的术语和行话都很缺乏,请耐心听我说 我正在编写一个程序来捕获3个测试分数,然后使用while循环验证它们(必须在1-100范围内)。然后平均测试分数,输出显示平均值。非常简单的东西 我想找到一种方法,如果可能的话,获取考试分数的数量,然后从那里获取每个实际分数。例如,程序询问“平均计算了多少个测试?”,然后取该数字,并使其与程序提示“请输入测试分数(1):”或类似提示的次数相同。因此,为了进一步明确起见,如果用户键入4作为测试次数,那么输入分数的提示将显示4次 我觉得上面的代码是多余的,因为对每个分数使用while循环,并且受到限制,因为该程序只适用于3个分数。非常感谢您的帮助,您可以随意评论代码中的任何其他内容。是的,您可以 您需要的是一个嵌套循环。在伪代码中:在Java中,是否可以使用某种while循环(或任何东西)来确定输入提示的数量? publicstaticvoidmain(字符串[]args) { //声明变量,捕获输入 字符串输入,name=JOptionPane.showInputDialog(“请”+ “输入您的名字和姓氏。”); 双测试分数1,测试分数2,测试分数3,平均; //捕获输入、强制转换和验证输入 input=JOptionPane.showInputDialog(“分数是多少”+ “你第一次考试的成绩如何?”); testScore1=Double.parseDouble(输入); 而(testScore1100) { input=JOptionPane.showInputDialog(“此测试分数不是”+ “介于1和100之间。\n请在中输入测试分数”+ “此范围:”; testScore1=Double.parseDouble(输入); } input=JOptionPane.showInputDialog(“分数是多少”+ “你的第二次测试结果如何?”; testScore2=Double.parseDouble(输入); 而(testScore2100) { input=JOptionPane.showInputDialog(“此测试分数不是”+ “介于1和100之间。\n请在中输入测试分数”+ “此范围:”; testScore2=Double.parseDouble(输入); } input=JOptionPane.showInputDialog(“分数是多少”+ “你的第三次测试结果如何?”; testScore3=Double.parseDouble(输入); 而(testScore3100) { input=JOptionPane.showInputDialog(“此测试分数不是”+ “介于1和100之间。\n请在中输入测试分数”+ “此范围:”; testScore3=Double.parseDouble(输入); } //计算平均值并显示输出 平均值=(testScore1+testScore2+testScore3)/3; showMessageDialog(null,name+),您的平均分数为:“+average”; },java,loops,input,while-loop,Java,Loops,Input,While Loop,首先,我是一个初学者程序员。我的术语和行话都很缺乏,请耐心听我说 我正在编写一个程序来捕获3个测试分数,然后使用while循环验证它们(必须在1-100范围内)。然后平均测试分数,输出显示平均值。非常简单的东西 我想找到一种方法,如果可能的话,获取考试分数的数量,然后从那里获取每个实际分数。例如,程序询问“平均计算了多少个测试?”,然后取该数字,并使其与程序提示“请输入测试分数(1):”或类似提示的次数相同。因此,为了进一步明确起见,如果用户键入4作为测试次数,那么输入分数的提示将显示4次 我觉
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);
}
while(条件)
{
int numberOfInput=getInput();//从用户处获取输入
for(int i=0;i100)
{
input=JOptionPane.showInputDialog(“此测试分数不是”+
“介于1和100之间。\n请在中输入测试分数”+
“此范围:”;
testScore1=Double.parseDouble(输入);
}
简短回答:是的,这是可能的选项1:首先询问用户计划输入多少分数,并将其存储在int变量中
例如:
while(condition)
{
int numberOfInput = getInput() ; //get the input from the user
for(int i =0 ; i < numberOfInput; i++) //iterate for the amount of prompts required
prompt() ; //get input
}
function prompt
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);
}
选项2:使用sentinel循环(用户必须输入一个字母-通常是“Q”或“N”-或其他东西才能退出循环)
希望有帮助。在中,有一个伪代码用基本Java编程技巧说明了类似的问题。在循环部分中,您可以简单地添加一个检查,检查用户输入的分数是否在1-100范围内。如果不是,您可以将循环变量减少“1”,以便用户可以再次输入他的分数
如需进一步说明,请在上述链接中代码的循环部分添加以下代码
您可以使用一个临时变量,而不是直接将用户输入的值分配给testScores数组,然后可以分配用户输入的范围内的分数
Create an int variable to store total loops (initialize to 0).
Create a double variable to add the scores (initialize it to 0.0)
Use a for loop, asking for the score;
Check if the value is the quit character
If it is not
Evaluate the score to ensure it's a valid number
If it's not a valid number, prompt the user again (this is still within
the same iteration, not a different iteration)
If it's a valid number, add it to the total scores variable and increment
the total loops variable by 1.
If it is
just divide the two variables (since the total
scores is a double, your answer will automatically be a double)
Display the answer.
Double-temp=Double.parseDouble(br.readLine());
如果(温度>1和温度<100){
testScores[loopVar]=温度;
}否则{
loopVar--;
}
Create an int variable to store total loops (initialize to 0).
Create a double variable to add the scores (initialize it to 0.0)
Use a for loop, asking for the score;
Check if the value is the quit character
If it is not
Evaluate the score to ensure it's a valid number
If it's not a valid number, prompt the user again (this is still within
the same iteration, not a different iteration)
If it's a valid number, add it to the total scores variable and increment
the total loops variable by 1.
If it is
just divide the two variables (since the total
scores is a double, your answer will automatically be a double)
Display the answer.
Double temp = Double.parseDouble(br.readLine());
if(temp > 1 && temp < 100) {
testScores[loopVar] = temp;
} else {
loopVar--;
}