如何使用方法int converter(int num)將基數10數轉換爲基數3數。將基數10轉換爲基數3數
import java.util.Scanner;
public class BaseConverter {
int answer;
int cvt = 0;
while (num >= 0) {
int i = num/3;
int j = num % 3;
String strj = Integer.toString(j);
String strcvt = Integer.toString(cvt);
strcvt = strj + strcvt;
num = i;
break;
}
answer = Integer.parseInt("strcvt");
return answer;
}
public static void main(String[] agrs) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = in.nextInt();
System.out.print(converter(number));
in.close();
}
這是編譯完成。但是,當我試圖運行它,並輸入一個數字時,它顯示 java.lang.NumberFormatException:對於輸入字符串:「strcvt」 我不知道如何解決它。我怎樣才能不使用字符串?
你是不是想在'parseInt'方法中使用變量'strcvt'?您的代碼當前通過字符串「strcvt」 – 2015-02-06 08:45:33
您對此行的期望是什麼? 'Integer.parseInt(「strcvt」);' – 2015-02-06 08:45:49
我在一團糟@^@ – AliceBobCatherineDough 2015-02-06 09:06:09