简体   繁体   English

do-while循环问题

[英]Issues with do-while loop

I am just a beginner in java coding i was just writing a simple program : user is given a menu he has to enter number between 1-4 if user enters correct number, required task is done if wrong number is entered,user is again asked for input. 我只是Java编码的初学者,我只是写一个简单的程序:给用户一个菜单,如果用户输入正确的数字,他必须在1-4之间输入数字,如果输入了错误的数字,则必须完成任务,再次询问用户用于输入。 Below is my program 下面是我的程序

class menu {
  public static void main(String [] args) throws java.io.IOException {
        int choice;
        do
        {
            System.out.println("HELP MENU: ");
            System.out.println("IF STATEMENT: 1 ");
            System.out.println("WHILE: 2 ");
            System.out.println("DO WHILE: 3 ");
            System.out.println("SWITCH: 4 ");
            choice = System.in.read();
            System.out.println(choice);
        }

        while( choice < 1 || choice > 4);
        System.out.println("\n");
        System.out.println(choice);

        switch (choice)
        {
        case 1:
            System.out.println("if statement is selected");
            break;
        case 2:
            System.out.println("while statement is selected");
            break;

        case 3:
            System.out.println("do while statement is selected");
            break;

        case 4:
            System.out.println("switch statement is selected");
            break;
        }
    }
}

OUTPUT: +++++++ 输出: +++++++

E:\study\javacode>java menu 
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
4
52
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
13
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
10
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4

what ever user is entering through keyboard ,the code keeps on iterating through do-while loop.i identified the cause by printing the input value and what i found is that input value is taken wrong by the code.Please help to solve this 用户通过键盘输入的内容都会不断重复执行do-while循环。我通过打印输入值确定了原因,发现代码输入值有误。请帮助解决此问题

To read numbers or String s, I would recommend you to use a Scanner object. 要读取数字或String ,我建议您使用Scanner对象。 Create and instantiate it at the begining of the main() : main()的开头创建并实例化它:

Scanner in = new Scanner(System.in);

and call the nextInt() method in the do-while : 并在do-while调用nextInt()方法:

do {
    System.out.println("HELP MENU: ");
    System.out.println("IF STATEMENT: 1 ");
    System.out.println("WHILE: 2 ");
    System.out.println("DO WHILE: 3 ");
    System.out.println("SWITCH: 4 ");
    choice = in.nextInt();
    System.out.println(choice);
} while (choice < 1 || choice > 4);

Notes: 笔记:

  • System.in.read() is actually returning the int value of the character you input. System.in.read()实际上返回您输入的字符的int值。 For example if you input 1 , the method will return 49 which is equal to (int)'1' 例如,如果输入1 ,则该方法将返回49 ,它等于(int)'1'
  • As I mentioned before, you can use Scanner to read String s, with the method nextLine() . 如前所述,您可以使用nextLine()方法使用Scanner读取String

Edit: 编辑:

Just to see that you can use System.in.read() (you must read the documentation to understand what it does) to read an integer, try this (in a separate file so you don't accidentally modify your code): 只是为了看到可以使用System.in.read() (必须阅读文档以了解其作用)来读取整数,请尝试这样做(在单独的文件中,以免意外修改代码):

int i = System.in.read();
System.out.println(Integer.parseInt(Character.toString(((char) i))));

The issue is that you're using InputStream.read(), which reads a byte from the stream, not a character, eg if you type '1', read() will return 0x31. 问题是您使用的是InputStream.read(),它从流中读取一个字节,而不是字符,例如,如果您键入“ 1”,则read()将返回0x31。

Javadoc of read(): read()的Javadoc:

/**
 * Reads the next byte of data from the input stream. The value byte is
 * returned as an <code>int</code> in the range <code>0</code> to
 * <code>255</code>. If no byte is available because the end of the stream
 * has been reached, the value <code>-1</code> is returned. This method
 * blocks until input data is available, the end of the stream is detected,
 * or an exception is thrown.
 *

System.in.read() does not quite work like you think it does. System.in.read()并不像您认为的那样工作。 It reads a character (not an int) and returns a byte value, not a integer. 它读取一个字符(不是int)并返回一个字节值,而不是整数。

When a user enters "1", read() returns 49, which is the integer byte value for character '1'. 当用户输入“ 1”时, read()返回49,它是字符“ 1”的整数字节值。 (50 is '2', 51 is '3', etc). (50是“ 2”,51是“ 3”,依此类推)。

@Christian's scanner suggestion is very good. @Christian的扫描仪建议非常好。 I think you should do that instead. 我认为您应该这样做。

Alternately, you can change your switch statement to use 49/50/51/etc, but that's kind of ugly. 或者,您可以将switch语句更改为使用49/50/51 / etc,但这有点丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM