我正在制作一个程序,该程序以一个人的名字和薪水为基础,并计算税款。但是每次运行它时,它都需要前两个输入(计数和名称),然后出现此错误:
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextLong(Scanner.java:2373)
at java.base/java.util.Scanner.nextLong(Scanner.java:2328)
at com.ishaan.javaapp.Main.main(Main.java:31)
这是我编写的代码:
public class Main {
public static void calculateTax(String str, long sal){
long tax=0;
if(sal >= 300000){
tax = (sal * 20)/100;
}
else if(sal >= 100000 && sal < 300000){
tax = (sal * 10)/100;
}
else if(sal < 100000){
tax = 0;
};
System.out.println(str + " : ₹ " + tax);
}
public static void main(String[] args) {
int count;
Scanner scanner = new Scanner(System.in);
count = scanner.nextInt();
String[] name = new String[count];
long[] salary = new long[count];
for(int i=1; i<=count; i++){
name[i] = scanner.nextLine();
salary[i] = scanner.nextLong();
}
for (int i=1; i<=count; i++){
calculateTax(name[i], salary[i]);
}
}
}
我必须使用长数据类型,并且似乎nextLong()是导致问题的一个。任何帮助将不胜感激。