Java输入不匹配异常

时间:2015-10-28 06:16:09

标签: java

我是一名新手java学生,我需要根据他们制作的单位来计算员工奖金。每当我运行程序时,由于输入而导致扫描仪错误。 “java.util.InputMismatchException”。任何帮助是极大的赞赏!

 /**
          * NAME: Mitchell Noble
          * DATE: October 27, 2015
          * FILE: lab9
          * COMMENTS: This java program is designed to caculate employee bonus's based on their units produced
          */
           import java.util.Scanner;

        public class lab9
        {
           public static void main(String[]args)
           {
             // declare variables
             double Lastname;
             double Currentunits;
             double Lastunits;
             double Firstname;

             //Create a Scanner Object
              Scanner keyboard= new Scanner(System.in);

             //Get names and units
             System.out.print("Hello we are going to learn about your production this year and award bonus's but first we need some information.");
             System.out.print("What is your last name?");
             Lastname = keyboard.nextDouble();
             System.out.print("What is your first name?");
             Firstname = keyboard.nextDouble();
             System.out.print("How many units did you produce this year?");
             Currentunits = keyboard.nextDouble();
             System.out.print("How many units did you produce last year?");
             Lastunits = keyboard.nextDouble();
             //Sort into proper bonus category
            if (Currentunits > Lastunits)
              {
                 if (Currentunits >= 1000)
                 {
                    if (Currentunits >= 3001)
                    {
                       if (Currentunits >= 6001)
                       {
                         System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $200.  Good work!");
                       }
                       else
                       {
                         System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $100.  Good work!");
                       }
                    }
                    else
                    {
                       System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $50.  Good work!");
                    }
                 }
                 else
                 {
                    System.out.print(Firstname + Lastname + "Based on your units produced this year your bonus will be $25. Good work!");
                 }
              }
              else
              {
                System.out.print(Firstname + Lastname + "Based on the units produced this year compared to last year you do not qualify for any bonu's.");
              }
           } // close main
        } // close lab7

3 个答案:

答案 0 :(得分:1)

你为什么要读一个双姓的姓氏?

         double Lastname;

将其定义为字符串,如下所示:

String Lastname;

并更改您的代码以从

中读取姓氏
         Lastname = keyboard.nextDouble();

使用nextLine读取字符串,如下所示:

         Lastname = keyboard.nextLine();

同样适用于firstName

答案 1 :(得分:0)

尝试使用以下代码

 String Lastname;
     double Currentunits;
     double Lastunits;
     String Firstname;

     //Create a Scanner Object
      Scanner keyboard= new Scanner(System.in);

     //Get names and units
     System.out.print("Hello we are going to learn about your production this year and award bonus's but first we need some information.");
     System.out.print("What is your last name?");
     Lastname = keyboard.nextLine();
     System.out.print("What is your first name?");
     Firstname = keyboard.nextLine();
     System.out.print("How many units did you produce this year?");
     Currentunits = keyboard.nextDouble();
     System.out.print("How many units did you produce last year?");
     Lastunits = keyboard.nextDouble();
     //Sort into proper bonus category
    if (Currentunits > Lastunits)
      {
         if (Currentunits >= 1000)
         {
            if (Currentunits >= 3001)
            {
               if (Currentunits >= 6001)
               {
                 System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $200.  Good work!");
               }
               else
               {
                 System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $100.  Good work!");
               }
            }
            else
            {
               System.out.print(Firstname + Lastname + "Based on your units prodcued this year your bonus will be $50.  Good work!");
            }
         }
         else
         {
            System.out.print(Firstname + Lastname + "Based on your units produced this year your bonus will be $25. Good work!");
         }
      }
      else
      {
        System.out.print(Firstname + Lastname + "Based on the units produced this year compared to last year you do not qualify for any bonu's.");
      }

答案 2 :(得分:0)

根据定义,双精度数据类型是双精度64位IEEE 754浮点。其值的范围是4.94065645841246544e-324d至1.79769313486231570e + 308d。它不代表两个名字!因此,您真的想将firstNamelastName设置为String