在这个程序中,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);
}
}
}