【问题标题】:Java Number Sequence Generator Not WorkingJava 数字序列生成器不工作
【发布时间】:2013-02-22 22:13:45
【问题描述】:

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

}

}

【问题讨论】:

    标签: java numbers sequence


    【解决方案1】:

    multdivipower 之类的声音都是 0,并且您永远不会更改它们——因此您正在执行您的 while 循环(因此您的 for 循环)是一个无限数次。

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

    【讨论】:

    • while循环应该改成if子句吧?
    • @FlorianMinges 我真的不知道 OP 的目的是什么——你不想打印序列吗?
    • 我的印象是 system.in 将用户输入分配给了这些变量?
    • System.out 只是将参数打印到控制台。
    • 再次感谢您的帮助
    【解决方案2】:

    您永远不会改变 multi、divi 或 power 的值。如果这些值永远不会改变,您如何期望时间结束?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多