如何用某个变量结束while循环 [英] how to end a while loop with a certain variable

查看:111
本文介绍了如何用某个变量结束while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有while循环的奇数或偶数程序.我试图弄清楚如何以一定的数字结束while循环.现在我有1个继续循环,并尝试使2终止循环.还试图弄清楚如果用户键入除字母/单词之类的数字以外的任何内容,则如何终止程序.

I am making an odd or even program with a while loop. I am trying to figure out how to end the while loop with a certain number. Right now I have 1 to continue the loop, and trying to make 2 the number that terminates it. Also trying to figure out how to terminate the program if a user types anything but a number like a letter/words.

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

    }

}

推荐答案

您可以尝试的另一种方法是,您可以继续要求用户输入正确的输入,而不必退出程序,而是退出该程序.我不知道您的要求是什么,但是如果您想通过良好的代码习惯进行操作,则不应仅因为用户输入了错误的输入而终止程序.想象一下,如果您用错字搜索了一个单词,而Google刚刚关闭.

Another thing you can try is that instead of exiting the program you can just keep asking user to enter correct input and only proceed if they do so. I don't know what is your requirement but if you want to go by good code practice then you shouldn't terminate your program just because user entered wrong input. Imagine if you googled a word with typo and google just shuts off.

无论如何,我是怎么做到的

Anyways here is how I did it


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

    }
}

这篇关于如何用某个变量结束while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆