在Java中的while循环之前添加程序出口

时间:2018-10-07 16:19:29

标签: java

如果输入-1以终止循环,则无法让程序停止并在下面显示消息。如果输入了不同的整数,该程序也将不会进入while循环。我似乎将大括号放到了某个地方,因为最后两个错误。

public static void main(String[] args)
{
    double score;
    double total = 0.0;
    double average;
    int scoreCount = 0;

    // create the Scanner object. Name it stdin
    Scanner stdin = new Scanner(System.in);

    // title at the top of the output
    System.out.println (" score report");;

    do{
    //   read the first score
        System.out.printf ("Enter a score  (1-100, -1 to quit)"
            + ": ", scoreCount);
        score = stdin.nextDouble();

        if (score == -1)
        {
            System.out.println ("Nothing entered.");
        }           
    while((score = stdin.nextDouble()) != -1.0)
    {              
        if (score<-1 || score>100)
        {
        System.out.println ("Illegal score.  Try again");
        System.out.printf ("Enter a score  (1-100, -1 to quit)"
              + ": ", scoreCount);
        }
        else
        {
            System.out.printf ("Enter a score  (1-100, -1 to quit)"
              + ": ", scoreCount); 
            scoreCount++;
            total += score;
        }        
           // end of for loop
    average = total / scoreCount;       //equation
    System.out.printf ("\nThe average score for %d students is %8.2f\n",
                          scoreCount, average); 
    }
} // end of main    

} //类定义的结尾

3 个答案:

答案 0 :(得分:0)

根据我的理解,该问题有一个简单的解决方案。要退出循环,只需使用import java.util.*; import java.io.*; /** * Counts composition of children's sexes among nuclear families. * * @author Thomas Morey * @version 10/7/18 */ public class Family { public static void main() throws IOException{ File read = new File("C:/Users/tdmor/Desktop/Misc/School/AP Comp Science/Recources/maleFemaleInFamily.txt"); //entire file path necisary? Scanner inf = new Scanner(read); //stands for in file String family; int quan; int i = 1; while(inf.hasNext()){ family = inf.next(); String famNum/*increment here*/ = family; //create a variable with the int "i" appended to the end of the variable name to represent how many families there are. quan = i; System.out.println(family); i++; } } }

break

此外,根据代码,实际上并不需要“ do”语句。只需使用常规的while循环即可。

答案 1 :(得分:0)

在do循环的末尾需要一个“ while”条件。这就是为什么将其称为“ do-while”循环的原因。没有while条件,您应该得到一个编译时错误。这是一个示例:

double score;
double total = 0.0;
double average;
int scoreCount = 0;

// create the Scanner object. Name it stdin
Scanner stdin = new Scanner(System.in);

// title at the top of the output
System.out.println ("Score Report");

 do{
  //   read the first score
      System.out.println("Enter a score (0-100 or -1 to quit)" 
            + ": " + scoreCount);
      score = stdin.nextDouble();//Retrieve the score.

      if(score == -1) {
        System.out.println("Bye!");
       }

      if (score<-1 || score>100)//Here is the if statement that makes the user enter another score if it is illegal
      {
        System.out.println("Illegal score.  Try again");
        continue; //A continue statement will start the loop over again from the top!
      }
      else if(score >= 0 && score <= 100 && score != -1)
      {

            scoreCount++;
            total += score;
       }        
      // end of for loop
      average = total / scoreCount;       //equation
      System.out.println();
      System.out.printf ("\nThe average score for %d students is %8.2f\n",
                          scoreCount, average); 
      System.out.println();
}while(score != -1); //Runs "while" the score != -1

以下是该程序可能的输出示例:

Score Report
Enter a score (0-100 or -1 to quit): 0
50.0


The average score for 1 students is    50.00

Enter a score (0-100 or -1 to quit): 1
50.0


The average score for 2 students is    50.00

Enter a score (0-100 or -1 to quit): 2
-90
Illegal score.  Try again
Enter a score (0-100 or -1 to quit): 2
50.0


The average score for 3 students is    50.00

Enter a score (0-100 or -1 to quit): 3
-1
Bye!


The average score for 3 students is    50.00

如您在此处看到的,如果语句更改为:

if (score<-1 || score>100)//Here is the if statement that makes the user enter another score if it is illegal
        {
        System.out.println("Illegal score.  Try again");
        continue;
        }

continue语句将强制循环从头开始,而无需在循环中执行其余代码。这将帮助您避免输入无效。 在do循环的最后,您还需要while条件:

 }while(score != -1);

现在,仅当分数不等于负1时,循环才会继续。如果分数等于-1,您还可以告知用户他们正在您的代码中退出程序:

if(score == -1) {
            System.out.println("Bye!");
        }

如果输入的数字在0到100之间,否则您可以将else语句更改为else if语句以执行,否则-1项将被计为分数:

else if(score >= 0 && score <= 100 && score != -1)
        {

            scoreCount++;
            total += score;
        }        
           // end of for loop

您也不会退出循环,就像您说自己在上面一样。每次迭代循环时,都会计算平均值。将显示平均值的代码放在循环之外:

}while(score != -1);
    average = total / scoreCount;       //equation
    System.out.println();
    System.out.printf ("\nThe average score for %d students is %8.2f\n",
                      scoreCount, average);

现在输出将如下所示:

Score Report
Enter a score (0-100 or -1 to quit): 0
50.0
Enter a score (0-100 or -1 to quit): 1
70.0
Enter a score (0-100 or -1 to quit): 2
90.0
Enter a score (0-100 or -1 to quit): 3
-1
Bye!


The average score for 3 students is    70.00

如果只希望在输入-1作为第一个选项时显示一条消息,请执行以下操作:

if(score == -1 && scoreCount == 0) {
            System.out.println("Bye!");
        }

答案 2 :(得分:0)

发布的代码将无法编译。这是一个正确的函数,可以输入一堆从0到100的数字并计算平均值。输入-1时,程序将退出。我已经在代码中添加了注释。

public static void main(String[] args)
{
    double score;
    double total = 0.0;
    double average;
    int scoreCount = 0;

    // create the Scanner object. Name it stdin
    Scanner stdin = new Scanner(System.in);

    // title at the top of the output
    System.out.println (" score report");

    //Request the first score
    System.out.printf ("Enter a score %d  (1-100, -1 to quit)"
            + ": ", scoreCount+1);
    //read the first score
    score = stdin.nextDouble();
    do{
        //check the entered score and decide what to do. 
        if (score == -1)
        {
            System.out.println ("Nothing entered.");
            break;
        } else if (score<-1 || score>100)
        {
            System.out.println ("Illegal score.  Try again");
            System.out.printf ("Enter a score %d  (1-100, -1 to quit)"
                    + ": ", scoreCount+1);
        }
        else
        {
            scoreCount++;
            total += score;
            // The entered score is good ask for the next one
            System.out.printf ("Enter a score %d  (1-100, -1 to quit)"
                    + ": ", scoreCount+1);
        }        
    }while((score = stdin.nextDouble()) != -1.0); // Here we read the input. 
    // end of the program.
    average = total / scoreCount;       //equation
    System.out.printf ("\nThe average score for %d students is %8.2f\n",
            scoreCount, average); 
} // end of main