gpt4 book ai didi

java - 基本转换错误检查

转载 作者:行者123 更新时间:2023-11-30 06:19:30 25 4
gpt4 key购买 nike

当我输入正确的值时,我的代码中的所有内容都工作正常。当我输入不正确的值故意导致错误时,错误消息永远不会显示。控制台一片空白,我必须终止。我不确定为什么程序从未真正到达错误消息。相反,它似乎进入了无限循环。

import java.util.Scanner;

public class BaseConversion {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String theValue;
String newNum;
int initialBase;
int finalBase;

System.out.println("Please enter a value: ");
theValue = s.nextLine();
System.out.println("Please enter original base: ");
initialBase = s.nextInt();


System.out.println("Please enter new base: ");
finalBase = s.nextInt();
s.close();

newNum = convertInteger(theValue, initialBase, finalBase);


System.out.println("new number: " + newNum);
}

public static void isValidInteger(String num, int finalBase) {

if (finalBase < 2 || finalBase > 36) {
System.out.println("Error: Base must be greater than or equal to 2 & less than or equal to 36");
System.exit(1);
}


char chDigit;

for(int d = 0; d < num.length(); d++) {
chDigit = num.toUpperCase().charAt(d);
if(Character.isDigit(chDigit) && (chDigit - '0') >= finalBase) {
System.out.println("Error");
System.exit(1);
}
else if (Character.isLetter(chDigit) && (chDigit - 'A') + 10 >= finalBase) {
System.out.println("Error");
System.exit(1);
}
else if (!Character.isDigit(chDigit) && !Character.isLetter(chDigit)) {
System.out.println("Error");
System.exit(1);
}

}
}

public static String convertInteger(String theValue, int initialBase, int finalBase) {

double val = 0;
double decDigit = 0;
char chDigit;


// loop through each digit of the original number
int L = theValue.length();
for(int p = 0; p < L; p++) {

// get the digit character (0-9, A-Z)
chDigit = Character.toUpperCase(theValue.charAt(L-1-p));

// get the decimal value of our character
if(Character.isLetter(chDigit)) {
decDigit = chDigit - 'A' + 10;
}
else if (Character.isDigit(chDigit)) {
decDigit = chDigit - '0';
}
else {
System.out.println("Error");
System.exit(1);
}
// add value to total
val += decDigit * Math.pow(initialBase, p);
}

// determine number of digits in new base
int D = 1;

for( ; Math.pow(finalBase, D) <= val; D++) {}

// use char array to hold new digits
char[] newNum = new char[D];

double pwr;
for(int p = D-1; p >= 0; p--) {

// calculate the digit for this power of newBase
pwr = Math.pow(finalBase, p);

decDigit = Math.floor(val / pwr);

val -= decDigit*pwr;

// store the digit character

if(decDigit <= 9) {
newNum[D-1-p] = (char) ('0' + (int)decDigit);
}
else {
newNum[D-1-p] = (char) ('A' + (int)(decDigit - 10));
}
}
return new String(newNum);
}
}

最佳答案

我注意到的问题是您从未调用 isValidInteger 函数。因此,当finalBase例如为1时,即无效输入,convertInteger函数中存在无限循环:

// determine number of digits in new base
int D = 1;

for( ; Math.pow(finalBase, D) <= val; D++) {}

我不确定这是否是唯一的问题,但这就是我发现的问题。

关于java - 基本转换错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com