有程序循环连续

时间:2016-05-22 22:04:43

标签: java loops

我正在为教师编写一个程序来输入成绩,平均成绩,然后给出该特定学生的所有平均成绩总和。我几乎将它编程为完整的。我遇到的唯一问题是弄清楚如何让整个程序循环,直到用户按下'enter'退出。我已经在下面包含了我的代码(并使用我下面的新代码更新了代码):

import java.util.Scanner;

public class StudentGrades {
  public static void main(String[] args){

    //Declare Scanner and instantiate keyboard
    Scanner keyboard;
    keyboard = new Scanner(System.in);

    //String to hold student name
    String StudentName;

    //Variables to hold number of grades entered
    int countOfGrades=0;
    int countOfGrades2=0;

    //Variables to hold individual grade inputted
    float inputtedGrade=0;
    float inputtedTestGrade=0;
    float inputtedExamGrade=0;

    //Variables to hold the total sum of grades entered
    float totalSumOfGrades=0;
    float totalSumOfGrades2=0;

    //Prompt user for input
    System.out.print("Input student name: ");
    StudentName = keyboard.nextLine();

    //Prompt the user for grade
    System.out.print("Input student's homework/quiz grade: ");

    //Get grade from user
    inputtedGrade = keyboard.nextFloat();

    //Loop while grade is greater than zero
    while(inputtedGrade > 0){

        //Add grade to sum total
        totalSumOfGrades=totalSumOfGrades+inputtedGrade;

        //Increment counter of grades
        countOfGrades++;

        //Prompt user for grade
        System.out.print("Input student's homework/quiz grade: ");

        //Get grade from user
        inputtedGrade=keyboard.nextFloat();

    }//Ending bracket of while loop

    //Calculate and output average of grades
    System.out.print("Average homework/quiz grade is: ");
    System.out.println((totalSumOfGrades/countOfGrades));

    //Prompt user for test score
    System.out.print("Input student's test grade: ");
    inputtedTestGrade = keyboard.nextFloat();

    //Loop while grade is greater than zero
    while(inputtedTestGrade > 0) {

        //Add grade to sum total
        totalSumOfGrades2=totalSumOfGrades2+inputtedTestGrade;

        //Increment counter of grades
        countOfGrades2++;

        //Prompt user for grade
        System.out.print("Input student's test grade: ");

        //Get grade from user
        inputtedTestGrade=keyboard.nextFloat();     
    }//Ending bracket of while loop

    //Calculate and output average of grades
    System.out.print("Average test grade is: ");
    System.out.println((totalSumOfGrades2/countOfGrades2));

    //Prompt user for exam score
    System.out.print("Enter student's exam grade: ");
    inputtedExamGrade=keyboard.nextFloat();

    //Double to hold average values then convert to int
    double avgHomework = (totalSumOfGrades/countOfGrades)*.2;
    double avgTest = (totalSumOfGrades2/countOfGrades2)*.5;
    double avgExam = (inputtedExamGrade)*.3;
    float avgHW = (float) avgHomework;
    float avgT = (float) avgTest;
    float avgEx = (float) avgExam;

    //Output student grade
    System.out.println("The grade for this student is: " +(avgHW+avgT+avgEx));

    //Close keyboard
    keyboard.close();

  }//Ending bracket of method main

}//Ending bracket of class StudentGrades


import java.util.Scanner;

public class StudentGrades {
    public static void main(String[] args){
    while (true){ 

    //Declare Scanner and instantiate keyboard
    Scanner keyboard;
    keyboard = new Scanner(System.in);

    //Strings to hold values
    String StudentName;
    String cont;

    //Variables to hold number of grades entered
    int countOfGrades=0;
    int countOfGrades2=0;

    //Variables to hold individual grade inputted
    float inputtedGrade=0;
    float inputtedTestGrade=0;
    float inputtedExamGrade=0;

    //Variables to hold the total sum of grades entered
    float totalSumOfGrades=0;
    float totalSumOfGrades2=0;

    //Prompt user for input
    System.out.print("Input student name: ");
    StudentName = keyboard.nextLine();

    //Prompt the user for grade
    System.out.print("Input student's homework/quiz grade: ");

    //Get grade from user
    inputtedGrade = keyboard.nextFloat();

    //Loop while grade is greater than zero
        while(inputtedGrade > 0){

        //Add grade to sum total
        totalSumOfGrades=totalSumOfGrades+inputtedGrade;

        //Increment counter of grades
        countOfGrades++;

        //Prompt user for grade
        System.out.print("Input student's homework/quiz grade: ");

        //Get grade from user
        inputtedGrade=keyboard.nextFloat();

    }//Ending bracket of while loop

    //Calculate and output average of grades
    System.out.print("Average homework/quiz grade is: ");
    System.out.println((totalSumOfGrades/countOfGrades));

    //Prompt user for test score
    System.out.print("Input student's test grade: ");
    inputtedTestGrade = keyboard.nextFloat();

    //Loop while grade is greater than zero
    while(inputtedTestGrade > 0) {

            //Add grade to sum total
            totalSumOfGrades2=totalSumOfGrades2+inputtedTestGrade;

            //Increment counter of grades
            countOfGrades2++;

            //Prompt user for grade
            System.out.print("Input student's test grade: ");

            //Get grade from user
            inputtedTestGrade=keyboard.nextFloat();     
    }//Ending bracket of while loop

    //Calculate and output average of grades
    System.out.print("Average test grade is: ");
    System.out.println((totalSumOfGrades2/countOfGrades2));

    //Prompt user for exam score
    System.out.print("Enter student's exam grade: ");
    inputtedExamGrade=keyboard.nextFloat();

    //Double to hold average values then convert to int
    double avgHomework = (totalSumOfGrades/countOfGrades)*.2;
    double avgTest = (totalSumOfGrades2/countOfGrades2)*.5;
    double avgExam = (inputtedExamGrade)*.3;
    float avgHW = (float) avgHomework;
    float avgT = (float) avgTest;
    float avgEx = (float) avgExam;

    //Output student grade
    System.out.println("The grade for this student is: " +(avgHW+avgT+avgEx));

    //Prompt user for continuity input
    System.out.println("Would you like to input another student's grades? (y/n)");
      cont = keyboard.next();
      if (cont.equalsIgnoreCase("n")) {
          break;
      }
    //Close keyboard
    keyboard.close();

    }//Ending bracket of very first while loop

}//Ending bracket of method main

}//Ending bracket of class StudentGrades

1 个答案:

答案 0 :(得分:3)

你可以这样做:

while(true){

  //put code in here

  System.out.println("Continue?(y/n)");
  String cont = keyboard.nextLine();
  if(cont.equalsIgnoreCase("n")){
    break;
  }
}