2013-02-22 48 views
0

在编写这段代码时遇到问题。代码的目的是为数字序列制定修饰符,然后按照该序列给出前10个数字。然而,我的循环机制似乎有些问题,因为它应该只是在做10时打印出无限多的值。我打算在代码中包含除法和功能函数,但遇到了这个问题。Java数字序列发生器不工作

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; 
      } 
     } 
    } 

} 

} 

回答

0

听起来multdivipower都是0,而你永远不会改变them--所以你执行你的while环(因此你for循环)的无数次。

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

+0

while循环可能应该更改为if子句? – 2013-02-22 22:17:00

+0

@FlorianMinges我真的不知道OP是怎么回事 - 不想打印序列吗? – Colleen 2013-02-22 22:18:10

+0

我的印象是,system.in将用户输入分配给这些变量? – user2057847 2013-02-22 22:19:27

0

你永远不会改变多元,divi或权力的价值。如果这些值永远不会改变,你期望如何结束这一过程?