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

查看:110
本文介绍了System.in.read()不读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,所以如果这是一个非常小的错误,请原谅我,

这是我的代码:

I am new to Java, so please forgive me if it is a very small mistake,
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;
        }
    }
}

我在编译后运行它,第18行(n = System.in.read())被跳过。

我得到的输出是:

when i run it after compiling, the 18th line(n = System.in.read()) gets skipped.
The output I get is this:


投票给(1-5):

1
输入0退出,输入1再次投票:

投票到(1-5):

2
输入0退出,输入1再次投票:

投票给(1-5):

^ C

Cast your vote to(1-5):
1 Enter 0 to exit, enter 1 to vote again:
Cast your vote to(1-5):
2 Enter 0 to exit, enter 1 to vote again:
Cast your vote to(1-5):
^C

n 的值不是 read()这使程序成为无限循环。

The value of n is not read() which makes the program an infinite loop.

感谢您的帮助。

推荐答案

我认为它如果您使用Scanner类会更容易。您可以实例化扫描程序对象,如扫描程序扫描=新扫描程序(System.in); 。然后第18行可以变成 n = scan.nextInt(); 这应该正确读取用户输入。有关Scanner类的更多信息,请参阅Java文档: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html 。希望这会有所帮助。

I think it would be easier if you made use of the Scanner class. You can instantiate a scanner object like so Scanner scan = new Scanner(System.in);. Then line 18 can become 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. Hope this helped.

这篇关于System.in.read()不读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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