2015-10-28 72 views
0

我是一個新手Java學生,我需要根據他們的單位生產計算員工獎金。每當我運行該程序時,由於輸入而導致掃描儀錯誤。 「java.util.InputMismatchException」。任何幫助是極大的讚賞!Java輸入不匹配異常

/** 
      * 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 
+1

你試圖輸入一些內容不能被解析爲一個'double'控制檯。你能告訴我們你的意見嗎? –

+1

你正在期待'lastName','firstName'這樣的值是'double'。檢查你的代碼。我想你必須使用'nextLine()'並將這些字段聲明爲字符串 – TheLostMind

+0

爲什麼要將firstName和LastName聲明爲double而不是String? – kswaughs

回答

1

你爲什麼要讀一個姓氏的雙?

  double Lastname; 

把它定義爲字符串,像這樣:

String Lastname; 

  Lastname = keyboard.nextDouble(); 

更改您的代碼讀取姓氏使用nextLine字符串讀取,像這樣:

  Lastname = keyboard.nextLine(); 

相同適用於姓

+0

非常感謝您的幫助! –

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."); 
     } 
+0

您必須將lastName&firstName聲明爲String,並且必須使用nextLine()來獲取來自用戶的輸入。 – DineshPandian