このプログラムでは、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);
}
}
}