Java 冷数计数器
对于这个实验室,冷数是任何除以3、4、5和6后剩下1的数。我已经完成了静态布尔值IsCoolNumber,并且我制作了一个私有int b用作计数器,但是我完全不知道我将把例如a b++放在哪里。任何帮助都将不胜感激。 提前谢谢Java 冷数计数器,java,Java,对于这个实验室,冷数是任何除以3、4、5和6后剩下1的数。我已经完成了静态布尔值IsCoolNumber,并且我制作了一个私有int b用作计数器,但是我完全不知道我将把例如a b++放在哪里。任何帮助都将不胜感激。 提前谢谢 import static java.lang.System.*; public class CoolNumbers { private int b=0; public static boolean isCoolNumber( int num )
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
}
}
导入静态java.lang.System.*;
公共类CoolNumber
{
私有int b=0;
公共静态布尔isCoolNumber(int num)
{
int x;
x=6;
对于isCoolNumber
中的(x=6;x),不需要循环,只需一条语句即可
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++;
}
}
for(int i=start;i
按以下方式编写您的方法countCoolNumbers
,然后尝试一下
public static int countCoolNumbers( int stop ){
boolean check=isCoolNumber(stop);
int num=0;
if(check==true){
num=stop;
}
num=0;
return num;
`}
}
然后输出为:
0个介于6-250之间的酷数字你的算法是什么?你如何解决这个问题?
public static void main( String[] args ) {
System.out.println( CoolNumbers.countCoolNumbers(250) + " cool numbers between 6 - " + 250);
//add more test cases