从系统输入中输入while条件不起作用

时间:2017-12-19 11:21:53

标签: java bufferedreader

main方法中,我正在阅读system.in的输入并将其传递给while条件。但它没有用。每次默认为53例。无法弄清楚错误在哪里。

如果我在while循环上方手动指定int num = 15而不是int num = br.read()。它工作正常。

public class Parenthesis
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter number of test cases: ");
        int num = br.read();
        while(num > 0)
        {
            System.out.println(num-- + "Chances Left");
            String str = br.readLine();
            if(isParenthesis(str))
                System.out.println("Cool Rudra");
            else
                System.out.println("Poor Rudra");
        }
    }

    public static boolean isParenthesis(String str)
    {
        if(str == "Rudra")
            return true;
        else
            return false;
    }
}

2 个答案:

答案 0 :(得分:0)

使用以下

int num = Integer.parseInt(br.readLine()); 

而不是

int num = br.read();

答案 1 :(得分:0)

从用户

获取int的另一种方法
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();