验证索引i

时间:2018-11-06 06:33:34

标签: java

我正在从事一项潜水编程运动,该运动存储了7名裁判的分数。我创建了一个getScore()方法,该方法从用户那里获取法官的分数。在该方法中,我需要从索引i处的法官那里得到一个分数并验证索引i。

下面是我的代码。我不确定我做对了吗。我对如何验证索引i感到困惑。

//Get the Score from the User 
public static void getScore(ArrayList<Double> score)
{
    //Scanner object to read the judges' scores
    Scanner keyboard = new Scanner(System.in);
    double next = 0; 
    System.out.println("Enter 7 scores");

    for(int i = 0; i < 7; i++)
    {
        invalid = true; 

        while (invalid)
        {
            try
            {
                System.out.println("Enter the score of judge " + (i + 1));
                double a = keyboard.nextDouble();
                score.add(a);

                if(score.contains(keyboard))
                {
                    System.out.println("Enter a valid score in the range (0-10).");
                    a = keyboard.nextDouble();
                    score.add(a); 
                    invalid = true; 
                }
                else
                {
                    invalid = false; 
                }
            }
            catch(Exception e)
            {
                System.out.println("Invalid characters found. Try again");
            }
        }
    }

    int index = highestScore(score);
    int lowIndex = lowestScore(score);
    double sum = sumScore(score, index, lowIndex);
    System.out.println("The sum of scores wihtout min and max scores : " + sum);
    System.out.println("The total diver's score is : " + ((sum * difficulty) * 0.6));
}

1 个答案:

答案 0 :(得分:0)

您需要删除其他内容,因为如果输入无效,无论如何您都要重新输入 c

if(score.contains(keyboard))
{
    System.out.println("Enter a valid score in the range (0-10).");
    a = keyboard.nextDouble();
    score.add(a); 
    invalid = true; 
}
else
{
    invalid = false; 
}

if(a<0||a>10)
{
    System.out.println("Enter a valid score in the range (0-10).");
    a = keyboard.nextDouble();
    score.add(a); 
    if(a=0 && a<=10)
        invalid = false; 
}

我假设您正在检查输入是否在0-10范围内。