问题
I am having an issue where when my program prints (base)^0=
, it doesn't print the answer (1)
(I shorend down the out put examples as I'm only having an issue with the first line of the output)
expected output:
2^0=1 2^1=2 2^2=2*2=4 2^3=2*2*2=8 2^4=2*2*2*2=16
actual output:
> 2^0=
> 2^1=2=2
> 2^2=2*2=4
> 2^3=2*2*2=8
> 2^4=2*2*2*2=16
code:
else if(option == 2){
base = Input.nextInt();
for(int i = 0; i<10; i+=1){
System.out.print(base+"^"+i+"=");
for(int j = 0; j < i; j+=1){
if(j != i -1){
System.out.print(base+"*");
}else{
System.out.format(base+"="+"%.0f",Math.pow(base,i));
}
}
System.out.println("");
}
}
回答1:
The first round when i = 0
, you don't enter the inner for loop as the condition to enter is j < i
, that is 0 < 0
=> false
来源:https://stackoverflow.com/questions/62050129/how-do-i-make-my-answer-print-correctly-in-java