Java System.in.read();比较

Java System.in.read();比较,java,Java,在我的课堂上,我们开始学习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: "); c

在我的课堂上,我们开始学习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"); 
   }
}
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");

    }
}
输出如下:

在这个截图中,我刚启动程序,键入“a”,然后按enter键

当我运行程序时,它会要求我输入第一个字符,然后它会要求我输入第二个字符,但它会跳过该字符并打印出“不同”的结尾。我不知道为什么

代码应比较用户输入的两个字符,如果它们相同,则应打印“相同”,否则应显示“不同”


谢谢你的回答

在这些情况下,您可能需要使用扫描仪:

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

这只是一个例子。您的问题是换行符,所以扫描器可能会在这里帮助您。

正如前面所说,您从扫描器中读取的第二个字符会在第一个字符后读取某些内容,而这些内容是换行符(或按您的想法输入)。您不相信,执行此代码:

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

    }
}
产出:

  • 按一个键并按Enter:a
  • 您的第一次阅读包含
  • 按第二个键并按Enter键:
  • 你的第二次阅读包含
  • 不同的
  • 见第5行。是打印新的行

    现在,如果您按Enter键两次(第一个和第二个字符),结果将是相同的

    附言。
    您可以使用调试器而不是打印到控制台,这将是首选方式

    我对您的程序做了一些更改,让您了解正在发生的事情:

     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");
    }
    
    运行上述程序两次: -键入“a”并按enter键。 -按回车键两次

    解释: in是InputStream的实例,是InputStream的读取方法,它“从输入流读取下一个字节的数据”。 问题是,当你点击回车键时,你正在向输入流中添加一个额外的字节,而ch2总是会存储它。
    *注意:x1,x2将显示您所点击字符的ASCII码。

    第二个键是Enter,或换行符。换行符Enter应该只是确认选择。它们是一样的。我有点困惑。在我输入第一个字符并用enter确认后,它应该要求我再次为第二个字符执行该操作,但它跳过了该操作。为什么?你的第二个角色是新台词。按enter键时,实际上是在创建新行。如果打印出第二个字符,您将在控制台中看到它实际上是一个新行。谢谢!我知道扫描仪,并问我的老师为什么我们不使用它,但他说他想要一个解释,为什么这个代码不起作用。我想我应该这么说,不过还是谢谢你!什么都有帮助。当您以文本形式读取文本时,请使用扫描仪。使用
    (char)System.in.read()
    ,您可以使用终端的字符编码(无论是什么)将文本读取为字节,然后将其转换为字符编码为ISO-8859-1(由于UTF-16的特性,这是
    char
    的编码)。尝试键入(或粘贴)ŝ。由于此代码,您将得到“垃圾”。