如何为该程序创建用户输入退出代码?

时间:2014-09-17 22:31:13

标签: java

如何为此类程序创建用户输入退出代码?

import java.util.Scanner;

public class Testprogram{

   public static void main(String[] args){

      System.out.println("Enter any test score between 0 and 100");
      Scanner keyboard = new Scanner(System.in);
      while(true){
         double myScore=Double.parseDouble(keyboard.next());

         for(double x=0; x<=100;){

            char grade;

            if (myScore>= 90){
               grade = 'A';
            }
            else if (myScore>=80){
               grade = 'B';
            }
            else if (myScore>=70){
               grade = 'C';
            }
            else if (myScore>=60){
               grade = 'D';
            } 
            else {
               grade = 'F';
            }
            System.out.println("Grade = " + grade);
            break;
         }
      }
   }
}

2 个答案:

答案 0 :(得分:1)

要从Java程序返回退出代码,您应该使用System.exit(int)

答案 1 :(得分:-1)

您可以通过更改此内容来完成此操作:

double myScore=Double.parseDouble(keyboard.next());

对此:

String code = keyboard.next();
if(code == "exit") //if user types 'exit' then exit the while loop
   break;
double myScore = Double.parseDouble(code);