练习1:计算0到100之间的奇数和偶数的和:
package struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算0到100之间的奇数和偶数的和
int oddSum = 0;
int evenSum =0;
for (int i = 0;i<100;i++){
if (i%2!=0){//奇数
oddSum+=i;
}else{//偶数
evenSum+=i;
}
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+evenSum);
}
}
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个:
package struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
for (int i = 1; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+" ");
}
if (i%(5*3)==0){//换行
System.out.println();
//System.out.print("
");
}
}
//println 输出完会换行
///print 输出完不会换行
}
}
九九乘法表:
package struct;
public class ForDemo04 {
public static void main(String[] args) {
//1.我们先打印第一列
//2.我们把固定的1再用一个循环包起来
//3.去掉重复项,i<=j
//4.调整样式
for (int j = 0; j <= 9; j++) {
for (int i = 0; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+" ");
}
System.out.println();
}
}
}
package struct;
public class LabelDemo {
public static void main(String[] args) {
//打印101-150之间所有的质数
int count = 0;
outer:for (int i = 101; i < 150; i++) {
for (int j = 2;j<i/2;j++){
if (i % j == 0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
打印三角形:
package com.kuang.Struct;
public class TestDemo01 {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 0; i <= 5; i++) {
for (int j = 5;j>=i;j--){
System.out.print(" ");
}
for (int j = 1;j<=i;j++){
System.out.print("*");
}
for (int j = 1; j <i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
}