永远循环

发布于 2024-08-12 00:07:15 字数 1470 浏览 6 评论 0原文

我正在尝试循环异常,但由于某种原因,它没有给我重新编写扫描仪文件的选项:

我不知道如何使用 BufferedReader,所以这就是我使用它的原因。有什么线索吗?

这是我的标准类和我的方法

package arrayExceptionsWithInput;
import java.util.*;
public class GetThoseNumbersBaby {

    int firstInt;
    int secondInt;
    boolean error;
    Scanner yourNumbers = new Scanner(System.in);

    public void findNumbers()
    {
        System.out.println(" please enter your first number");
        firstInt = yourNumbers.nextInt();
        System.out.println(" pleas enter your second number");
        secondInt = yourNumbers.nextInt();
        int finalInt = (firstInt+secondInt);
        System.out.println("total is: " + finalInt);
    }
}

这是我的主类,除了用循环实现的例外:

package arrayExceptionsWithInput;
import java.util.*;

public class InputException
{
    public static void main(String[] args)
    {
        boolean error = false;
        GetThoseNumbersBaby zack = new  GetThoseNumbersBaby();


        {
            do {
                try
                {
                    zack.findNumbers();
                }
                catch(InputMismatchException Q)
                {
                    System.out.println(" one of your integers was incorrect, please try again");
                    Q.printStackTrace();
                    error = true;
                } 
            } while (error == true);
        }
        error = false;
    }
}

如果有人对如何以不同的方式循环有任何想法,我洗耳恭听。

I'm trying to loop an exception, but for some reason its not giving me the option to re write my scanner file:

I don't know how to use BufferedReader so that's why I'm using this. Any clues?

Here's my standard class with my method

package arrayExceptionsWithInput;
import java.util.*;
public class GetThoseNumbersBaby {

    int firstInt;
    int secondInt;
    boolean error;
    Scanner yourNumbers = new Scanner(System.in);

    public void findNumbers()
    {
        System.out.println(" please enter your first number");
        firstInt = yourNumbers.nextInt();
        System.out.println(" pleas enter your second number");
        secondInt = yourNumbers.nextInt();
        int finalInt = (firstInt+secondInt);
        System.out.println("total is: " + finalInt);
    }
}

And here's my main class with the exception being implemeted with a loop:

package arrayExceptionsWithInput;
import java.util.*;

public class InputException
{
    public static void main(String[] args)
    {
        boolean error = false;
        GetThoseNumbersBaby zack = new  GetThoseNumbersBaby();


        {
            do {
                try
                {
                    zack.findNumbers();
                }
                catch(InputMismatchException Q)
                {
                    System.out.println(" one of your integers was incorrect, please try again");
                    Q.printStackTrace();
                    error = true;
                } 
            } while (error == true);
        }
        error = false;
    }
}

If anyone has any ideas on how to loop this differently I'm all ears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-08-19 00:07:15

在操作之前将错误设置为 false。这样,如果用户正确的话,您就拥有正确的退出条件。

error = false;
zack.findNumbers(); 

Set error false before the action. That way you have the correct exit condition if the user gets it right.

error = false;
zack.findNumbers(); 
眼泪淡了忧伤 2024-08-19 00:07:15

我决定也摆脱方法类
并将该方法放入try异常区域即可。

在扫描仪之后使用了 .nextLine ,它似乎已修复。

这看起来还可以吗?

package arrayExceptionsWithInput;
import java.util.*;
public class InputException
{
public static void main(String[] args)
{
boolean error = false;
int firstInt;
int secondInt;
Scanner yourNumbers = new Scanner(System.in);
{
    do{
            try
            {

                    System.out.println(" please enter your first number");
                    firstInt = yourNumbers.nextInt();
                    yourNumbers.nextLine();
                    System.out.println(" pleas enter your second number");
                    secondInt = yourNumbers.nextInt();
                    yourNumbers.nextLine();
                    int finalInt = (firstInt+secondInt);
                    System.out.println("total is: " + finalInt);
                    yourNumbers.nextLine();

            }
            catch(InputMismatchException Q)
            {

                Q.printStackTrace();
                System.out.println(" one of your integers was incorrect, please try again");
                error = true;
                yourNumbers.nextLine();
            }   
        }while (error == true);
    }error = false;
}
}

i decided too get rid of the method class
and just put the method into the try exception area.

used a .nextLine after scanner and it seems fixed.

this looks ok?

package arrayExceptionsWithInput;
import java.util.*;
public class InputException
{
public static void main(String[] args)
{
boolean error = false;
int firstInt;
int secondInt;
Scanner yourNumbers = new Scanner(System.in);
{
    do{
            try
            {

                    System.out.println(" please enter your first number");
                    firstInt = yourNumbers.nextInt();
                    yourNumbers.nextLine();
                    System.out.println(" pleas enter your second number");
                    secondInt = yourNumbers.nextInt();
                    yourNumbers.nextLine();
                    int finalInt = (firstInt+secondInt);
                    System.out.println("total is: " + finalInt);
                    yourNumbers.nextLine();

            }
            catch(InputMismatchException Q)
            {

                Q.printStackTrace();
                System.out.println(" one of your integers was incorrect, please try again");
                error = true;
                yourNumbers.nextLine();
            }   
        }while (error == true);
    }error = false;
}
}
我只土不豪 2024-08-19 00:07:15

'error'变量具有值'true'时循环。然而,只有当抛出 时(即执行'catch' 块时),它才变为'true'。控制流达不到它。

You loop while 'error' variable has value 'true'. However, it becomes 'true' only when the is thrown (i.e. when 'catch' block is executed). Control flow doesn't reach it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文