【发布时间】:2024-01-12 22:36:01
【问题描述】:
import java.util.Scanner;
public class Problem5 {
public static void main(String args[]) {
System.out.println("Base Calculator");
System.out.println("Input a number greater than 0 and less than 10 to use as your base");
System.out.println("------------------------------------------------------------------");
System.out.println("Base: ");
//menu
Scanner input = new Scanner(System.in);
int base = 0;
int basenumber[];
basenumber = new int[1000001];
base = input.nextInt();
int remainder, tens;
if (base <= 0 || base >=10) { //only working with this parameter
System.out.println("This calculator does not work with bases that are less than 1 or bases that are greater than 9. Please re-run the program.");
return;
}
else {
System.out.print("(This means a base " + base + " calculator. Hence, the allowed values corresponding to base 10 values of 0 through 102 would be: ");
for(int i = 0; i<=101; i++) { //a sample of what i am getting when a base is inputed;notice how when the number gets to 40 base 4, it does not go automatically to 100
if(i < base){
basenumber[i] = i;
System.out.print(basenumber[i] + ", ");
}
if (i >= base && i < 101){
tens = (i / base) * 10;
remainder = i % base;
basenumber[i] = tens + remainder;
System.out.print(basenumber[i] + ", ");
}
if (i >= base && i == 101){
tens = (i / base) * 10;
remainder = i % base;
basenumber[i] = tens + remainder;
System.out.println(basenumber[i] + ")");
System.out.println("\n");
}
}
//How could I change the above code to make it such that when four 4's show up in the base 4 fours spot (base 10 tens spot), it sees that as one 16 and moves it to the base 4 sixteens spot (base 10 hundreds spot)?
}
}
}
输出
基础计算器 输入一个大于 0 且小于 10 的数字作为基数
基础: 4 (这意味着一个以 4 为底的计算器。因此,对应于 0 到 102 的以 10 为底的值的允许值将是:0、1、2、3、10、11、12、13、20、21、22、23、30 , 31, 32, 33, 40, 41, 42, 43, 50, 51, 52, 53, 60, 61, 62, 63, 70, 71, 72, 73, 80, 81, 82, 83, 90, 91 , 92, 93, 100, 101, 102, 103, 110, 111, 112, 113, 120, 121, 122, 123, 130, 131, 132, 133, 140, 141, 142, 143, 150, 151, 152 , 153, 160, 161, 162, 163, 170, 171, 172, 173, 180, 181, 182, 183, 190, 191, 192, 193, 200, 201, 202, 203, 210, 211, 212, 213 , 220, 221, 222, 223, 230, 231, 232, 233, 240, 241, 242, 243, 250, 251
预期的输出是: 0,1,2,3,10,......,33,100,101,102,103,110(基数 4)
如何修改上面的代码以产生预期的输出?
【问题讨论】:
-
你的问题是什么?错误?意外输出?
-
你刚刚给了我们代码。说说有什么问题?输出有什么问题?
-
您的预期输出在 33 之后出错 - 这不是碱基 4 序列的预期输出。请记住,基数 X 表示没有代表 X 的单个字符。X=10。以 2 为底和以 102 为底为真。
-
对不起,我没有说清楚,但代码中已对问题进行了注释:如何更改上面的代码以使其在四个 4 出现在 base 4fours 点时(base 10 个十位点),它把它看成一个 16 并将其移动到基数 4 的十六位点(基数 10 百位点)?
标签: java int calculator base