美文网首页
【JAVA学习笔记】基础语法

【JAVA学习笔记】基础语法

作者: Geekero | 来源:发表于2021-03-22 21:02 被阅读0次

学习自华为开发者学院JAVA系列课程

数据类型

数据类型转换(自动转换+强制转换)

public class AppleSal {
    public static void main(String[] args){
        int before = 20;
        //Java 默认浮点数类型是double
        //float rise = 9.8f;
        double rise = 9.8;

        double result = before + rise;
        System.out.println("今年苹果笔记本销售额为: "+result);

        int result2 = before + (int)rise;
        System.out.println("今年苹果笔记本销售额为: "+result2); //精度丢失

        int result3 = (int)(before + rise);
        System.out.println("今年苹果笔记本销售额为: "+result3); //精度丢失
    }
}
public class CalcAvg {
    public static void main(String[] args){
        double firstArg = 81.29;
        double secondArg;
        int rise = 2;
        //自动类型转换:小范围-->大范围
        //int 和 double运算,结果自动提升为double
        secondArg = firstArg + rise;
        System.out.println("第二次平均分为: "+secondArg);
    }
}

运算符

import java.time.format.SignStyle;

public class CalcSymbol {
    public static void main(String[] args){
        // /取商 %取余
        int num1 = 5;
        int num2 = 2;
        System.out.println(num1/num2); //商2
        System.out.println(num1%num2); //余1

        //自加++ 自减--
        //++或--在变量前,先自加后自减运算,再使用
        //反之,先使用完变量,再自加或者自减
//        System.out.println(++num1);  //6: num1 = num1 + 1
//        System.out.println(--num2); //1 num2 = num2 - 1
//        System.out.println(num1); //6
//        System.out.println(num2); //1
        System.out.println(num1++);  //6: num1 = num1 + 1
        System.out.println(num2--); //1 num2 = num2 - 1
        System.out.println(num1); //6
        System.out.println(num2); //1

        //符合运算符
        System.out.println("-----------------------");
        int num3 = 5;
        num3 += 2;
        System.out.println(num3);

        System.out.println("-----------------------");
        double num4 = 5;
        double num5 = 2;
        System.out.println(num4/num5); //2.5
        System.out.println(num4%num5); //1.0
    }
}

逻辑判断

import java.util.Scanner;

public class LuckyCard {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入您的四位会员卡号: ");
        int no = input.nextInt();

        //分解数字8645
        int geWei = no % 10;
        System.out.print("会员卡号的各位数字: 个位" + geWei);
        int shiWei = no / 10 % 10;
        System.out.print("十位" + shiWei);
        int baiWei = no / 100 % 10;
        System.out.print("百位" + baiWei);
        int qianWei = no / 1000 % 10;
        System.out.print("千位" + qianWei);

        //计算数字之后
        int sum = geWei + shiWei + baiWei + qianWei;
        System.out.println("会员卡号后四位数字之和为: " + sum);

        //判断和是否大于20
//        String info = (sum<=20) ? "谢谢参与" : "恭喜卡号"+no+"的会员,您中了Iphone一部!";
//        System.out.println(info);
        //会员卡号百位数=系统随机数(0~10,不包括10)
        int random = (int)(Math.random()*10);
//        System.out.println(random);
        if(baiWei==random){
            System.out.println("恭喜会员"+no+"获得了精美礼品一份! ");
        }else{
            System.out.println("会员"+no+"感谢参与! ");
        }
   }
}

三目运算符

import java.util.Scanner;

public class IsEven {
    //条件表达式判断是否为偶数 三目运算符
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个非0的数字: ");
        int num = input.nextInt();

        String reuslt = (num%2==0) ? "偶数" : "奇数";
        System.out.println(num+"是"+reuslt);
    }
}

选择结构

IF语句

import com.sun.org.apache.xpath.internal.objects.XString;

import java.util.Scanner;

public class RunningMatch {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入跑步成绩(s): ");
        double score = input.nextDouble();
        
        //成绩判断
        if(score<10){
            //分组
            System.out.print("请输入性别: ");
            String sex = input.next();
            if(sex.equals("男")){
                System.out.println("进入男子组决赛!");
            }else if(sex.equals("女")){
                System.out.println("进入女子组决赛!");
            }else{
                System.out.println("性别输入不正确!");
            }
        }else{
            System.out.println("淘汰!");
        }
    }
}

Case Switch语句

import java.util.Scanner;

public class Complete {

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入比赛名次: ");
        int rank = input.nextInt();
        switch (rank) { //表达式内数据类型只能是int,short,byte,char,枚举类型String; 不能是double!!!!
            case 1:
                System.out.println("参加麻省理工大学计算机学院组织1个月的夏令营");
                break;
            case 2:
                System.out.println("奖励惠普笔记本一部");
                break;
            case 3:
                System.out.println("奖励移动硬盘一部");
                break;
            default:
                System.out.println("没有任何奖励");
                break;
        }
    }
}

循环结构

while循环

public class PrintExam {
    public static void main(String[] args){
        //打印试卷份数
        int count = 1;
        while(count<=50){
            System.out.println("打印第"+count+"份试卷");
            count++;
        }
    }
}
import java.util.Scanner;

public class StudyList {

        public static void main(String[] args){
            String answer;
            Scanner input = new Scanner(System.in);
            System.out.print("合格了吗?(y/n) ");
            answer = input.next();
            while(!"y".equals(answer)){
                System.out.println("Do...");
                //每次执行完需要再验证是否合格,保证能跳出循环
                System.out.print("合格了吗?(y/n) ");
                answer = input.next();
            }
        //合格时跳出循环:
        System.out.println("完成!");
        }
}

Do while循环

import java.util.Scanner;

public class DoWhileDemo {

   public static void main(String[] args){
       Scanner input = new Scanner(System.in);
       String answer = ""; //标识是否为合格

       do{
           System.out.println("执行!");
           System.out.print("合格了吗?(y/n) ");
           answer = input.next();
       }while(!answer.equals("y"));

   //跳出循环后后执行
   System.out.println("完成!");
   }
}

for循环

public class PrintNums {

    public static void main(String[] args){
        //while
//        int i = 1;
//        while(i<=100){
//            System.out.println(i);
//            i++;
//        }
        //for
        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
    }
}
import java.util.Scanner;

public class SumTable {

    public static void main(String[] args){
        int i,j;
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个值:");
        int num = input.nextInt();

        for(i=0,j=num; i<num; i++,j--){
            System.out.println(i+"+"+j+"="+(i+j));
        }
    }
}

多重循环

public class PrintRectangle {
    public static void main(String[] args){
        //矩形
        for (int i=1; i<=5; i++) {
            for (int j = 1; j <= 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        //平行四边形
        for (int i=1; i<=5; i++){

            for (int j=1;j<=5-i; j++) {
                //输出每行的空格 5-i
                int space = 5 -i;
                System.out.print(" ");
            }
            for (int j=1;j<=5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        //等腰三角形
        for (int i=1; i<=5; i++){

            for (int j=1;j<=5-i; j++) {
                //输出每行的空格 5-i
                int space = 5 -i;
                System.out.print(" ");
            }
            for (int j=1; j<=2*i-1; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
import java.util.Scanner;

public class PrintTrangleNums {
    public static void main(String[] args) {
        //等腰三角形
        Scanner input = new Scanner(System.in);
        System.out.print("多少行数字? ");
        int num = input.nextInt();
        for (int i=1; i<=num; i++) {

            for (int j = 1; j <= num - i; j++) {
                //输出每行的空格 5-i
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}
//九九乘法表
public class PrintMultiTable {
    public static void main(String[] args) {
        for(int a=1; a<=9;  a++){
            for(int b=1; b<=a; b++){
                System.out.print(a+"*"+b+"="+(a*b)+" ");
            }
            System.out.println();
        }
    }
}

break continue

省略

相关文章

网友评论

      本文标题:【JAVA学习笔记】基础语法

      本文链接:https://www.haomeiwen.com/subject/vostcltx.html