程序打印错误到控制台

时间:2015-01-31 16:01:32

标签: java try-catch

我创建了一个程序,要求用户输入(等级)。我正在使用try / catch语句来捕获InputMismatchException,以防用户输入错误的数据类型。在第二次try / catch语句期间出现此问题。程序询问“输入您的百分比标记?”后在if语句中,用户输入的数据类型不正确。该程序然后重新打印您的成绩两次,你想要两次输入你的成绩。

import java.util.InputMismatchException;
import java.util.Scanner;


public class CatchingException {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int score;
    String choice;

    try {
    System.out.println("Enter your percentage mark: ");
    score = scan.nextInt();


    do {
        if(score <40) {
            System.out.println("You FAILED");
        }else if(score >=40 && score <50){
            System.out.println("Your grade: PASS MARK");
        }else if(score >=50 && score <60) {
            System.out.println("Your grade: 2:2");
        }else if (score >=60 && score <70) {
            System.out.println("Your grade: 2:1");
        }else {
            System.out.println("Your grade: 1:1");
        }

        System.out.println("Do you want to enter another grade: ");
        choice = scan.next();
        if(choice.equalsIgnoreCase("yes")) {
            try{
            System.out.println("Enter your percentage mark: ");
            score = scan.nextInt();
            }catch(InputMismatchException e) {
                System.err.print("Incorrect Input");
            }
        }
    }while(!choice.equalsIgnoreCase("No"));

    }catch(InputMismatchException e) {
        System.err.println("Incorrect Input ");
    }

    System.out.println("program terminated");
    scan.close();

}

   }

3 个答案:

答案 0 :(得分:2)

你的拦截块不会转移控制权。 (例如,通过返回或抛出另一个异常)这意味着在打印消息后,程序会检查while条件。由于在这种情况下该条件永远不会成立,因此它将使用分数重新运行循环。

更新得分的声明引发了异常,因此未更新。

答案 1 :(得分:0)

应该是while(choice.equalsIgnoreCase("No"));而不是while(!choice.equalsIgnoreCase("No"));

固定它!现在试一试。

import java.util.InputMismatchException;
import java.util.Scanner;
public class CatchingException {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score;
String choice;

try {
System.out.println("Enter your percentage mark: ");
score = scan.nextInt();


do {
    if(score <40) {
        System.out.println("You FAILED");
    }else if(score >=40 && score <50){
        System.out.println("Your grade: PASS MARK");
    }else if(score >=50 && score <60) {
        System.out.println("Your grade: 2:2");
    }else if (score >=60 && score <70) {
        System.out.println("Your grade: 2:1");
    }else {
        System.out.println("Your grade: 1:1");
    }

    System.out.println("Do you want to enter another grade: ");
    choice = scan.next();
    if(choice.equalsIgnoreCase("yes")) 
        System.out.println("Enter your percentage mark: ");{
        try{
        score = scan.nextInt();
        }catch(InputMismatchException e) {
            System.err.print("Incorrect Input");
        }
    }
}while(choice.equalsIgnoreCase("No"));

}catch(InputMismatchException e) {
    System.err.println("Incorrect Input ");
}

System.out.println("program terminated");
scan.close();

 }

}


enter image description here

答案 2 :(得分:0)

如果用户输入错误的输入数据,您需要继续向用户重复询问百分比。

现在,它捕获异常,然后输入do while循环,从而再次打印等级。您可能需要执行以下操作:

do {
    System.out.println("Enter your percentage mark: ");//inner one
    try{
        score = scan.nextInt();
    }catch(InputMismatchException e) {
        System.err.print("Incorrect Input");
        score = -1;
    }
}while(score == -1);

所以你循环直到得分为-1(得分不能在考试中为负,因此为-1)。因此,下次当您使用无效输入运行它时,它将捕获异常并将分数设置为-1,然后检查while条件,该条件将满足并因此将再次从do开始,即要求用户输入百分比。

注意:如果您在第一次输入分数时输入了无效的数字(比如一个字符串),那么您的程序将在打印后输出&#34;错误输入&#34;到错误控制台。