如何知道什么是逻辑错误?

时间:2016-05-25 11:07:05

标签: java arrays operators

制作一个程序,找到以空格分隔的行中给出的单个整数的立方体。

这是该计划。它有一些逻辑错误。它适用于任何输入都是字符的情况。

class Cube{

    void process(){
        int i=0,sum=0,j;
        int cube;
        int ar[]=new int[100];
        Scanner scan=new Scanner(System.in);
        while(scan.hasNext()){
            if(scan.hasNextInt()){
               ar[i]=scan.nextInt();
               i++;
            }
            else{
                scan.next();
            }
        }     
        for(j=0;j<=i;j++){
            cube=ar[j]*ar[j]*ar[j];
            sum=sum+cube;
        }
        System.out.print(sum);
    }  
    public static void main(String args[]){
        Cube obj=new Cube();
        obj.process();
    }
}

通过在线编译器输入:1 2 3 4 5或任何长度

1 个答案:

答案 0 :(得分:0)

如果输入单行为example.com/login/,您可以使用它将输入作为字符串,然后将其解析为String数组。

1 2 3 4 5