简体   繁体   English

System.in.read()不读取

[英]System.in.read() does not read

I am new to Java, so please forgive me if it is a very small mistake, 我是Java的新手,如果这是一个很小的错误,请原谅我,
here's my code: 这是我的代码:

import java.io.*;
public class election
{
    public static void main(String args[])
    {
        boolean x=false;
        Ballot ballot=new Ballot();
        int n;
        while(x!=true)
        {
            System.out.println("Cast your vote to(1-5): ");
            try
            {
            n=System.in.read();
            System.out.flush();
            ballot.add(n);
            System.out.println("Enter 0 to exit, enter 1 to vote again: ");
            n = System.in.read();
            if(n==0)
            {
                x=true;
            }
            else
            {
                x=false;
            }
            }
            catch(IOException e)
            {
                System.out.println("I/O Error");
                System.exit(1);
            }
        }
    }
}
class Ballot
{
    int votes,spoilt;
    int cand[] = new int[5];

    //methods
    void add(int n)
    {
        votes=votes+1;
        if(n <= 5 && n>=1)
        {
            cand[n-1]=cand[n-1]+1;
        }
        else
        {
            spoilt = spoilt + 1;
        }
    }
    void display()
    {
        System.out.println("Total votes cast: " + votes);
        for(int i=0;i<5;i++)
        {
            System.out.println("Candidate " + (i+1) + ": " + cand[i]);
        }
        System.out.println("Spoilt: " + spoilt);
        System.out.println("Valid votes: " + (votes-spoilt));
    }
    Ballot()
    {
        votes=0;
        spoilt=0;
        for(int i=0;i<5;i++)
        {
            cand[i]=0;
        }
    }
}

when i run it after compiling, the 18th line(n = System.in.read()) gets skipped. 当我在编译后运行它时,第18行(n = System.in.read())被跳过。
The output I get is this: 我得到的输出是这样的:

Cast your vote to(1-5): 对(1-5)投您的一票:
1 Enter 0 to exit, enter 1 to vote again: 1输入0退出,输入1再次投票:
Cast your vote to(1-5): 对(1-5)投您的一票:
2 Enter 0 to exit, enter 1 to vote again: 2输入0退出,输入1再次投票:
Cast your vote to(1-5): 对(1-5)投您的一票:
^C ^ C

The value of n is not read() which makes the program an infinite loop. n的值不是read() ,这会使程序成为无限循环。

Thank You for your help. 谢谢您的帮助。

I think it would be easier if you made use of the Scanner class. 我认为,如果您使用Scanner类,会更容易。 You can instantiate a scanner object like so Scanner scan = new Scanner(System.in); 您可以像这样实例化扫描程序对象,例如: Scanner scan = new Scanner(System.in); . Then line 18 can become n = scan.nextInt(); 然后第18行可以变成n = scan.nextInt(); This should properly read the user input. 这应该正确读取用户输入。 For more information on the Scanner class see the Java docs: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html . 有关Scanner类的更多信息,请参见Java文档: http : //docs.oracle.com/javase/6/docs/api/java/util/Scanner.html Hope this helped. 希望这会有所帮助。

System.in.read() only reads a single byte, however the standard input stream only flushes when you hit "Enter". System.in.read()仅读取一个字节,但是只有在您按“ Enter”键时才会刷新标准输入流。 So, by the time the first call returns, there are two bytes to be read ( '1' , '\\n' ). 因此,在第一个调用返回时,有两个字节要读取( '1''\\n' )。 The '1' is returned normally, then on the next call the '\\n' is returned. 通常返回'1' ,然后在下一次调用时返回'\\n'

I should also point out the the value read is not converted to an int as you seem to be expecting. 我还应该指出,所读取的值没有像您期望的那样转换为int '1' , for instance, will be 49 in decimal, or 32 in hex. 例如, '1'将是十进制的49或十六进制的32 You could try converting it by subtracting '0' , but as others pointed out it's better to use a more high level function to input integers. 可以尝试通过减去'0'转换,但正如其他人指出的那样,最好使用更高级别的函数输入整数。

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

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