2016-02-13 50 views
0

我工作的问题是,如何获取代码继续提示下一个号码的用户?

“写入是确定,对于一对整数的倍数,第二整数是否 是第一的倍数。所述的方法,将需要2 整数参数并返回true,如果第二个是首先是 的倍数,否则返回false [提示:使用余数运算符] 将此方法合并到输入一组整数对(一次一对)的应用程序中,并确定每对中的第二个值是否是第一个的倍数。“

现在,我写了一个程序,有点有用,但我似乎不能让它继续前进。当按下0时它应该停止,但它不会超过第一个整数。

当我改变while(first!= 0)时,我确实让它运行了一个循环;到while(first == 0);.

我只是留下了一些东西或有完全错误的东西吗?任何信息都会有帮助。

这里是我的代码

import java.util.Scanner; 

public class Multiples 
{ 
    public static void main(String[] args) 
    { 
     System.out.println("Len Rogers - Programming Assignment 4\n"); 

     Scanner input = new Scanner(System.in); 

     int first; // first number 
     int second; // second number 

     System.out.print("Enter first number (0 to exit): "); 
     first = input.nextInt(); 

     // set 0 as the sentinel value, since we cannot divide by 0 
     while (first != 0); 
     { 

      System.out.print("Enter second number: "); 
      second = input.nextInt(); 

      if (ismultiple(first, second)) 
       System.out.printf("true\n\n", second, first); 
      else 
       System.out.printf("false\n\n" , second, first); 

      System.out.print("Enter first number (0 to exit): "); 
      first = input.nextInt(); 
     } // end while loop 
    } // end main 

    // determin if the firest integer is a multiple of the second 
    public static boolean ismultiple(int firstNumber, int secondNumber) 
    { 
     return secondNumber % firstNumber == 0; 
    } // end method multiple 
} // end class Multiples 
+0

这与NetBeans 8有什么特别的关系? –

+0

提示:在输入input.nextInt()或input.next()(或类似的)后,你应该总是有一个后面的调用'input.nextLine()',因为后者不消耗挂起的换行符进入控制台。 – Seelenvirtuose

+3

问题是分号(终结者)刚过while.Remove它,它会工作 –

回答

0

我觉得

do { 

} while(first != 0) 

是您正在寻找的,因为它会阻止您需要有独立的代码读取第一个输入回路

如果你使用readline,然后检查你的阅读可以被转换为int,否则退出你应该能够得到你想要的。