JAVA—Day4 of Self-Learning JAVA

JAVA—Day4 of Self-Learning JAVA

Record daily learning progress from now,to be a self-improved, confident and optimistic lion 🦁️

Due Today:

  • Selection Structure
    if、switch
  • Loop Structure
    for、while、do-while
  • Exercise

Selection Structure:if、switch

import java.util.Scanner;
/*
 * 流程语句:
 * 1、顺序语句;
 * 2、选择语句:if/switch;
 * 3、循环语句:for/while
 */

public class SelectionStructure_if {
	public static void main(String[] args) {
		//if
		int price = 19;
		int balance = 999;
		if(balance >= price) {
			System.out.println("buy it");
		}
		System.out.println("go home");
		
		//if-else
		int a = 10;
		int b =20;
		int c =15;
		if (a > b) {
			if(a > c) {
				System.out.println("max is : " + a);
			}
			else
				System.out.println("max is : " + c);	
		}
		else {
			if( b < c) {
				System.out.println("max is : " + c);
			}
			else
				System.out.println("max is : " + b);
		}
		 
		//if-else if-else
		Scanner input = new Scanner(System.in);
		System.out.println("pleasse input an integer: ");
		int star = input.nextInt();
		if(star > 0 && star < 10) {
			System.out.println("sulver");
		}else if (star > 9 && star < 20) {
			System.out.println("gold");
		}else if (star > 19 && star <30 ) {
			System.out.println("platina");
		}else {
			System.out.println("diamaond");
		}	
	}
}
import java.util.Scanner;
/*
 * swithch语句:
 * 1、switch:可以使用byte、short、char、int、String、枚举
 * 2、case后面的值是常量
 * 3、default可以省略,且位置没关系
 * 4、没有default,会出现穿透效果
 * 5、结束的标志:遇到break;执行到代码最后一行
 */

public class SelectionStructure_switch {
	public static void main(String [] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("please rnter an integer: ");
		int week = input.nextInt();
		switch(week) {
		case 1:
			System.out.println("Monday");
			break;
		case 2:
			System.out.println("Thursday");
			break;
		case 3:
			System.out.println("Wensday");
			break;
		case 4:
			System.out.println("Tuesday");
			break;
		case 5:
			System.out.println("Friday");
			break;
		default:
			System.out.println("Weekend");
			break;
		}
	}
}

Loop Structure:for、while、do-while

/*
 * for(初始值;判断条件;控制语句){
 * 循环体;
 * }
 * 水仙花定义:abc = a^3 + b^3 + c^3
 */

public class LoopStructure_for {
	public static void main(String [] args) {
		//打印水仙花
		int count = 0;
		for(int i = 100; i < 1000; i++) {
			int ge = i % 10;
			int shi = i / 10 % 10;
			int bai =  i / 100;
			if(i == Math.pow(ge,3) + shi*shi*shi+ bai*bai*bai){
				System.out.println(i);
				count++;
			}		
		}
		System.out.println("-------------------------");
		System.out.println(count);
	}
}
/*
 * while格式:
 * 初始值
 * while(判断条件){
 * 循环体;
 * 控制条件;
 * }
 */

public class LoopStructure_while {
	public static void main(String [] args) {
		int i = 0;
		while(i < 10) {
			System.out.println("hello world");
			i++;
		}		
	}
}
/*
 * do-whilej a格式:
 * 初始值;
 * do{
 * 循环体;
 * 控制条件;
 * }while(判断条件);
 * 
 * for while do-while
 * 1、三者可以等价循环,选择顺序for、while、do-while
 * 2、do-while至少执行一次
 * 3、for循环的初始值在循环外无法使用
 */

public class LoopStructure_dowhile {
	public static void main(String [] args) {
		int i = 1;
		do {
			System.out.println("hello world");
			i++;
		}while(i < 10);
	}
}

Exercise:

/*
 * return:方法中
 * break:结束循环
 * continue:跳过循环
 * 
 * 标签格式:
 * 名称:循环
 */

public class Jump_break_continue_return {
	public static void main(String [] args) {
		outer:for(int i = 1;i <= 5; i++) {
			inner:for(int j = 1 ; j <=5; j++) {
				 if(j == 3) 
					continue outer;                      // continue、break
				 System.out.print("*");
			}
			System.out.println("");
		}		
	}
}
//99乘法表

public class Exercise2_for {
	public static void main(String[] args) {
		for(int i = 1 ; i <= 9;i++) {
			for(int j = 1; j <= i; j++ ) {
				System.out.print(j + "*" + i + "="  + i*j + 't');
			}
			System.out.println("");
		}
	}
}

Date:2020-05-29
CodeLion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值