2015-04-04 117 views
2

我正在编写一个程序,要求用户输入总共十个不同时间的两个整数。第一个数字是第二个数字的倍数

接下来程序需要评估第一个整数是否是第二个整数的倍数。

如果第一个是第二个的倍数,那么程序应该输出“true”,如果不是那么它应该打印出“false”。

这里是我的代码:

 public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     int counter = 0; // Initializes the counter 

     System.out.printf("Enter first integer: "); // asks the user for the first integer 
     int number1 = input.nextInt(); // stores users input for the first integer 

     System.out.printf("Enter second integer: "); // asks the user for the second integer 
     int number2 = input.nextInt(); // stores the users input for the second integer 

     while (counter <= 10) // starts a loop that goes to 10 
     { 
      if (number1 & number2 == 0) // checks to see if number1 is a multiple of number2 
       System.out.print("true"); // if so then print out "true" 
      else 
       System.out.print("false"); // otherwise print out "false" 
     } 

    } // end class 

某处沿着我的代码是打破了线。有没有人能够帮助,或者至少让我指向正确的方向?

+2

使用'%'而不是'&'来检查number1是否是number2的倍数。 – 2015-04-04 20:54:18

回答

3

您需要阅读两个输入10次。并测试number1number2的倍数。类似于

public static void main(String str[]) throws IOException { 
    Scanner input = new Scanner(System.in); 

    for (int counter = 0; counter < 10; counter++) { 
     System.out.printf("Enter first integer for counter %d: ", counter); 
     int number1 = input.nextInt(); 

     System.out.printf("Enter second integer for counter %d: ", counter); 
     int number2 = input.nextInt(); 

     // Since you want to print true if number1 is a multiple of number2. 
     System.out.println(number1 % number2 == 0); 
    } 
} 
1

&是按位逻辑与功能。我很确定这不会做你想做的事情。似乎你想要MODULO运营商,%

E.g.使用number1%number2。而不是NUMBER1 & NUMBER2

 while (counter <= 10) // starts a loop that goes to 10 
    { 
     if (number1 % number2 == 0) // checks to see if number1 is a multiple of number2 
     System.out.print("true"); // if so then print out "true" 

     else 
     System.out.print("false"); // otherwise print out "false" 
    } 
+0

这是对的;然而,现在我需要程序循环十次并打印出“真”或“假”。 – justLearning 2015-04-04 20:56:41

相关问题