循环结构
for循环实现简单数字相加
public class CH03_4 {
public static void main(String[] args) {
int sum=0;
for(int i=1,j=100; i<j;i++,j--){
sum+=i+j;
}
System.out.println(sum);
}
}
嵌套for循环
public class CH03_5 {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int m=1;m<=5;m++){
System.out.print(" ");
}
for(int j=1;j<=5;j++){
System.out.print("*");
}
System.out.println();
}
}
}
do-while循环实现100以内数字相加减
public class CH03_2 {
public static void main(String[] args) {
int sum = 0;
int i = 0;
do{
sum += i;
i++;
}while(i<=100);
System.out.println("sum=" +sum);
}
}
嵌套结构
public class Homework {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("请输入密码:");
for(int i=0;i<3;i++){
int code = input.nextInt();
if(code==111111){
System.out.println("请输入金额:");
while(true){
int money =input.nextInt();
if(money<=1000&&money>0&&money%100==0){
System.out.println("您取了"+money+"元");
System.out.println("交易完成,请取卡");
return;
}else{
System.out.println("您输入的金额不合法,请重新输入:");
}
}
}else if(i<2){
System.out.println("密码错误,请重新输入");
}else{
System.out.println("谢谢您的使用,请取卡");
}
}
}
}