我編寫了一個將十六進制數轉換爲十進制的程序。大多數情況下它是有效的,但是,當我輸入非十六進制字符串時,它會正確計算該值並將其顯示爲非十六進制數字,但它也會跳轉到下一行並輸出它。我認爲這是這是有問題的代碼部分:將十六進制轉換爲十進制的方法 - 輸入非十六進制數時程序不正確
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)
請修正您的代碼,以便編譯(刪除字符串內的換行符),並正確設置代碼的格式 - 在{}內部縮進代碼。 – Blorgbeard