How do i make my answer print correctly in java

北战南征 提交于 2020-06-27 04:17:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!