作者rexrex0808 (jiangjiang)
看板java
标题[问题] 新手 method使用问题
时间Wed Apr 11 21:20:28 2018
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://webptt.com/cn.aspx?n=bbs/java/M.1523452830.A.7AD.html
1F:→ x12345x: while(n%i==0) { 04/11 21:40
2F:推 x12345x: 耶不对 怎麽有两个回圈 04/11 21:44
3F:推 x12345x: while那边改成if(n%i==0) 04/11 21:47
啊啊!竟然没有注意到这个!为什麽用while不行呢?
※ 编辑: rexrex0808 (123.193.81.18), 04/11/2018 21:55:36
4F:推 x12345x: 因为你变数n,s在while回圈里面没有变 就无穷回圈了 04/11 21:59
谢谢你!我懂了
※ 编辑: rexrex0808 (123.193.81.18), 04/11/2018 22:22:01