Java自学 --换车重启2

Java自学 --换车重启2


if-else练习题

//4-10旺季票价,成人(18-60)60 儿童(<18)半价 老人(>60) 1/3
//淡季  成人40 其他20
import java.util.Scanner;
public class Test04 {
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入月份:");
        int age ;
        double price;
        int month = scanner.nextInt();
        if(month >= 4 && month <= 10){
            System.out.println("~欢迎光临,本月为旺季~");
            price = 60 ;
            System.out.println("请输入您的年龄:");
            age = scanner.nextInt();
            if(age > 60){
                System.out.println("老人票:" + price/3 + "元" );
            }else if(age < 18 && age >=0){
                System.out.println("儿童票:" + price/2 + "元" );
            }else if(age >= 18 && age <= 60){
                System.out.println("成人票:" + price + "元" );
            }else{
                System.out.println("非法年龄");
            }

        }else if((month > 0 && month <= 3)
                ||(month >= 11 && month <= 12)){
            System.out.println("~欢迎光临,本月为淡季~");
            price = 40 ;
            System.out.println("请输入您的年龄:");
            age = scanner.nextInt();
            if(age > 60){
                System.out.println("老人票:" + "20" + "元" );
            }else if(age < 18 && age >=0){
                System.out.println("儿童票:" + "20" + "元" );
            }else if(age >= 18 && age <= 60){
                System.out.println("成人票:" + price + "元" );
            }else{
                System.out.println("非法年龄");
            }
        } else {
            System.out.println("月份出错");
        }
    }
}

Switch

在java中,只要是有返回值,就是一个表达式

Scanner scanner = new Scanner(System.in);
char  c1 = scanner.next().charAt(0);
switch(c1){  // **在java中,只要是有返回值,就是一个表达式**
        case ** :
        breark;
}
  • 表达式数据类型, 应和case后的常量类型一致, 或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是int
  • switch(表达式) 中表达式的返回值必须是:(byte, short, int, char, enum[枚举] , String)
  • case子句中的值必须是常量, 而不能是变量
  • default子句是可选的, 当没有匹配的case时, 执行default
  • break语句用来在执行完一个case分支后使程序跳出switch语句块; 如果没有写break,程序会顺序执行到switch结尾,除非遇到break;

练习

​ 很有意思的编程思想,如果一定要用swich来表示这个练习的话

//对学生成绩大于 60 分的,输出"合格"。低于 60 分的, 
//输出"不合格"。(注:输入的成绩不能大于 100), 提示 成绩/60 
//思路分析 
//1. 这道题,可以使用 分支来完成, 但是要求使用 switch 
//2. 这里我们需要进行一个转换, 编程思路 : 
// 如果成绩在 [60,100] , (int)(成绩/60) = 1 
// 如果成绩在 [0,60) , (int)(成绩/60) = 0

import java.util.Scanner;
public class Test04 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩");
        double score = scanner.nextDouble();
        if(score>=0 && score <= 100){
            switch ((int)(score/60)){
                case 1 :
                    System.out.println("合格");
                    break;
                case 0 :
                    System.out.println("不合格");
                    break;
            }
        }else {
            System.out.println("成绩不合法");
        }
    }
}

For 练习

//统计 3 个班成绩情况,每个班有 5 名同学, 
// 求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]。 
// 统计三个班及格人数,每个班有 5 名同学。

import java.util.Scanner;
public class Test04 {
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        double sum2 = 0;
        double score;

        for (int i = 1; i <= 3; i++) {
             double sum1 = 0;
            int student = 0;
            for (int j = 1; j <= 5; j++) {
                System.out.println("请输入"+ i +"班第"
                        + j +"个同学的成绩:" );
                score = scanner.nextDouble();
                sum1 += score;
                if(score>=60){
                    student++;
                }
            }
            System.out.println(i+"班的平均成绩为"+(sum1/5) );
            System.out.println(i+"班及格人数为"+student);
            sum2 += sum1;
        }
        System.out.println("3本班的总平均分为"+ sum2/15);
    }
}

空心菱形

思路分析 化繁为简
1. 先打印一个矩形
*****
*****    
*****
*****
*****
2. 打印半个金字塔
*        //第1层有1个*
**       //第2层有2个*
***      //第3层有3个*
****     //第4层有4个*
*****    //第5层有5个*
3. 打印整个金字塔 
    *      //第1层有1个*  2*1-1 有4=(总层数-1)个空格 
   ***     //第2层有3个*  2*2-1 有3=(总层数-2)个空格 
  *****    //第3层有5个*  2*3-1 有2=(总层数-3)个空格
 *******   //第4层有7个*  2*4-1 有1=(总层数-4)个空格
*********  //第5层有9个*  2*5-1 有0=(总层数-5)个空格
4. 打印空心的金字塔 [最难的] 
    *    //第1层有1个* 当前行的第一个位置是*,最后一个位置也是* 
   * *   //第2层有2个* 当前行的第一个位置是*,最后一个位置也是*
  *   *  //第3层有2个* 当前行的第一个位置是*,最后一个位置也是*
 *     * //第4层有2个* 当前行的第一个位置是*,最后一个位置也是*
*********   //第5层有9个* 全部输出*
5.课堂作业 - 空心菱形
先用一开始打半个金字塔的思想打出预留的空格,这边的总层数要比上面的要少一层
然后开始准备*号的数量,找出星号与行数间的关系,尽量实现和最上面totolLevel层数这个变量的数学关系,然后打出实心的反向三角形
依样画葫芦把三角形掏空    
    
public class Test04 {
    public static void main(String[] args){

        int totolLevel= 5;
        for (int i = 1; i <= totolLevel; i++) {

            for (int k = 1; k <= (totolLevel-i) ; k++) {
                System.out.print(" ");
            }

            for (int j = 1; j <= 2*i-1; j++) {
                if(j== 1 || j== 2*i-1 ){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
        for (int i = 1; i <= totolLevel-1 ; i++) {

            for (int k = 1; k <= i; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (totolLevel-i)*2-1; j++) {
                if(j== 1 || j==(totolLevel-i)*2-1){
                System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println("");
        }
    }
}    

break跳转控制语句

​ (1) break语句可以指定退出哪层
​ (2) label 1是标签, 由程序员指定
​ (3) break后指定到哪个label就退出到哪里
​ (4)在实际的开发中,尽量不要使用标签.
​ (5) 如果没有指定break, 默认退出最近的循环体

//break终止循环   for ,while ,do-while 都可以用

//ROLL到97就停止,并记录ROLL了几次
//Math.random()   0-1之间的随机小数,不包括1
public class Test04 {
    public static void main(String[] args){

        int a ;
        int sum = 0;
        for (;;) {
            a = (int)(Math.random()*100+1);
            if(a!=97){
                System.out.println(a);
                sum++;
            }else{
                ++sum;
                System.out.println(a);
                System.out.println("roll了"+sum+"次终于出了");
                break;
            }
        }
    }
}
import java.util.Scanner;
public class Test04 {
    public static void main(String[] args){

       String id = "丁真";
       String code = "666";
       Scanner scanner = new Scanner(System.in);

        for (int i = 1; i <= 3; i++) {
            String id1;
            String code1;
            System.out.println("请输入帐号:");
            id1 = scanner.next();
            System.out.println("请输入密码:");
            code1 = scanner.next();
            if(id.equals(id1)  && code.equals(code1)){
                System.out.println("登录成功");
                    break;
            }else{
                System.out.println("密码错误,还有"+(3-i)+"次机会 ");
            }
        }
        System.out.println("done");
    }
}

字符串比较–是否相等

String name "林黛玉";
System.out.println(name.equals("贾宝玉"))    
System.out.println("贾宝玉".equals(name))//推荐,可以避免空指针    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值