2016-01-11 27 views
-1

在本實驗中一個很酷的數量是任何數量,當由3,4,5 & 6分將留下的剩餘1.我已經做了靜態布爾IsCoolNumber和我做了一個私人INT B中使用一個櫃檯,但我絕對不知道我會把例如一個b ++。任何幫助將不勝感激。 在此先感謝!酷數量計數器

import static java.lang.System.*; 

public class CoolNumbers 
{ 
    private int b=0; 

    public static boolean isCoolNumber(int num) 
    { 
     int x; 
     x = 6; 
     for(x = 6; x<num; x++) 
     { 
     if ((x%3==1) && (x%4==1) && (x%5 ==1) && (x%6 == 1)) 

      return true; 

     } 
     return false; 

    } 

    public static int countCoolNumbers(int stop) 
    { 


     //add counter 


    } 

    public static void main(String[] args) 
    { 
     System.out.println(CoolNumbers.countCoolNumbers(250) + " cool numbers between 6 - " + 250); 
     //add more test cases 
    } 
} 
+0

250你是什麼算法?你如何解決這個問題? – Praveen

回答

4

isCoolNumber你不需要循環,只需要一個語句就可以了。

public static boolean isCoolNumber(int x) { 
    return (x % 3 == 1) && (x % 4 == 1) && (x % 5 == 1) && (x % 6 == 1); 
} 

要計算在當前空countCoolNumbers(int stop)法「酷號」加上一個簡單的循環。

for (int i = start; i < stop; i++) { 
    if (isCoolNumber(i)) { 
     count++; 
    } 
} 
0

下面寫你的方法countCoolNumbers和嘗試...

public static int countCoolNumbers(int stop){ 
    boolean check=isCoolNumber(stop); 
     int num=0; 
     if(check==true){ 
     num=stop; 
     } 
     num=0; 
    return num; 

`}

public static void main(String[] args) { 
System.out.println(CoolNumbers.countCoolNumbers(250) + " cool numbers between 6 - " + 250); 
//add more test cases 

}

然後輸出將是:

0很酷的數字6之間 -