1.遍历100以内偶数
class forTest1{
public static void main(String []args){
for(int i = 0;i <= 100;i++){
if(i % 2 == 0 || i ==0){
System.out.println(i);
}
}
}
}
2.遍历100以内偶数,并求其总和
class forTest1{
public static void main(String []args){
int sum = 0;
for(int i = 0;i <= 100;i++){
if(i % 2 == 0 || i ==0){
System.out.println(i);
sum += i;
}
}
System.out.println("100以内偶数总和为"+sum);
}
}
3.从1循环到150,并在每行打印一个值,另外在每个3的倍数行上打印出“foo”,
在每个5的倍数行上打印“biz”,在每个7的倍数行上打印输出“baz”
class pra4
{
public static void main(String []args){
for(int i = 1; i <= 150; i++){
if(i % 3==0){
System.out.println(i +" foo");
}else if(i % 5 == 0){
System.out.println(i +" biz");
}else if(i % 7 == 0){
System.out.println(i +" baz");
}else{
System.out.println(i);
}
}
}
}
原文:https://www.cnblogs.com/wxchenrush/p/14637709.html