1

在这个程序中,char g = (char)(br.read())每当我运行程序时都会跳过该行。输入年龄后,如果我输入性别为 m,则会收到错误 java.lang.NumberFormatException。

import java.io.*;
public class Tax
{
    public static void main()throws IOException
    {
        InputStreamReader isr  = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("Enter age");
        int age = Integer.parseInt(br.readLine());
        System.out.println("Enter gender as m or f");
        char g = (char)(br.read());
        System.out.println("Enter Taxable Income");
        int ti = Integer.parseInt(br.readLine());
        double in;
        if (age > 65 || g == 'f')
        System.out.println("Wrong Category");
        else
        {
            if (ti <= 160000)
            in = 0;
            else if(ti > 160000 && ti<=500000)
            in = (ti - 160000) * (10.0/100.0);
            else if(ti > 500000 && ti<=800000)
            in = (ti - 50000) * (20.0/100.0) + 34000;
            else
            in = (ti - 800000) * (30.0/100.0) + 94000;
            System.out.println("Income Tax = " + in);
        }
    }
}

但是如果程序被修改如下,即如果char g = (char)(br.read());被替换String g = br.readLine(),它可以工作

import java.io.*;
public class Tax
{
    public static void main()throws IOException
    {
        InputStreamReader isr  = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("Enter age");
        int age = Integer.parseInt(br.readLine());
        System.out.println("Enter gender as m or f");
        String g = br.readLine();
        System.out.println("Enter Taxable Income");
        int ti = Integer.parseInt(br.readLine());
        double in;
        if (age > 65 || g.equalsIgnoreCase("f"))
        System.out.println("Wrong Category");
        else
        {
            if (ti <= 160000)
            in = 0;
            else if(ti > 160000 && ti<=500000)
            in = (ti - 160000) * (10.0/100.0);
            else if(ti > 500000 && ti<=800000)
            in = (ti - 50000) * (20.0/100.0) + 34000;
            else
            in = (ti - 800000) * (30.0/100.0) + 94000;
            System.out.println("Income Tax = " + in);
        }
    }
}
4

3 回答 3

6

问题是您只从阅读器中读取一个字符 - 留下行分隔符部分仍有待阅读。

因此,如果BufferedReader逻辑上包含:

30\r\nm\r\n20

然后:

  • readLine()第一次调用会消耗"30\r\n"
  • 然后调用read()将消耗"m"离开\r\n20
  • 第二次调用readLine()会消耗"\r\n",返回一个空字符串

如果您实际上键入“30”,按回车,然后键入“m20”并按回车,它会起作用 - 但显然这不是您想要的 :)

要么使用扫描仪,要么只使用readLine()并检查“m”和“f”行的内容。

于 2013-01-09T18:17:04.690 回答
2

尝试使用扫描仪

     char g;
     Scanner scan = new Scanner (System.in);
     g = scan.nextChar();`
于 2013-01-09T18:16:53.973 回答
0

你只读一个字节,这意味着如果你写类似的东西

m 22

这意味着您在数字前有一个空格,如果您键入

m
22

这意味着后面的行的其余部分m是空白的。

我就是这样写的

Scanner in  = new Scanner(System.in);
System.out.println("Enter age");
int age = in.nextInt(); in.nextLine();
System.out.println("Enter gender as m or f");
String mf = in.next(); in.nextLine();
System.out.println("Enter Taxable Income");
int ti = in.nextInt(); in.nextLine();
于 2013-01-09T18:18:22.950 回答