Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用循环进行乘法的回答不正确_Java - Fatal编程技术网

在java中使用循环进行乘法的回答不正确

在java中使用循环进行乘法的回答不正确,java,Java,我尝试分别使用for、while和do-while循环来计算5*10*15*…50。当我运行代码时,它会显示不正确的答案,该答案等于0。我在代码中找不到问题。有人能帮我看看吗??非常感谢~~下面是我的代码: public class Main { public static void main(String[] args) { ForMethod show1 = new ForMethod(); show1.computeForLoop();

我尝试分别使用for、while和do-while循环来计算5*10*15*…50。当我运行代码时,它会显示不正确的答案,该答案等于0。我在代码中找不到问题。有人能帮我看看吗??非常感谢~~下面是我的代码:

public class Main {

    public static void main(String[] args) {


        ForMethod show1 = new ForMethod();  
        show1.computeForLoop();
        System.out.println();

        DWMethod show2 = new DWMethod();
        show2.computeDWLoop();
        System.out.println();

        WhileMethod show3 = new WhileMethod();
        show3.computeWhileLoop();

    }

}


// For Loop
public class ForMethod {

    long mul;

    public void computeForLoop(){

        System.out.println("Compute using For Loop : ");
        for(int x = 1; x <= 10 ; x++){

            if ( x < 10){

                System.out.print(x*5 + " x ");

            } else { System.out.print(x*5);}

            mul *= x*5;
        }

        System.out.println("\nThe Product of Number = " + mul);

    }
}


// While Loop Method
public class WhileMethod {

    int x = 1 ;
    long mul;

    public void computeWhileLoop(){

                System.out.println("Compute using While Loop : ");

        while(x < 10){

            System.out.print(x*5 + " x ");
            x++;


            if (x == 10){

                System.out.print(x*5);
            }

            mul *= (x*5);
        }

        System.out.println();
        System.out.println("The Product of Number = " + mul);
    }


}


// Do-While Loop Method
public class DWMethod {

    int x = 1;
    long mul;

    public void computeDWLoop(){

            System.out.println("Compute using Do-While Loop : ");

        do{

            if (x < 10 ){

                System.out.print(x*5 + " x ");

            } else

                if (x == 10){

                    System.out.print(x*5);
                }

            mul *= x;
            x++;

        } while (x <= 10);


        System.out.println();
        System.out.println("The Product of Number = " + mul);

    }
}
公共类主{
公共静态void main(字符串[]args){
FormMethod show1=新的FormMethod();
show1.computeForLoop();
System.out.println();
DWMethod show2=新的DWMethod();
show2.computeDWLoop();
System.out.println();
WhileMethod show3=新WhileMethod();
show3.computeWhileLoop();
}
}
//For循环
公共类方法{
长骡;
public void computeForLoop(){
System.out.println(“使用For循环进行计算:”;

对于(int x=1;x你应该给值1初始化
mul
你应该给值1初始化
mul
问题是你的
mul
字段被初始化为零。这意味着你总是乘以零。

问题是你的
mul
字段被初始化为零s表示总是乘以零。

在我看来,最好的方法是将mul初始化为“5”,然后开始循环2!

在我看来,最好的方法是将mul初始化为“5”然后开始循环2!

你应该初始化
mul
。如果你不初始化
mul
,计算机给它值0。

你应该初始化
mul
。如果你不初始化
mul
,计算机给它值0。

你的问题可能是所有这些数字的乘积太小arge并溢出您的
long
变量。将
mul
初始化为1,默认为0。您需要使用
biginger
而不是
long
。感谢所有帮助。在我更改代码后,它会显示for循环和do while循环的正确答案,它们等于3.54375 x 10^13。但是while循环的答案是正确的显示与其他人不同的答案,即7.0875 x 10^12…我如何解决此问题?您的问题可能是所有这些数字的乘积太大,溢出了
long
变量。将
mul
初始化为1,默认为0。您需要使用
biginger
而不是
long
。谢谢对于所有帮助..在我更改代码后,它显示了for循环和do while循环的正确答案,它们等于3.54375 x 10^13..但是while循环的答案与其他答案不同,它们是7.0875 x 10^12…我如何解决这个问题??