简体   繁体   English

System.in.read(); 比较

[英]System.in.read(); comparing

In my class we are starting to learn java, we were tasked to explain why this code: 在我的课堂上,我们开始学习Java,我们受命解释为什么这段代码:

import java.io.IOEXception;

 class Inputif {
    public static void main(String args[]) throws java.io.IOException {

         char ch, ch2;

         System.out.print("Hit a key and press Enter: ");

         ch = (char) System.in.read();

         System.out.println("Hit a second key and press Enter: ");

         ch2 = (char) System.in.read();

         if (ch == ch2) System.out.println("Same"); else System.out.println("different"); 
   }
}

outputs this: output 输出此: 输出

in this screenshot i just started the program and typed "a" and then hit enter. 在此屏幕快照中,我刚刚启动程序并键入“ a”,然后按Enter。

When I run the program it asks me to enter the first char, then it should ask me to enter the second one, but it skips that and prints out the "different" end. 当我运行程序时,它要求我输入第一个字符,然后它应要求我输入第二个字符,但它会跳过该字符并打印出“不同”的结尾。 I don't know why. 我不知道为什么

The code should compare the two chars that the user inputs and if they are the same it should print "Same" else it should say "different". 该代码应比较用户输入的两个字符,如果它们相同,则应打印“ Same”,否则应显示“ different”。

Thanks for any answers! 感谢您的任何答案!

In these situations you may want to use a Scanner: 在这些情况下,您可能要使用扫描仪:

class Inputif {
    public static void main(String args[]) throws java.io.IOException {

        char ch, ch2;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Hit a key and press Enter: ");

        ch = scanner.next().charAt(0);

        System.out.println("Hit a second key and press Enter: ");

        ch2 = scanner.next().charAt(0);

        if (ch == ch2)
            System.out.println("Same");
        else
            System.out.println("different");
    }
}

It's just an example. 这只是一个例子。 Your problem is the newline, so maybe Scanner can help you here. 您的问题是换行符,因此Scanner可以在这里为您提供帮助。

I made a few changes to your program to give you some insigth into what is happening: 我对您的程序进行了一些更改,以使您对正在发生的事情有所了解:

 public static void main(String args[]) throws java.io.IOException {

    char ch, ch2;

    System.out.print("Hit a key and press Enter: ");

    ch = (char) System.in.read();

    System.out.println("Hit a second key and press Enter: ");

    ch2 = (char) System.in.read();
    int x1=ch;
    int x2 = ch2;
    System.out.println("x1="+x1+" | x2="+x2);
    if (ch == ch2) System.out.println("Same"); else System.out.println("different");
}

Run the above program two times : - type "a" and hit enter. 运行两次以上程序:-键入“ a”,然后按Enter。 - hit enter twice -按两次Enter

Explication : System.in is an instance Of InputStream and the read method of an InputStream https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html that "Reads the next byte of data from the input stream". 说明:System.in是InputStream的实例,并且是InputStream的read方法https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html ,它“读取数据的下一个字节从输入流中”。 The problem is that when you re hitting enter you are adding an extra byte to the input stream and ALWAYS ch2 will store it. 问题是,当您再次按Enter键时,您正在向输入流中添加一个额外的字节,并且ch2总是将其存储。 *Note : x1, x2 will display the ASCII codes of the chars you hit. *注意:x1,x2将显示您击中的字符的ASCII码。

Well, as already said your second read from Scanner reads the something after your first char, and that something is new line (or Enter as you think). 好吧,正如您已经说过的那样,您从Scanner进行的第二次阅读会在您的第一个字符之后读取一些内容,并且这些内容是换行符(或您认为的Enter键)。 You're not convinced so, execution of this code: 您不那么相信执行以下代码:

public class Helper {

    public static void main(String[] args) throws IOException {

        char ch, ch2;

        System.out.print("Hit a key and press Enter: ");
        ch = (char) System.in.read();
        System.out.println("your first read contains " + ch);

        System.out.println("Hit a second key and press Enter: ");
        ch2 = (char) System.in.read();
        System.out.println("your second read contains " + ch2);


        if (ch == ch2) System.out.println("Same");
        else System.out.println("different");

    }
}

outputs: 输出:

  1. Hit a key and press Enter: a 按下一个键,然后按Enter:
  2. your first read contains a 您的第一次阅读包含
  3. Hit a second key and press Enter: 按下第二个键,然后按Enter:
  4. your second read contains 您的二读包含
  5. different 不同

See, in line 5. is printed new line... 请参阅第5行。

Now, if you hit Enter twice (for the first and the second char) the result will be Same ! 现在,如果您按Enter键两次(对于第一个和第二个字符),结果将是 Same

PS You can use the debugger instead of printing to the console, and it would be the preferred way! PS您可以使用调试器,而不是打印到控制台, 将是首选的方式!

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

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