目录
1.100000元过路口,大于50000时每次交5%,小于50000时每次交1000,看看能过多少路口,要求while+break;
代码都是由上往下去执行。
if(条件表达式){执行代码块;可以有多条执行语句};只有一条语句大括号可以省略,但是建议写哦。
案例:输入一个年龄大于18输出,成年人要对自己行为负责,送入监狱
- import java.util.*; //导入
- public class Hello{
- public static void main(String[]args){
- Scanner scanner = new Scanner(System.in);
- if(scanner.nextInt() >= 18){
- System.out.println("成年人要对自己行为负责,送入监狱");
- }
- }
- }
if ( 条件表达式 ){ 执行代码块;可以有多条执行语句 } else { 执行代码块;可以有多条执行语句 }
案例:输入一个年龄大于18输出,成年人要对自己行为负责,送入监狱,小于输出这次放过你哦
- import java.util.*; //导入
- public class Hello{
- public static void main(String[]args){
- Scanner scanner = new Scanner(System.in);
- if(scanner.nextInt() >= 18){
- System.out.println("成年人要对自己行为负责,送入监狱");
- }else{
- System.out.println("这次放过你哦");
- }
- }
- }
if ( 条件表达式 ){ 执行代码块;可以有多条执行语句 } else if ( 条件表达式 ){ 执行代码块;可以有多条执行语句 }
案例:键盘输入判断:100:优秀,80-99:良好,60-80:一般,其他输出不及格啊
- import java.util.*; //导入
-
- public class Hello {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int score = scanner.nextInt();
- if (score == 100) {
- System.out.println("very good");
- } else if (score >= 80 && score <= 99) {
- System.out.println("good");
- } else if (score >= 60 && score < 80) {
- System.out.println("just so so");
- } else {
- System.out.println("not good");
- }
- }
- }
if(条件表达式) { if( 条件表达式 ){ 执行语句 }}
案例:输入成绩大于8.0,性别为其分配组
- import java.util.*; //导入
-
- public class Hello {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double score = scanner.nextInt();
- String gender = scanner.next();
- if(score>8.0){
- if (gender.equals("man")){ //注意这里判断是equals哦,不能用==
- System.out.println("man group score:"+score);
- }else {
- System.out.println("woman group score:"+score);
- }
- }else {
- System.out.println("sorry,you not engage us");
- }
- }
- }
switch( 表达式 ){ case 常量: 语句块;break; default 语句块 break; } 注意没有break会一直执行的
案例1:输入a、b,输出星期一二等等
- import java.util.*; //导入
-
- public class Hello {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- char c1 = scanner.next().charAt(0);
- switch (c1) {
- case 'a':
- System.out.println("Moday");
- break;
- case 'b':
- System.out.println("Tuesday,wednesday ,Thursday Friday Saturday Sunday");
- break;
- default:
- System.out.println("please input right character");
- }
- }
- }
案例2:345春季,678夏季,91011秋,1212春天
- import java.util.*; //导入
-
- public class Hello {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int season = scanner.nextInt();
- switch (season) {
- case 3:
- case 4:
- case 5:
- System.out.println("spring");
- break;
- case 6:
- case 7:
- case 8:
- System.out.println("summer");
- break;
- case 9:
- case 10:
- case 11:
- System.out.println("autumn");
- break;
- case 12:
- case 1:
- case 2:
- System.out.println("winter");
- break;
- default:
- System.out.println("input error,again");
- }
- }
- }
具体数字不多,且是byte,short,int,char,enum使用switch
区间的使用if挺好的
定义:for(int i = 0 ; i < 10 ; i++){ } i为局部变量 for(;;){} 无限循环
案例:打印1-100的9的整数,统计个数,并计算他的和
- public class Hello {
- public static void main(String[] args) {
- int sum = 0, count = 0;
- for (int i = 1; i < 100; i++) {
- if (i % 9 == 0) {
- System.out.println(i);
- sum += i;
- count ++;
- }
- }
- System.out.println("count:"+count);
- System.out.println("sum:"+sum);
- }
- }
定义 while(循环条件){ 循环体;循环迭代 }
案例:输出1-100可以被3整除的数
- public class Hello {
- public static void main(String[] args) {
- int i = 1;
- while (i <= 100) {
- if (i % 3 == 0)
- System.out.println("i="+i);
- i++;
- }
- }
- }
定义:do{代码块,循环体} while(表达式) 最少执行一次
案例:统计1-200之间能被5整除不能被3整除的个数
- public class Hello {
- public static void main(String[] args) {
- int i = 1,count = 0;
- do {
- if (i % 5 == 0 && i % 3 != 0) {
- count++;
- System.out.println("i="+i);
- }
- i++;
- } while (i <= 200);
- System.out.println(count);
- }
- }
定义:for(){ for(){ for(){ } } } 套三层很多了
案例1:打印九九乘法表
- public class Hello {
- 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();
- }
- }
- }
案例2:打印空心金字塔
- public class Hello {
- public static void main(String[] args) { // *
- for (int i = 1; i <= 5; i++) { // **
- //打印空格 // * *
- for (int k = 1; k <= 5 - i; k++) { // * *
- System.out.print(" "); // *********
- }
- //打印*
- for (int j = 1; j <= 2 * i - 1; j++) {
- if (j == 1 || j == 2 * i - 1 || i == 5) {
- System.out.print("*");
- } else {
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
- }
定义:可以直接跳出循环结束循环,字符串比较使用equals
*标签的使用,break可以使用标签控制跳出那个循环,尽量不要用在实际的开发中。
- public class Hello {
- public static void main(String[] args) {
- lable1:
- for (int i = 0; i < 4; i++) {
- lable2:
- for (int j = 0; j < 10; j++) {
- if(j == 2) break lable1;
- System.out.println("i"+j);
- }
- }
- }
- }
案例:输入账号123456密码abc123,3次机会
- import java.util.*;
-
- public class Hello {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- boolean flag = false;
- String username, password;
- for (int i = 0; i < 3; i++) {
- System.out.println("please input username");
- username = sc.next();
- System.out.println("please input password");
- password = sc.next();
- if (username.equals("123456") && password.equals("abc123")) {
- System.out.println("successs");
- flag = true;
- break;
- } else {
- System.out.println("faith,have " + (2 - i) + "chance");
- }
- }
- if (!flag) System.out.println("no chance");
- }
- }
定义:结束本次循环,进入下次循环。
*标签的使用,continue可以使用标签控制结束那次循环进入下次循环,尽量不要用在实际的开发中。
- public class Hello {
- public static void main(String[] args) {
- lable1:
- for (int i = 0; i < 4; i++) {
- lable2:
- for (int j = 0; j < 10; j++) {
- if (j == 2) continue lable1;
- System.out.println("j " + j);
- }
- }
- }
- }
定义:用在方法跳出方法,写main里面结束程序,与break一样
- public class Hello {
- public static void main(String[] args) {
- double money = 100000, count = 0;
- while (money > 1000) {
- if (money > 5000) {
- money *= 0.95;
- } else {
- money -= 1000;
- }
- if (money > 1000){
- count++;
- }
- }
- System.out.println(count);
- }
- }
- import java.util.*;
-
- public class Hello {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int floor = sc.nextInt();
- int h = floor / 100; //百位
- int t = floor / 10 % 10; // 十位
- int s = floor % 10; //个位
- if (floor == (h*h*h+t*t*t+s*s*s)){
- System.out.println("is floor");
- }else{
- System.out.println("not is floor");
- }
- }
- }