2016-11-12 18 views
1

我不知道我在做什麼錯,分配是創建一個代碼,將溫度從C轉換爲F或從F轉換爲C,直到用戶決定完成爲止,我也是應該在出現無效字符時打印錯誤消息,並讓用戶在不再次詢問數字部分的情況下更正錯誤消息。Java temp轉換程序。正確的輸出,但錯誤

該程序似乎運行良好,直到我輸入'c''C''f'或'F'以外的值。在這一點上,我仍然得到所需的輸出,但比我得到一個錯誤,這裏是我的代碼。

import java.util.Scanner; 

public class ProjectThree 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Please enter a temperature to be converted followed" 
      + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
      + "\nfinished converting temperatures enter done."); 
     String userInput, intString; 
     userInput = keyboard.nextLine(); 

     while (!(userInput.equalsIgnoreCase("done"))) 
     { 
      int length, temp, degreesC, degreesF; 
      length = userInput.length(); 
      intString = userInput.substring(0,length - 1); 
      temp = Integer.parseInt(intString); 
      char scale = userInput.charAt(length - 1); 

      while (!((scale == 'c') || (scale == 'C') || (scale =='f') || 
       (scale == 'F'))) 
      { 
       System.out.println("Error: Invalid temperature unit. Enter a C or c" 
        + " for Celsius or an F or f for Fahrenheit."); 
       String errorInput = keyboard.next(); 
       scale = errorInput.charAt(0); 
       userInput = intString + errorInput; 
      } 
      switch (scale) 
      { 
       case 'C': 
       case 'c': 
        degreesF = (9 * (temp/5) + 32); 
        System.out.println(userInput + " is equal to " + degreesF 
         + "F"); 
        break; 
       case 'F': 
       case 'f': 
        degreesC = (5 * (temp - 32))/9; 
        System.out.println(userInput + " is equal to " + degreesC 
         + "C"); 
        break; 
      } 
      System.out.println("\nPlease enter a temperature to be converted followed" 
       + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
       + "\nfinished converting temperatures enter done."); 
      userInput = keyboard.nextLine(); 
     } 
    } 
} 

,誤差

Please enter a temperture to be converted followed 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 
by a C or c for Celsius or an F or f for Farenheit. If 
finished converting tempertures enter done. 
    at java.lang.String.substring(String.java:1955) 
    at ChapterFour.ProjectThree.main(ProjectThree.java:33) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 5 seconds) 
+0

您應該將代碼放在try catch語句中 –

+0

哪個是生成此異常的輸入? –

+0

使用調試器,你會發現爲什麼你會得到這個異常 – Jens

回答

0

一般情況下,有很多的事情,「可能」去錯了錯誤的輸入,因爲你沒有任何錯誤檢查做。這裏有一些我注意到的事情...

1)正如我認爲別人提到的,你沒有檢查任何輸入的長度,然後才行事。如果有人在沒有其他事情的情況下進入,您可能會遇到異常情況。相同,如果他們輸入無效輸入。

2)switch語句缺少默認子句。你應該添加一些打印出「出錯了,請再試一次」的東西,以便知道它發生了。

3)當你只詢問字符時,我首先看到你使用keyboard.nextLine()和keyboard.next()。如果在字符後面輸入回車,則換行符可能會被下一個nextLine()拾取,導致打印錯誤,如上面1中所述。雖然谷歌搜索未來的)行爲(以確認這一點,我碰到下面的(其他人似乎也有類似的問題):

Issues with nextLine();

+0

我實際上已將默認條款從交換機中取出,因爲它只能在適當的情況下激活。關於增加檢查長度的好處!我沒有考慮到這些輸入錯誤 – OnlinePseudonym

0

爲空字符串或長度one你沒有處理任何事情的串所以它throwexception。你需要用戶try catch塊或做我喜歡的事情。我簡單地忽略輸入字符串的長度zeroone

Scanner keyboard = new Scanner(System.in); 

     System.out.println("Please enter a temperature to be converted followed" 
       + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
       + "\nfinished converting temperatures enter done."); 

     String userInput, intString; 
     userInput = keyboard.nextLine(); 

     while (!(userInput.equalsIgnoreCase("done"))) { 
      int length, temp, degreesC, degreesF; 
      length = userInput.length(); 
      System.out.println(length); 
      if (length > 1) { 
       intString = userInput.substring(0, length - 1); 
       temp = Integer.parseInt(intString); 
       // System.out.println("Temp = " + temp); 
       char scale = userInput.charAt(length - 1); 
       // System.out.println("scale" + scale); 

       while (!((scale == 'c') || (scale == 'C') || (scale == 'f') || (scale == 'F'))) { 
        System.out.println("Error: Invalid temperature unit. Enter a C or c" 
          + " for Celsius or an F or f for Fahrenheit."); 
        String errorInput = keyboard.next(); 
        scale = errorInput.charAt(0); 
        userInput = intString + errorInput; 
       } 
       switch (scale) { 
       case 'C': 
       case 'c': 
        degreesF = (9 * (temp/5) + 32); 
        System.out.println(userInput + " is equal to " + degreesF + "F"); 
        break; 
       case 'F': 
       case 'f': 
        degreesC = (5 * (temp - 32))/9; 
        System.out.println(userInput + " is equal to " + degreesC + "C"); 
        break; 
       } 
      } 

      System.out.println("\nPlease enter a temperature to be converted followed" 
        + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
        + "\nfinished converting temperatures enter done."); 
      userInput = keyboard.nextLine(); 
     } 

注意String substring(int beginIndex, int endIndex):返回substring從給定的index(beginIndex)直至成爲規定index(endIndex)開始。對於例如「塔尼亞」。 substring(2,5)將返回「ait」。它拋出IndexOutOfBoundsException如果beginIndex小於零或beginIndex > endIndexendIndex大於Stringlength

在你的情況,如果你只是erter cC lenght-1成爲zero這是你的last index。還有你的first index也是zero。那就是爲什麼你得到了IndexOutOfBoundsException

希望你明白了。