gpt4 book ai didi

java - 线程中的异常 "main"java.util.Input MismatchException

转载 作者:行者123 更新时间:2023-12-02 09:25:54 24 4
gpt4 key购买 nike

我正在做类作业。你得计算一下这个月的工资。每次我尝试运行它时,它都会一直这样说:我怎样才能解决这个问题?线程“main”中的异常 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);
}
}

最佳答案

它似乎失败的提示是:

"Please enter your name"

如果您输入的是您的姓名而不是整数,这就是它抛出异常的原因。扫描仪将寻找一个整数,但你的名字不是。您可能想要替换:

int name = sc.nextInt();

与:

String name = sc.next();

或类似于将名称获取为字符串的内容。

关于java - 线程中的异常 "main"java.util.Input MismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com