大数据系列-day4流程控制语句

  • 条件语句
// IF语句
public
class IfTest1 { public static void main(String[] args) { int heartBeats = 79;
// if...
if(heartBeats < 60 || heartBeats > 100) { System.out.println("需要做进一步检查"); } System.out.println("检查结束");
//if...else
int age = 23; if(age < 18) { System.out.println("你还可以看动画片"); }else { System.out.println("你可以看成人片了"); } // if...elseif...else if(age < 0) { System.out.println("您输入的数据非法"); }else if(age < 18){ System.out.println("青少年时期"); }else if(age < 35) { System.out.println("青壮年时期"); }else if(age < 60) { System.out.println("中年时期"); }else if(age < 120) { System.out.println("老年时期"); }else { System.out.println("你是要成仙呀~~"); } } }
// 从键盘获取变量
import java.util.Scanner;

public class ScannerTest {
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        System.out.println(num);
    }
    
}
// 从键盘获取不同类型的变量
import java.util.Scanner;

public class ScannerTest1 {
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("请输入你的姓名 >");
        String name = scan.next();
        System.out.println(name);
        
        System.out.println("请输入你的芳龄 >");
        int age = scan.nextInt();
        System.out.println(age);
        
        System.out.println("请输入你的体重 >");
        double weight = scan.nextDouble();
        System.out.println(weight);
        
        System.out.println("你是否相中我了呢?(true/false)");
        boolean isLove = scan.nextBoolean();
        System.out.println(isLove);
        
        //char的获取:获取字符串后,取索引为0的位置
        System.out.println("请输入你的性别:(男/女)");
        String gender = scan.next();
        char genderChar = gender.charAt(0);
        System.out.println(genderChar);
    }
    
}

 

import java.util.Scanner;

public class IfTest {
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("请输入岳云鹏期末考试成绩:(0-100)");
        int score = scan.nextInt();
        
        if(score == 100) {
            System.out.println("奖励一台BMW");
        }else if(score > 80 && score <= 99) {
            System.out.println("奖励一台iphone xs max");
        }else if(score >= 60 && score <= 80) {
            System.out.println("奖励一个ipad");
        }else {
            System.out.println("什么奖励也没有");
        }
    }
    
}
//switch...case
public class SwitchCaseTest {
    public static void main(String[] args) {
        
        int number =2;
        switch(number) {
        
        case 0:
            System.out.println("zero");
            
        case 1:
            System.out.println("one");
            
        case 2:
            System.out.println("two");
            
        case 3:
            System.out.println("three");
            
        default:
            System.out.println("other");
        }
    }
    
}
public class SwitchCaseTest2{
    public static void main(String[] args) {
        
        String reason = "summer";
        switch(reason) {
        case "spring":
            System.out.println("春暖花开");
            break;
        case "summer":
            System.out.println("夏日炎炎");
            break;
        case "autumn":
            System.out.println("秋高气爽");
            break;
        case "winter":
            System.out.println("冬雪皑皑");
            break;
        default:
            System.out.println("季节输入有误");
            break;
        }
    }
    
}
  • 循环语句
public class ForTest {
    public static void main(String[] args) {
        
        int num=1;
        for(System.out.println('a');num<=3;System.out.println('c'),num++) {
            System.out.println('b');
        }
    }
}
// 遍历100以内偶数,输出所有偶数的和,输出偶数的个数
public class ForTest1{
    public static void main(String[] args) {
        
        int sum = 0;
        int count = 0;
        for(int i =1; i <= 100; i++) {
            
            if(i % 2 == 0) {
                System.out.println(i);
                sum += i;
                count++;
            }
        }
        
        System.out.println("总和为: " + sum);
        System.out.println("个数为: " + count);
    }
}

 

posted @ 2020-05-25 16:55  覆水难收d  阅读(126)  评论(0编辑  收藏  举报