2016-07-25 11 views
0

私はJavaコードをプログラムして、2と5の主要な要素である出力番号を生成しようとしています。Javaの "For"ループ出力が繰り返されます

たとえば、入力がの場合、出力はになります。 しかし、出力を印刷すると、結果は2 5 4 5 8 5になります。

私が間違っている箇所についてアドバイスをしてください。 あなたが@AmedeeVanGasseのコメントを確認した後、あなたは括弧を修正する必要があなたに

import java.util.Scanner; 

class twofive { 
    public static void main(String [] args) { 
    Scanner sc = new Scanner(System.in); 
    System.out.print("Enter n:"); 
    int n = sc.nextInt();              
    double num = 0; 
    double num2 = 0; 
    for (int i = 1; (((Math.pow(2,i))<= n) || ((Math.pow(5,i)) <=n) || (((Math.pow(2,i))<= n) && ((Math.pow(5,i)) <=n))) ; i++) { 
     if ((Math.pow(2,i)) <= n)             
     num = (Math.pow(2,i)); 
     int convert = (int) num;{ 
     System.out.print(convert + " "); 
     } 
     if ((Math.pow(5,i)) <= n) 
     num2 = (Math.pow(5,i)); 
     int convert2 = (int) num2; 
     {System.out.print(convert2 + " "); 
    } 
    } 
} 
} 
+3

* 8の*整数*係数は** 1 2 4 8 **です。 8の*素因数分解は* 2 2 2 *または* 2^3 *です。あなたのコードは何か他のことをします:与えられた整数以下の2と5のすべての*複合*(と2と5の素数)を生成します。 –

答えて

0

ありがとうございます。

public static void main(String [] args) { 
    Scanner sc = new Scanner(System.in); 
    System.out.print("Enter n:"); 
    int n = sc.nextInt();              
    double num = 0; 
    double num2 = 0; 
    for (int i = 1; Math.pow(2,i))<= n) || ((Math.pow(5,i)) <=n) || (((Math.pow(2,i))<= n) && ((Math.pow(5,i)) <=n))) ; i++) { 
     if ((Math.pow(2,i)) <= n) {             
     num = (Math.pow(2,i)); 
     int convert = (int) num; 
     System.out.print(convert + " "); 
     } 
     if ((Math.pow(5,i)) <= n) { 
      num2 = (Math.pow(5,i)); 
      int convert2 = (int) num2; 
      System.out.print(convert2 + " "); 
     } 
    } 
} 

また、あなたのforループとif文のロジックを確認する必要があります。彼らは不要な冗長に満ちています。

関連する問題