为了账号安全,请及时绑定邮箱和手机立即绑定

如何用某个变量结束 while 循环

如何用某个变量结束 while 循环

温温酱 2023-07-13 14:35:43
我正在制作一个带有 while 循环的奇数或偶数程序。我想弄清楚如何以某个数字结束 while 循环。现在我有 1 来继续循环,并尝试使 2 成为终止循环的数字。还试图找出如果用户输入除数字(如字母/单词)以外的任何内容时如何终止程序。package oddoreven;import java.util.Scanner;public class oddoreven {    public static void main (String[] args){                    int num;        int x = 1;        while(x == 1) {            System.out.println("Enter a number to check whether or not it is odd or even");            Scanner s = new Scanner(System.in);            num = s.nextInt();            if (num % 2 == 0)                System.out.println("The number is even");            else                 System.out.println("The number is odd");            //trying to figure out how to get the code to terminate if you put in a value that isn't a number            System.out.println("Type 1 to continue, 0 to terminate");            x = s.nextInt();        }    }}
查看完整描述

4 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

您应该尝试使用“真正的终止条件”来终止循环while(或任何与此相关的循环);它更干净,应该更容易被其他人理解。

在你的情况下,我认为最好有一个do-while循环,围绕这个逻辑有一些条件:num % 2 == 0,以及一个用于处理用户输入/验证的内部while循环。

查看完整回答
反对 回复 2023-07-13
?
繁星coding

TA贡献1797条经验 获得超4个赞

我没有完全遵循您想要的条件,因为除非有其他选项,否则 拥有继续条件和终止条件是没有意义的。

如果用户输入,您希望用户做什么34或者5?退出代码还是继续代码?好吧,如果默认是退出,那么您不需要退出代码,2因为它已经退出了!如果默认是继续,则不需要继续1,只需要退出2。因此,在这种情况下两者都做是没有意义的。

以下是使用do while循环的修改后的代码,以确保循环至少进入1 次:

   int x;

    do {

        System.out.println("Enter a number to check whether or not it is odd or even");

        Scanner s = new Scanner(System.in);

        int num = s.nextInt();

        if (num % 2 == 0)

            System.out.println("The number is even");

        else 

            System.out.println("The number is odd");

        //trying to figure out how to get the code to terminate if you put in a value that isn't a number

        System.out.println("Type 1 to check another number, anything else to terminate.");


        if (!s.hasNextInt()) {

            break;

        }

        else {

            x = s.nextInt();

        }

    } while(x == 1);

   }

请注意,我添加了一个检查来!s.hasNextInt()检查用户是否输入了除 an 之外的任何内容,并且在这些情况下int将通过从循环中终止而不抛出 an 来终止(这与在本例中终止程序相同)。Exceptionbreak


如果x是有效整数,则将x设为该值,然后循环条件检查是否x为1。如果x不是1,则循环终止,如果是,则将再次继续循环。


查看完整回答
反对 回复 2023-07-13
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

您可以尝试的另一件事是,您可以继续要求用户输入正确的输入,并且只有在他们这样做时才继续操作,而不是退出程序。我不知道你的要求是什么,但如果你想遵循良好的代码实践,那么你不应该仅仅因为用户输入了错误的输入而终止你的程序。想象一下,如果你用谷歌搜索一个有拼写错误的单词,然后谷歌就关闭了。


无论如何,这就是我的做法



import java.util.Scanner;


public class oddoreven {

    public static void main(String[] args) {

        int num;

        int x = 1;

        while (x == 1) {

            System.out.println("Enter a number to check whether or not it is odd or even");

            Scanner s = new Scanner(System.in);


            boolean isInt = s.hasNextInt(); // Check if input is int

            while (isInt == false) { // If it is not int

                s.nextLine(); // Discarding the line with wrong input

                System.out.print("Please Enter correct input: "); // Asking user again

                isInt = s.hasNextInt(); // If this is true it exits the loop otherwise it loops again

            }

            num = s.nextInt(); // If it is int. It reads the input


            if (num % 2 == 0)

                System.out.println("The number is even");

            else

                System.out.println("The number is odd");

            // trying to figure out how to get the code to terminate if you put in a value

            // that isn't a number

            System.out.println("Type 1 to continue, 0 to terminate");

            x = s.nextInt();

        }


    }

}


查看完整回答
反对 回复 2023-07-13
?
繁花如伊

TA贡献2012条经验 获得超12个赞

要在用户输入数字以外的任何内容时退出程序,请将变量 x 类型更改为字符串


if (!StringUtils.isNumeric(x)) {

    System.exit(0);

}

当用户输入 2 时退出程序


if (x == 2) {

    System.exit(0);

}


查看完整回答
反对 回复 2023-07-13
  • 4 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信