【自动化__持续集成】___java___控制结构

Java条件与循环语句示例
本文提供了Java中if、switch、for、while及do-while等控制语句的具体应用实例,通过不同场景展示了如何根据输入进行条件判断以及循环处理。

 

一、if和switch语句代码如下

package com.wujianbo;
import java.util.Scanner;

public class Demo06 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo06 demo06= new Demo06();
		//demo06.branch();
		demo06.execif();
		

	}
    public void execif(){
		Scanner sc= new Scanner(System.in);
		System.out.println("请输入年龄:");
		int age= sc.nextInt();
		if (age <= 20) {
			System.out.println("您好,骚年!");
		}
		else if (age <= 40) {
			System.out.println("您好,大哥!");
		}
		else if (age <= 60) {
			System.out.println("您好,大叔!");
		}
		else {
			System.out.println("您好,大爷!");
		}
		if (age == 45) {
			System.out.println("您好,强哥!");	
		}
		
	}

}

 

package com.wujianbo;
import java.util.Date;
import java.util.Scanner;

public class Demo06 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo06 demo06= new Demo06();
		//demo06.branch();
		//demo06.execif();
		demo06.execcase();
		
	}
public void execcase(){
		String x="";
		int d= new Date().getDate();
		System.out.println(d+"a");
		switch (d) {
			case 0:
				x = "今天是星期一";
				break;
			case 1:
				x = "今天是星期一";
				break;
			case 2:
				x = "今天是星期一";
				break;
			case 3:
				x = "今天是星期一";
				break;
			case 4:
				x = "今天是星期一";
				break;
			case 5:
				x = "今天是星期一";
				break;
			case 6:
				x = "今天是星期一";
				break;
			case 25:
				x = "今天是星期七";
				break;
		}
		System.out.println(x);
	}
}

 

 

二、for、while、do while的代码如下

 

public void exeFor(){
	int result= 0;
	//for (int i=1; i<=100; i++) {
	//	result += i;
	//}
	int i= 1;
	for (; i<=100 ;) {
		result += i;
		i++;
	}
	System.out.println(result);
}

 

public void exeWhile() {
	int i= 1;
	int result= 0;
	while (i<101) {
		result += i;
		i++;
	}
	System.out.println(result);
}

 

 

public void exeDowhile() {
	int i= 1;
	int result= 0;
	do {
		result += i;
		i++;
	}while (i<101);
	System.out.println(result);
 }

 

转载于:https://www.cnblogs.com/wujianbo123/p/7487217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值