Java编号序列生成器不工作

时间:2013-02-22 22:13:46

标签: java numbers sequence

我写这个代码有问题。代码的目的是为数字序列制定修饰符,然后给出该序列中的前10个数字。但是,我的循环机制出现了一些问题,因为当它应该只执行10时,它会打印出无限量的值。我计划将除法和幂函数包含在代码中,但遇到了这个问题。

import java.util.Scanner;

public class PatternCreator {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out
            .println("Please enter the starting value of the number sequence.");
    double sequence = s.nextInt();
    System.out
            .println("Please enter the addition/subtraction modifier; e.g. 2,-2.");
    double addsub = s.nextInt();
    System.out
            .println("Please enter the multiplication modifier; 0 for none.");
    double mult = s.nextInt();
    System.out.println("Please enter the division modifier; 0 for none.");
    double divi = s.nextInt();
    System.out
            .println("Please enter the exponential modifier; 0 for none.");
    double power = s.nextInt();

    double addonly = sequence + addsub;

    while (mult == 0 && divi == 0 && power == 0) {
        for (int count1 = 1; count1 <= 10; count1++) {
            if (count1 == 1) {
                System.out.print(sequence + " ");
            } else {
                System.out.print(addonly + " ");
                addonly = addonly + addsub;
            }
        }
    }

    double multadd = sequence + addsub * mult;

    while (mult != 0 && divi == 0 && power == 0) {
        for (int count2 = 1; count2 <= 10; count2++) {
            if (count2 == 1) {
                System.out.print(sequence + " ");
            } else {
                System.out.print(multadd + " ");
                multadd += multadd;
            }
        }
    }

}

}

2 个答案:

答案 0 :(得分:0)

听起来multdivipower都是0,你永远不会改变它们 - 因此你正在执行你的while循环(因此你的for循环无数次。

为什么你有那个while循环呢?

答案 1 :(得分:0)

你永远不会改变多重,神圣或力量的价值。如果这些价值观从未改变,你如何期待结束呢?