我正在上课的家庭作业。您必须计算月薪。每当我尝试运行它时,它总是这样说: 我怎样才能解决这个问题? 线程“主”中的异常java.util.InputMismatchException
在java.util.Scanner.throwFor(Scanner.java:864)
在java.util.Scanner.next(Scanner.java:1485)
在java.util.Scanner.nextInt(Scanner.java:2117)
在java.util.Scanner.nextInt(Scanner.java:2076)
在D2.main(D2.java:23)
import java.util.Scanner;
public class D2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int L1Cs = 80;
int L2Cs = 120;
int L3Cs = 160;
int L1Ct = 100;
int L2Ct = 140;
int L3Ct = 180;
int L1Csv = 100;
int L2Csv = 150;
int L3Csv = 200;
int BSalary = 3000;
int Stotal, Ttotal, SVtotal;
System.out.println("Please enter your name");
int name = sc.nextInt();
System.out.println("How many sedans have you sold this month");
int Sedans = sc.nextInt();
System.out.println("How many trucks have you sold this month");
int Trucks = sc.nextInt();
System.out.println("How many SUVs have you sold this month");
int SUVs = sc.nextInt();
if(Sedans <= 10)
{
Stotal = (L1Cs * Sedans);
}
else if (Sedans >= 11 && Sedans <=20)
{
Stotal = (L1Cs*10 +(Sedans - 10) * L2Cs);
}
else
{
Stotal = (L1Cs*10 +L1Cs *10 + (Sedans - 10) * L3Cs);
}
System.out.println("Your comission for the sedans is:" + Stotal);
if(Trucks <= 10)
{
Ttotal = (L1Cs * Trucks);
}
else if (Trucks >= 11 && Trucks <=20)
{
Ttotal = (L1Ct*10 + (Trucks - 10) * L2Ct);
}
else
{
Ttotal = (L1Ct*10 + L2Ct *10 + (Trucks - 10) * L3Ct);
}
System.out.println("Your comission for the trucks is:" + Ttotal);
if(SUVs <= 10)
{
SVtotal = (L1Cs * SUVs);
}
else if (SUVs >= 11 && SUVs <=20)
{
SVtotal = (L1Csv*10 + (SUVs - 10) * L2Csv);
}
else
{
SVtotal = (L1Csv*10 + L2Csv *10 + (SUVs - 10) * L3Csv);
}
System.out.println("Your comission for the SUVs is:" + SVtotal);
double comission = (Stotal + Ttotal + SVtotal);
double MSalary = (comission + 3000);
}
}
答案 0 :(得分:1)
似乎失败的提示是:
"Please enter your name"
如果输入名称而不是整数,这就是它引发异常的原因。扫描程序将寻找一个整数,您的名字不是。您可能要替换:
int name = sc.nextInt();
具有:
String name = sc.next();
或类似于将名称作为字符串获取的东西。