for循环乘法程序输出不正确

时间:2014-12-11 21:06:03

标签: java

现在我遇到了循环的另一个问题,这次尝试使用与上面相同的代码制作一个简单的乘法程序。

    package loops;


import java.util.Scanner;

 public class forloops {

public static void main(String[] args) {

   Scanner input = new Scanner(System.in);

    System.out.println("Enter an integer you wish to multiply");

    int loopval;
    int end_val = 11;
    int times = input.nextInt();


    for (loopval = 0; loopval < end_val; loopval++) {
        times = times * loopval;
        System.out.println(loopval + " " +"Times" + " " + times + " " + "=");

}




    }
}

我的输出从1到10循环,但它似乎只是指数化。我只是想让它去1次5 = 5,2次5 = 10等等。

1 个答案:

答案 0 :(得分:0)

请尝试这样做;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter an integer you wish to multiply");

    int loopval;
    int end_val = 11;
    int value = input.nextInt();


    for (loopval = 0; loopval < end_val; loopval++) {
        int result = value * loopval;
        System.out.println(loopval + " " +"Times" + " " + value + " " + "=" + result);

}