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