为了账号安全,请及时绑定邮箱和手机立即绑定

线程“main”中的异常 java.util.Input MismatchException

线程“main”中的异常 java.util.Input MismatchException

猛跑小猪 2023-10-13 16:24:43
我正在做班级的家庭作业。你得计算一下这个月的工资。每次我尝试运行它时,它都会不断地说:我该如何解决这个问题?线程“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);        }
查看完整描述

1 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

它似乎失败的提示是:

"Please enter your name"

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

int name = sc.nextInt();

和:

String name = sc.next();

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


查看完整回答
反对 回复 2023-10-13
  • 1 回答
  • 0 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信