看板 java 關於我們 聯絡資訊
package test; import java.util.Scanner; import static java.lang.System.out; public class TestClass { public static int numOfFactors(int n, int s, int e) { int factors = 0; for(int i = s; i<=e; i++) { while(n%s==0) { factors = factors + 1; } } return factors; } public static void main(String[] args) { TestClass test = new TestClass(); System.out.println(test.numOfFactors(100,10,30)); } } 這是我的程式碼,想在s,e區間中找到n的因數個數。但在使用Eclipse編譯的過程中, run完都沒有結果產生(印出簡單的Hello World是成功的)。自己覺得是使用method的方法有誤,但在找完網路上的資料試過, 都不太清楚是什麼地方有誤,想請問大大們@@ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.81.18 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1523452830.A.7AD.html
x12345x: while(n%i==0) { 04/11 21:40
x12345x: 耶不對 怎麼有兩個迴圈 04/11 21:44
x12345x: while那邊改成if(n%i==0) 04/11 21:47
啊啊!竟然沒有注意到這個!為什麼用while不行呢? ※ 編輯: rexrex0808 (123.193.81.18), 04/11/2018 21:55:36
x12345x: 因為你變數n,s在while迴圈裏面沒有變 就無窮迴圈了 04/11 21:59
謝謝你!我懂了 ※ 編輯: rexrex0808 (123.193.81.18), 04/11/2018 22:22:01