Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当循环打印错误时_Java - Fatal编程技术网

Java 当循环打印错误时

Java 当循环打印错误时,java,Java,我正在读取来自用户的数据(考试分数)。然后我检查成绩。之后,我将显示成绩。然后我问用户是否想要进入另一个等级,如果他们说是,那么我要求他们输入另一个考试分数。但在这里,我使用try/catch来验证他们输入的数据类型。如果他们输入的不是整数,那么我尝试再次循环,询问他们是否要输入另一个等级,但同时显示输入整数错误消息。但是在这里循环打印两次你想输入另一个成绩和两次考试成绩吗?它打印旧成绩和两次你想输入另一个成绩陈述吗?为什么会发生这种情况 import java.util.InputMismat

我正在读取来自用户的数据(考试分数)。然后我检查成绩。之后,我将显示成绩。然后我问用户是否想要进入另一个等级,如果他们说是,那么我要求他们输入另一个考试分数。但在这里,我使用try/catch来验证他们输入的数据类型。如果他们输入的不是整数,那么我尝试再次循环,询问他们是否要输入另一个等级,但同时显示输入整数错误消息。但是在这里循环打印两次你想输入另一个成绩和两次考试成绩吗?它打印旧成绩和两次你想输入另一个成绩陈述吗?为什么会发生这种情况

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;
    boolean loop = true;


    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.println("Enter Integer");
                    loop = false;
                }

            }
        }while(!loop);

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

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

}

  }
import java.util.InputMismatchException;
导入java.util.Scanner;
公共类捕获异常{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
智力得分;
字符串选择;
布尔循环=真;
试一试{
System.out.println(“输入您的百分比标记:”);
score=scan.nextInt();
做{

如果(score=40&&score=50&&score=60&&score试着用分支语句和标签来简化它

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;

        Main:
        while (true) { //this will continue until we call "break Main;"
            try {
                System.out.println("Enter your percentage mark: ");
                score = scan.nextInt();

                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");
                }

                Prompt:
                while (true) { //this will continue until we call "break Prompt;"
                    System.out.println("Do you want to enter another grade: ");
                    choice = scan.next();
                    try {
                        if (choice.equalsIgnoreCase("yes")) {
                            continue Main; //OK. Start again at "Main:"
                        } else if (choice.equalsIgnoreCase("no"))
                            break Main; //OK. We're done.
                        else {
                            System.err.println("Incorrect Input ");
                            continue Prompt; //whoops! try asking again.
                        }
                    } catch (InputMismatchException e) {
                        System.err.println("Incorrect Input ");
                        continue Prompt; //whoops! try asking again.
                    }
                }
            } catch (InputMismatchException e) {
                System.err.println("Incorrect Input ");
                continue Main; //whoops! try another input.
            }
        }
        System.out.println("program terminated");
        scan.close();
    }
}
import java.util.InputMismatchException;
导入java.util.Scanner;
公共类捕获异常{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
智力得分;
字符串选择;
主要内容:
虽然(正确){//这将继续,直到我们称之为“断开主”
试一试{
System.out.println(“输入您的百分比标记:”);
score=scan.nextInt();
如果(分数<40){
System.out.println(“您失败了”);
}否则如果(分数>=40和分数<50){
System.out.println(“你的成绩:及格”);
}否则如果(分数>=50和分数<60){
System.out.println(“你的成绩:2:2”);
}否则如果(分数>=60和分数<70){
System.out.println(“你的成绩:2:1”);
}否则{
System.out.println(“你的分数:1:1”);
}
提示:
while(true){//这将继续,直到我们调用“breakprompt”
System.out.println(“您想输入另一个等级:”);
choice=scan.next();
试一试{
如果(选择。相等信号情况(“是”)){
继续Main;//确定。从“Main:
}else if(选择.相等信号情况(“否”))
断开干线;//好的,我们结束了。
否则{
系统错误打印项次(“输入错误”);
继续提示;//哇!再试一次。
}
}捕获(输入不匹配异常e){
系统错误打印项次(“输入错误”);
继续提示;//哇!再试一次。
}
}
}捕获(输入不匹配异常e){
系统错误打印项次(“输入错误”);
继续Main;//哇!请尝试其他输入。
}
}
System.out.println(“程序终止”);
scan.close();
}
}

首先,设置
循环
变量的方式表明,只要
循环
,您就希望继续迭代。在这种情况下,您应该将
while(!loop);
更改为
while(loop);

在那之后,你必须看看如果用户在被问到是否想要进入一个新的等级时写“否”会发生什么。事实上(在上面的更改中),你将继续迭代。要解决这个问题,你可以更改

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.println("Enter Integer");
        loop = false;
    }
}
差不多

System.out.println("Do you want to enter another grade: ");
choice = scan.next();
if(choice.equalsIgnoreCase("no")) {
    loop = false;
}
这将使任何输入(除“否”之外)导致循环不断迭代(如果只希望“是”起作用,那么通过一些额外的检查应该很容易修复)

当然,这样做需要你把

System.out.println("Enter your percentage mark: ");
score = scan.nextInt();
do while
循环内

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

如果用户输入的整数不正确,您希望继续要求百分比标记,可以执行以下操作:

System.out.println("Enter your percentage mark: ");
while (!scan.hasNextInt()) {
    System.out.println("Please enter a valid percentage mark: ");
    scan.next();
}
score = scan.nextInt();

这将一直询问,直到用户输入一个实际整数。

因为您的问题是
为什么会发生这种情况
我将尝试向您解释。我的输入示例如下:

Enter your percentage mark: 
70
Your grade: 1:1
Do you want to enter another grade: 
yes
Enter your percentage mark: 
a
a
不是有效整数,因此
Scanner#nextInt()
抛出一个
inputmatchException
,程序“进入”到
catch
块:

catch(InputMismatchException e) {
    System.err.println("Enter Integer");
    loop = false;
}
在这里,程序打印错误消息并将
loop
设置为
false
。由于
while
条件
while(!loop)
将要执行新的循环迭代。请注意
Scanner\nextInt()
不会从输入流中“读取”(并删除)无效输入,因此它仍然存在

因此,我们再次来到这里:

do {
    if(score <40) {
        System.out.println("You FAILED");
    }
    //...
分数验证后,如果我们想输入另一个分数,它会再次询问我们:

System.out.println("Do you want to enter another grade: ");
choice = scan.next();
还有另一个问题:我们在上一次迭代中输入的
a
仍然可用,而且由于
Scanner\#next()
接受此输入,
选项现在将是
“a”
。 这就是为什么该
if
语句返回false,其主体将被跳过(
“a”
不等于
“yes”
):

我们现在的情况是,
a
从输入流中消失,并且
loop
仍然设置为
false
。因此,程序将进行另一次循环迭代,我们处于循环的开始(再次):

好消息是:
a
从输入流中消失,下一个问题实际上请求用户输入新的内容:

System.out.println("Do you want to enter another grade: ");
choice = scan.next();
因此,由他来决定他想要什么


if(choice.equalsIgnoreCase("yes")) {
do {
    if(score <40) {
        System.out.println("You FAILED");
    }
    //...
Your grade: 1:1
System.out.println("Do you want to enter another grade: ");
choice = scan.next();
public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);
    int score;
    String choice;
    while(true)
    {
        System.out.println("Enter your percentage mark: ");
        score = scan.nextInt();
        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("no"))
        {
            System.out.println("Program terminated");
            scan.close();
            break;
        }
    }
}