2017-03-16 4 views
0

ここにコードの混乱があります。私は、3より大きい正の整数を入力するプログラムを書く必要があります。整数が3より大きいかどうかを検証します。次に、入力された数以下の正の整数の可能なすべてのペアを出力します。ループを使用して入力要素をプリントアウトするにはどうすればよいですか?

ex. If 24 is the input. 
It would print: 
4 = 2 x 2 
6 = 2 x 3 
8 = 2 x 4 
10 = 2 x 5 
12 = 2 x 6 
14 = 2 x 7 
16 = 2 x 8.... 
9 = 3 x 3 
12 = 3 x 4.. 
24 = 3 x 8... 
all the way to 
24 = 4 x 6 

import java.util.Scanner; 

public class Factors { 
    public static void main(String[] args) { 
     // Define Variables 
     Scanner input = new Scanner(System.in); 
     int i = 0; 
     int j = 0; 
     int k = 2; 
     int product = 0; 
     // Ask for input/loop 
     while (i < 3) { 
      System.out.println("Please enter an integer greater than 3"); 
      i = input.nextInt(); 
     } 
     while (product < i) { 
      if (product == i) { j++; k = 2; 
     for (j = 2; product < i; k++) { 
       product = j * k; 
       System.out.println(product + " = " + j + " x " + k); 
       if (product == i) { j++; k = 2; 
       } 
      } 
      } 
     } 
    } 
} 
+0

SOへようこそ。あなたの問題はまさに...? –

+0

あなたはinputValueが必要で、 LoneWanderer

+0

申し訳ありませんが、私はもっと明確にできたと思います。上記のコードでは、すべての要素の出力を2で2 x = 4 ...得ることができます。しかし、私は3、4などをすることができるように残りの部分をどうやって行うのか分かりません。次の場所や何をするべきかによってむしろ失われています。 2 x 3 = 6、3 x 2 = 6などを繰り返すことなく、すべての可能な要素を印刷する必要があります。 –

答えて

0
public class Factors { 
public static void main(String[] args) { 
    // Define Variables 
    Scanner input = new Scanner(System.in); 
    int i = 0; 
    int product = 0; 
    // Ask for input/loop 
    while (i < 3) { 
     System.out.println("Please enter an integer greater than 3"); 
     i = input.nextInt(); 
    } 
    for (int j = 2; j < i/2; j++) { 
     for (int k = 2; k < i/2; k++) { 
      if (j <= k && j * k <= i) 
       System.out.println(j * k + " = " + j + "*" + k); 
     } 
    } 
    // while (product < i) { 
    // if (product == i) { 
    // j++; 
    // k = 2; 
    // for (j = 2; product < i; k++) { 
    // product = j * k; 
    // System.out.println(product + " = " + j + " x " + k); 
    // if (product == i) { 
    // j++; 
    // k = 2; 
    // } 
    // } 
    // } 
    // } 
} 

}

+0

これは完璧で、私はあなたの人生を借りています。これはとてもシンプルですが、入れ子にされたループは私にとってかなり混乱しています。どうもありがとうございます。 –

関連する問題