2017-08-16 137 views
1

我編寫了一個將十六進制數轉換爲十進制的程序。大多數情況下它是有效的,但是,當我輸入非十六進制字符串時,它會正確計算該值並將其顯示爲非十六進制數字,但它也會跳轉到下一行並輸出它。我認爲這是這是有問題的代碼部分:將十六進制轉換爲十進制的方法 - 輸入非十六進制數時程序不正確

for (int i = 0; i < hexInput.length(); i++){ 
    int value = hexValue (hexInput.charAt(i)); 

    if (value == -1){ 
    System.out.println ("You must enter a hexadecimal number. Try again 
    please."); 
} 

這正確地識別不正確的輸入並執行println聲明。但是,它也執行的下一行代碼:

System.out.println("The hexadecimal number, " + hexInput + ", in base 10, is " + hexDecimalConversion (hexInput) + "."); 

因此,如果不正確的數字 - 說j --is進入,結果是:

您必須輸入十六進制數。請再試一次。
基數爲10的十六進制數j爲-1。

我想擺脫第二行,並恢復循環,以便用戶可以輸入另一個數字。

我只需要知道如何從頭開始恢復程序,而不是在執行完最後一行代碼後輸入了錯誤的數字。

這裏是整個程序:

import java.util.Scanner; 

public class HexBinaryConversion { 

    //method takes string as input, converts string to decimal 
    //using hexvalue method. 

    public static long hexDecimalConversion(String hexInput) { 

     long value = 0; 
     for (int i = 0; i < hexInput.length(); i++) { 
      value = value * 16 + hexValue(hexInput.charAt(i)); 
     } 
     return value; 

    } //end of HexDecimalConversion method. 

    //method takes character as input and converts it to 
    //appropriate hexadecimal value. 

    public static int hexValue(char ch) { 
     switch (ch) { 
      case '0': 
       return 0; 
      case '1': 
       return 1; 
      case '2': 
       return 2; 
      case '3': 
       return 3; 
      case '4': 
       return 4; 
      case '5': 
       return 5; 
      case '6': 
       return 6; 
      case '7': 
       return 7; 
      case '8': 
       return 8; 
      case '9': 
       return 9; 
      case 'a': 
      case 'A': 
       return 10; 
      case 'b': 
      case 'B': 
       return 11; 
      case 'c': 
      case 'C': 
       return 12; 
      case 'd': 
      case 'D': 
       return 13; 
      case 'e': 
      case 'E': 
       return 14; 
      case 'f': 
      case 'F': 
       return 15; 
      default: 
       return -1; 
     } 

    } //end of hexValue method. 

    public static void main(String[] args) { 

     Scanner stdin = new Scanner (System.in); 
     String hexInput = "0"; 

     while (!hexInput.equalsIgnoreCase ("x")){ 

     System.out.println("Enter a hexadecimal number or enter x to terminate 
     the program: "); 
     hexInput = stdin.next(); 
     if (hexInput.equalsIgnoreCase ("x")) { 

     System.out.println ("Thank you for using this program."); 
     break; 
     } 

     long answer = hexDecimalConversion (hexInput); 

     for (int i = 0; i < hexInput.length(); i++){ 
     int value = hexValue (hexInput.charAt(i)); 

     if (value == -1){ 

     System.out.println ("You must enter a hexadecimal number. Try again 
     please."); 

     break; 
     } 
     } 

     System.out.println("The hexadecimal number, " + hexInput + ", in base 
     10, is " + answer + "."); 


     }//end of while loop. 

    }//end of main(). 

}//end of class. 

這是輸出:

run: 
Enter a hexadecimal number or enter x to terminate the program: 
a 

The hexadecimal number, a, in base 10, is 10. 

Enter a hexadecimal number or enter x to terminate the program: 
45 

The hexadecimal number, 45, in base 10, is 69. 

Enter a hexadecimal number or enter x to terminate the program: 
j 

You must enter a hexadecimal number. Try again please.   
The hexadecimal number, j, in base 10, is -1. 

**************************incorrect line*************************** 

Enter a hexadecimal number or enter x to terminate the program: 
ll 

The hexadecimal number, ll, in base 10, is 17. 

Enter a hexadecimal number or enter x to terminate the program: 
x 
Thank you for using this program. 
BUILD SUCCESSFUL (total time: 36 seconds) 
+1

請修正您的代碼,以便編譯(刪除字符串內的換行符),並正確設置代碼的格式 - 在{}內部縮進代碼。 – Blorgbeard

回答

0

要打印,即使你有無效值的結果。您測試-1,打印錯誤消息,並且流程繼續到System.out.println,並且它永遠不會到達那裏。

更改此:

if (value == -1){ 
     System.out.println ("You must enter a hexadecimal number. Try again please."); 
     break; 
    } 
} 
System.out.println("The hexadecimal number, " + hexInput + ", in base 10, is " + answer + "."); 

要這樣:

if (value == -1){ 
     System.out.println ("You must enter a hexadecimal number. Try again please."); 
     break; 
    } else { 
     System.out.println("The hexadecimal number, " + hexInput + ", in base 10, is " + answer + "."); 
    } 
} 

和尖端:不使用查找表六角轉換爲十進制。您可以使用正則表達式來刪除不想要的字符,並parseInt函數直接轉換:

int intValue = Integer.parseInt(hexString.replaceAll("[^a-fA-F0-9]", ""), 16); 

試驗X解析之前,就大功告成了。

+0

感謝您的幫助!但有些東西仍然是錯誤的。我進行了建議的更改,但是這是輸出:輸入一個十六進制數字或輸入x來終止程序: j 必須輸入一個十六進制數字。請再試一次。 輸入一個十六進制數或輸入x以終止程序:十六進制數,45,在基座10,是69 十六進制數,45,在基座10,是69 輸入一個十六進制數或輸入x終止程序: –

+0

您可能仍然有'System.out.println(「十六進制數字...」兩次...刪除最後一個。 – ThoriumBR

相關問題