java
1public class PrimeFactorization { 2 public static void main(String[] args) throws IOException, NumberFormatException { 3 System.out.print("2以上の整数を入力してください----- "); 4 5 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 String str = br.readLine(); 7 br.close(); 8 9 int num = Integer.parseInt(str); 10 String result = new String(); 11 12 for (int i = 2; num > 1; i++) { 13 if (num % i == 0) { 14 if (result.length() > 0) { 15 result += " * "; 16 } 17 result += Integer.toString(i); 18 num = num / i; 19 i--; 20 } 21 } 22 if (!(result.contains("*"))) { 23 result = "その整数は素数です。"; 24 } 25 26 System.out.println(result); 27 } 28}
上記は入力された整数の素因数分解を求めて表示させるコードです。
このコードを実行すると
2以上の整数を入力してください----- 34
2 * 17
と表示されますが、
2以上の整数を入力してください----- 34
2
17
と表示させたいです。どこをどのようにいじれば治りますか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。