2
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 个分数而受到限制。非常感谢任何帮助,并随时批评代码中的任何其他内容。

4

3 回答 3

4

是的你可以。

你需要的是一个嵌套循环。在伪代码中:

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); 
      }
于 2012-10-19T19:29:54.027 回答
1

简短的回答:是的,这是可能的。
选项 1:最初询问用户他们计划输入多少分数,并将其存储在 int 变量中。
例如:

  
       Ask user how many scores to enter.
       Check the response, and store it in an int variable.
       Create a double variable to add the scores (initialize it to 0.0)

       Use a for loop, asking for the score;
       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
       Once loop is exhausted, just divide the two variables (since the total 
         scores is a double, your answer will automatically be a double)
       Display the answer. 

  

选项 2:使用哨兵循环(用户必须输入一个字母——通常是“Q”或“N”——或其他东西才能退出循环)

  
       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. 

  

希望能帮助到你。

于 2012-10-19T19:51:17.120 回答
0

http://korada-sanath.blogspot.in/p/discussion-on-tech-topics.html中,有一个伪代码说明了基本 Java 编程技能的类似问题。在循环部分中,您可以简单地添加检查用户输入的分数是否在 1-100 范围内。如果没有,您可以将循环变量减少“1”,以便用户可以再次输入他的分数......

如需进一步说明,请在上述链接中的代码循环部分添加以下代码。

而不是直接将用户输入的值分配给您的 testScores 数组,您可以使用一个临时变量,然后可以分配用户输入的分数是否在范围内。

Double temp = Double.parseDouble(br.readLine());
if(temp > 1 && temp < 100) {

    testScores[loopVar] = temp;

} else {

    loopVar--;
}
于 2012-10-19T20:14:18.167 回答