0

我正在编写的这段代码有问题。代码的目的是为数字序列制定修饰符,然后给出该序列中的前 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;
            }
        }
    }

}

}
4

2 回答 2

0

听起来像mult,divipower都是 0,而且你永远不会改变它们——因此你正在执行你的while循环(因此你的for循环)无限次。

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

于 2013-02-22T22:15:28.227 回答
0

你永远不会改变多、分或权力的价值。如果这些价值观永远不会改变,你如何期望这段时间结束?

于 2013-02-22T22:17:05.173 回答