2015-10-15 69 views
0

我試圖讓代碼找到用戶輸入的因素,給用戶輸入的素因式分解,並給出lcm和gcm。它應該很簡單。它是編程類的簡介,但它對我來說太快了。我花了數小時閱讀和學習,並試圖讓這個代碼工作。請幫忙。Java因式分解失敗 - 爲什麼它循環?

import java.util.Scanner; 


public class PattersonFactorization { 


    public static void main(String[] args) 
    { 
     //create scanner to obtain input from command window 
     Scanner input = new Scanner(System.in); 

     //initialization phase 
     int input1 = 0; //initialize first input from user 
     int input2 = 0; //initialize second input from user 


     //prompt twice for two inputs from user 
     //until they enter a positive value for each input 

     do 
     { 
      System.out.println("Please provide your first positive number: "); 
      input1 = input.nextInt(); 
     }//end do for input1 
     while(input1 <= 0); 

     do 
     { 
      System.out.println("Please provide your next positive number: "); 
      input2 = input.nextInt(); 
     }//end do for input2 
     while(input2 <= 0); 

     // call methods for factorization calculations 
     calculateFactors(input1); 
     calculateFactors(input2); 
     calculatePrime(input1); 
     calculatePrime(input2); 
     calculateLCM(input1, input2); 
     calculateGCF(input1, input2); 


    }//end of main 

    public static void calculateFactors(int input) 
    { 

     for(int countFactor = 1; countFactor < input; countFactor++) 
     { 
      if(countFactor % input == 0); 
      System.out.println(countFactor); 
     } 

    }//end of calculateFactors 

    public static void calculatePrime(int input) 
    { 
     for(int countPrime = 1; countPrime < input; countPrime++) 
     { 
      if(countPrime % input == 0); 
      System.out.println(countPrime); 
      input = input/countPrime; 
      countPrime--; 
     } 
    }//end of calculatePrime 

    public static void calculateLCM(int input1, int input2) 
    { 
     for(int factorNum = 1; input1 % factorNum != 0 && input2 % factorNum != 0; factorNum++) 
     System.out.println(factorNum); 
    }//end of calculateLCM 

    public static void calculateGCF(int input1, int input2) 
    { 
     for(int factorNum = input1; input1 % factorNum != 0 && input2 % factorNum != 0; factorNum--) 
     System.out.println(factorNum); 
    } 

}//end of class 

`

+0

是,「分解計算的調用方法」是一條評論。我修復了它並試圖再次運行它。當我運行程序時,它只是循環「1」。 –

+0

我在程序的僞代碼設計上獲得了100%的回報,並且我將它記錄到了這封信中,但這根本不起作用。 –

+0

只需注意一下(雖然這不能解決您的問題):您應該在完成掃描時關閉掃描儀(例如,在'main'方法的末尾放置'input.close();')。 – neuronaut

回答

2

我相信這個問題是在這裏:

 for(int countPrime = 1; countPrime < input; countPrime++) 
     { 
      if(countPrime % input == 0); 
      System.out.println(countPrime); 
      input = input/countPrime; 
      countPrime--; 
     } 

在循環遞減countPrime增量它。結果它永遠不會改變它的價值。

+0

謝謝。它停止循環1! 〜擁抱〜我欣賞你:) –

+0

不客氣。請繼續學習。編程可以很有趣。 –

+0

我喜歡VBA和SQL,但這個Java的東西完全是它自己的動物。我甚至都不懂這個術語,更不用說這些錯綜複雜的交織概念和隱祕句法的應用了。我覺得我正在看一個俱樂部會所的窗戶,裏面充滿了在Alienese中喋喋不休的偷窺...我只想轉動尾巴和螺栓!但我必須先通過這個學期...呃! –

0

有你的代碼的幾個問題:

if(countFactor % input == 0); 
System.out.println(countFactor); 

你的代碼在這裏都會被執行System.out.println(countFactor);不管你的條件表達式的值。問題是if(countFactor % input == 0); - 您的if末尾有分號。

您既可以使用no braces in this case

if(countFactor % input == 0) 
    System.out.println(countFactor); 

但它幾乎總是更加清晰使用括號既明確的編碼器和編譯器顯示範圍:

if(countFactor % input == 0) { 
    System.out.println(countFactor); 
} 

你無限循環來自您的for循環中遞增和遞減countPrime

for(int countPrime = 1; countPrime < input; countPrime++) 
{ 
    if(countPrime % input == 0); 
    System.out.println(countPrime); 
    input = input/countPrime; 
    countPrime--; 
} 

countPrime將在循環結束時遞減至0,然後在下一次循環迭代時遞增至1input = input/countPrime;將始終爲input = input/1,這也是您的終止表達從不爲true的另一個原因。

+0

非常感謝你的幫助mkobit。 –