java里取余的问题

来源:百度知道 编辑:UC知道 时间:2024/06/01 21:09:10
求1到100之间有多少个能被3整数的数
提示:x%3==0就是能被3整除的数
以上是题目,我要如何输出?

java中求余数用“%”,之后输出满足条件的数值,如10%5,结果就是0;
public class PrimeNumber
{
public static void main(String[] args)
{
int i=2;
for(i=2;i<20;i++)
{
if(i%5==0)
System.out.print(i+"\t"); }
}
输出结果:5 10 15 。

最简单的
public static void main(String[] args) {
for (int i = 0; i < 101; i++) {
if (i % 3 == 0) {
System.out.println(i);
}

}
}

class ForTestDemo
{
public static void main(String[] args)
{
int count = 0;
for (int x = 1;x <= 100;x++ )
{
if (x % 3 == 0)
{
System.out.println("x="+x);
}
}
}
}