1 變量,類型,運算符,表達式

配套視頻教程

本文B站配套視頻教程

image.png

常量

(1)在程序執(zhí)行的過程中,其值不發(fā)生改變的量
(2)字面值常量
A:字符串常量 "hello"
B:整數(shù)常量 12,23
C:小數(shù)常量 12.345
D:字符常量 'a','A','0'
E:布爾常量 true,false
F:空常量 null(后面講)

/*
    常量:
        在程序執(zhí)行過程中,其值不發(fā)生改變的量。
        
    分類:
        A:字面值常量
        B:自定義常量(后面講)
        
    字面值常量
        A:字符串常量    用雙引號括起來的內(nèi)容。
            舉例:"hello","world","HelloWorld"
        B:整數(shù)常量    所有的整數(shù)
            舉例:100,200
        C:小數(shù)常量    所有的小數(shù)
            舉例:10.23,110.11
        D:字符常量    用單引號括起來的內(nèi)容
            舉例:'a','A','0'
            錯誤的:'ab'
        E:布爾常量    比較特殊
            舉例:true,false
        F:空常量    后面講
            舉例:null
*/
class ConstantDemo {
    public static void main(String[] args) {
        //字符串常量的輸出
        System.out.println("hello");
        
        //整數(shù)常量的輸出
        System.out.println(100);
        
        //小數(shù)常量的輸出
        System.out.println(100.10);
        
        //字符常量的輸出
        System.out.println('a');
        System.out.println('A');
        System.out.println('0');
        //這個是有問題的,字符常量有單引號,字符串常量用雙引號
        //System.out.println('ab');
        
        //布爾常量的輸出
        System.out.println(true);
        System.out.println(false);
    }
}

變量的三要素

類型,變量名,保存的值

基本數(shù)據(jù)類型

  1. 數(shù)值
  • 整數(shù) byte,short,int,long
    25,-987, 0
  • 小數(shù) float,double
    5.23,3.14
  1. 字符串
  • 字符串 String
    "你好","中國的首都"
  • 字符 char
    'a', '的'
  1. 布爾型
    boolean

使用一個變量的步驟

  1. 定義一個變量
    數(shù)據(jù)類型 變量名;
    int money;

2.給變量賦值
變量名 = 數(shù)值;
money = 1000 ;

第一步和第二步可以合并
數(shù)據(jù)類型 變量名=數(shù)值;
int money = 1000;

3.使用這個變量
System.out.println(money );

    int age;//定義一個變量
    age = 34; //給變量賦值
    System.out.println(age); //在屏幕上輸出變量的值
    
    String name="zhangsan";//定義一個變量,并且馬上給其賦值
    System.out.println(name); //在屏幕上輸出變量的值

定義幾個變量

手機的品牌brand是華為,價格(price)2500,重量(weight)0.125千克,
顏色(color) 紅

String brand = "華為";
int price = 2500;
double weight = 0.125;
char color = '紅';

   System.out.println("手機品牌:" + brand);
  System.out.println("手機價格:" + price);
image.png

獲取用戶輸入

使用Scanner類

Scanner sc = new Scanner(System.in);


/*
 * Scanner:用于接收鍵盤錄入數(shù)據(jù)。
 * 
 * 前面的時候:
 *         A:導(dǎo)包
 *         B:創(chuàng)建對象
 *         C:調(diào)用方法
 * 
 */
import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        // 創(chuàng)建對象
        Scanner sc = new Scanner(System.in);

        int x = sc.nextInt();
        
        System.out.println("x:" + x);
    }
}
        Scanner scanner = new Scanner(System.in);//定義一個從屏幕獲得輸入信息的變量scanner

        System.out.println("請輸入您的年齡");
        int age = scanner.nextInt();//獲得用戶從屏幕輸入的一個整數(shù),有一個阻塞的副作用,通俗點說,就是程序卡在這里了

        System.out.println("您的年齡是" + age);
        System.out.println("請輸入您的姓名");
        String name = scanner.next();//獲得用戶從屏幕輸入的一個字符串,有一個阻塞的副作用,通俗點說,就是程序卡在這里了

        System.out.println("歡迎您" + name);

Scanner類的hasNextInt()和nextInt()方法


import java.util.Scanner;

/*
 * 基本格式:
 *         public boolean hasNextXxx():判斷是否是某種類型的元素
 *         public Xxx nextXxx():獲取該元素
 * 
 * 舉例:用int類型的方法舉例
 *         public boolean hasNextInt()
 *         public int nextInt()
 * 
 * 注意:
 *         InputMismatchException:輸入的和你想要的不匹配
 */
public class ScannerDemo {
    public static void main(String[] args) {
        // 創(chuàng)建對象
        Scanner sc = new Scanner(System.in);

        // 獲取數(shù)據(jù)
        if (sc.hasNextInt()) {
            int x = sc.nextInt();
            System.out.println("x:" + x);
        } else {
            System.out.println("你輸入的數(shù)據(jù)有誤");
        }
    }
}

Scanner類中的nextLine()產(chǎn)生的換行符問題

import java.util.Scanner;

/*
 * 常用的兩個方法:
 *         public int nextInt():獲取一個int類型的值
 *         public String nextLine():獲取一個String類型的值
 * 
 * 出現(xiàn)問題了:
 *         先獲取一個數(shù)值,在獲取一個字符串,會出現(xiàn)問題。

 * 如何解決呢?
 *         A:先獲取一個數(shù)值后,在創(chuàng)建一個新的鍵盤錄入對象獲取字符串。
 *         B:把所有的數(shù)據(jù)都先按照字符串獲取,然后要什么,你就對應(yīng)的轉(zhuǎn)換為什么。
 */
public class ScannerDemo {
    public static void main(String[] args) {
        // 創(chuàng)建對象
        Scanner sc = new Scanner(System.in);

        // 獲取兩個int類型的值
        // int a = sc.nextInt();
        // int b = sc.nextInt();
        // System.out.println("a:" + a + ",b:" + b);
        // System.out.println("-------------------");

        // 獲取兩個String類型的值
        // String s1 = sc.nextLine();
        // String s2 = sc.nextLine();
        // System.out.println("s1:" + s1 + ",s2:" + s2);
        // System.out.println("-------------------");

        // 先獲取一個字符串,在獲取一個int值
        // String s1 = sc.nextLine();
        // int b = sc.nextInt();
        // System.out.println("s1:" + s1 + ",b:" + b);
        // System.out.println("-------------------");

        // 先獲取一個int值,在獲取一個字符串,這里會出問題
        // int a = sc.nextInt();
        // String s2 = sc.nextLine();
        // System.out.println("a:" + a + ",s2:" + s2);
        // System.out.println("-------------------");

        int a = sc.nextInt();
        Scanner sc2 = new Scanner(System.in);
        String s = sc2.nextLine();
        System.out.println("a:" + a + ",s:" + s);
    }
}

運算符

  1. 賦值運算符
    =

員王浩的Java成績是80分,學(xué)員張萌的Java成績與王浩的相同,輸出張萌的成績

int wangScore = 80;    //王浩成績
int zhangScore;        //張萌成績
zhangScore = wangScore; 
System.out.println(“張萌成績是" +zhangScore);

2.算數(shù)運算符
+,-,*,/,%

        System.out.println(1600%5);//0-4
        System.out.println(3%5);//3
        System.out.println(3%2);//1
        System.out.println(21%5);//1
        System.out.println(13%4);//1
//        System.out.println(m%n);//0----n-1
        System.out.println(23%7);//2
        System.out.println(25%7);//4

一個問題

用戶在屏幕上輸入一個天數(shù),程序回答出是幾周零幾天?

   //  用戶在屏幕上輸入一個天數(shù),程序回答出是幾周零幾天?
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入天數(shù)");
        //獲得用戶輸入的天數(shù)dayCount
        int dayCount = scanner.nextInt();
        System.out.println(dayCount+"天是"+ dayCount/7 + "周零" +  dayCount%7 + "天");

關(guān)系運算符

比較高低、大小、長短等
張三的考試成績是否比李四高
大象是否比烏龜更長壽
籃球跟地球一樣大嗎

,<
==,!=
=,<=
boolean (布爾)類型
boolean類型的值:
真:true
假:false

表達式(3+40%6)>(9/2*3)的結(jié)果是什么?

false

從控制臺輸入張三同學(xué)的成績,與李四的成績(80分)比較,輸出“張三的成績比李四的成績高嗎?” 的判斷結(jié)果

image.png
int liSi = 80;          //學(xué)員李四成績
boolean isBig ;    
        
Scanner input = new Scanner(System.in);   
System.out.print("輸入學(xué)員張三成績: ");       
int zhangSan =  input.nextInt();                     //輸入張三的成績 
        
isBig = zhangSan > liSi ; 
System.out.println( "張三成績比李四高嗎 ? "+isBig );    //輸出比較結(jié)果

表達式

y = x-9+(x +90);

練習(xí)

商場推出幸運抽獎活動
抽獎規(guī)則:
顧客的四位會員卡號的
各位數(shù)字之和大于20,
則為幸運顧客。

image.png

類型轉(zhuǎn)換

自動類型轉(zhuǎn)換

某班第一次Java考試平均分81.29,第二次比第一次多2分,計算第二次考試平均分?

double firstAvg = 81.29;  //第一次平均分
  double secondAvg;         //第二次平均分
  int rise = 2;

  secondAvg = firstAvg + rise;
        
  System.out.println("第二次平均分是:"  + secondAvg);

規(guī)則1:如果一個操作數(shù)為double型,則整個表達式可提升為double型

規(guī)則2:滿足自動類型轉(zhuǎn)換的條件
兩種類型要兼容:
數(shù)值類型(整型和浮點型)互相兼容
目標(biāo)類型大于源類型:
例如:double 型大于 int 型

強制類型轉(zhuǎn)換

(類型名)表達式

int  b  = (int)10.2;
double a = 10;
int c = (int)a;

去年Apple筆記本所占市場份額是20,今年增長的市場份額是9.8,求今年所占份額?

int before = 20;     //apple筆記本市場份額
double rise = 9.8;     //增長的份額

int now = before+ (int)rise;      //現(xiàn)在的份額

當(dāng)強制轉(zhuǎn)換時,精度有損失

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容