java基础:数据类型、运算、判断、循环

基本数据类型:

  整型(int)、浮点型(float、double)、布尔类型(boolean)、字符类型(char)

引用数据类型:

  字符串(String)

        System.out.println("hello world!");
        int x = 1;
        System.out.println(x);
        x = 100;
        System.out.println(x);
        int y = x;
        System.out.println(y);
        float z = 3.14f;
        boolean a = true;
        boolean b = false;
        boolean isGreater = 5 > 3;
        System.out.println(isGreater);

常量:

  final 类型= 值

  不可被再次赋值

final double PI = 3.14;// 定义常量

三元运算符:

        int age = 7;
        // 三元运算符 b?x:y;  x与y的类型必须相同 如果b为true,则返回x;如果b为false,则返回false
        String res =  age <= 12 && age > 6 ?"yes":"no";
        System.out.println(res); yes

布尔运算:

优先级:

  • !
  • >>=<<=
  • ==!=
  • &&
  • ||
boolean isGreater = 5 > 3; // true
int age = 12;
boolean isZero = age == 0; // false
boolean isNonZero = !isZero; // true
boolean isAdult = age >= 18; // false
boolean isTeenager = age >6 && age <18; // true

 switch条件选择:

  例子:石头、剪刀、布游戏

  

package com.company;

import java.util.Scanner;

/**
 * @Time :2020/6/28 13:49
 * @Description: <描述>
 * @作者 :aiyumo
 */
public class JudgeTest {
    public static void main(String[] args){
        // int n = 70;
        // if (n >= 60) {
        //     System.out.println("及格了");
        // }
        Scanner scanner = new Scanner(System.in);
        System.out.println("please choice:");
        System.out.println(" 1: Rock(石头)");
        System.out.println(" 2: Scissors(剪刀)");
        System.out.println(" 3: Paper(布)");

        while (true){
            String option = scanner.nextLine();
            int random = 1 + (int) (Math.random() * 3);
            String randomS = random + "";
            switch (option){
                case "1":{
                    switch (randomS) {
                        case "1":System.out.println("平局!");break;
                        case "2":System.out.println("你赢了!");break;
                        case "3":System.out.println("你输了!");break;
                    }
                    break;
                }
                case "2":{
                    switch (randomS) {
                        case "1":System.out.println("你输了");break;
                        case "2":System.out.println("平局!");break;
                        case "3":System.out.println("你赢了!");break;
                    }
                    break;
                }
                case "3":{
                    switch (randomS) {
                        case "1":System.out.println("你赢了");break;
                        case "2":System.out.println("你输了!");break;
                        case "3":System.out.println("平局!");break;
                    }
                    break;
                }
                case "q": System.out.println("退出游戏");return;
                default:
                    System.out.println("输入错误,请重新输入!");
            }
        }
    }

}
View Code

 循环:

  1、while循环

    while(条件){循环语句}

  2、do...while循环

    do{语句}while(条件) 至少执行一次循环

  3、for循环

    for(初始变量;循环检测条件;计数器;){语句}

    注意:for(;;;)可以省略初始变量、条件、计数器,但;必须存在

    遍历迭代类型中的元素

      for(one:可迭代类型){语句}

package com.company;

/**
 * @Time :2020/6/28 15:41
 * @Description: <描述>
 * @作者 :aiyumo
 */
public class CirculatoryTest {
    public static void main(String[] args) {
        int sum = 0;
        int n = 100;
        int m = 20;
        // while (m<=n) {
        //     sum += m;
        //     m++;
        // }

        // do{
        //   sum += m;
        //   m++;
        // }while (m<=n);
        // for (;m<=n;m++){
        //     sum +=m;
        // }
        // System.out.printf("20~100的加和为%d",sum);
        int[] ns = { 1, 4, 9, 16, 25 };
        for (int i=0;i<ns.length;i++){
            sum = sum + ns[i];
        }
        System.out.printf("数组加和为%d",sum);
        // 遍历数组获取数组的每个值
        for (int one : ns){
            System.out.println(one);
        }
        // 倒序输出数组的每个元素
        for (int i=ns.length-1;i>-1;i--){
            System.out.println(ns[i]);
        }
        // 使用for each 实现数组元素加和
        for (int one :ns){
            sum = sum + one;
        }
        System.out.printf("数组元素和为%d",sum);
        
        // 计算圆周率
        double pi = 0;
        for(int i = 1;i<1000000;i++){
            pi = pi + (1.0/(2*i-1) * Math.pow(-1,(i+1)));
        }
        pi = pi * 4;
        System.out.println(pi);
    }
}
View Code

 

posted @ 2020-06-12 16:51  爱语默  阅读(212)  评论(0)    收藏  举报