變量的三要素
類型季惩,變量名录粱,保存的值
基本數(shù)據(jù)類型
1.數(shù)值
- 整數(shù) byte ,short,int,long 25,-987,0
- 小數(shù) float,double 5.23,3.14
2.字符串
- 字符串 String "你好"画拾,"中國的首都"
- 字符 char 'a' , '的'
3.布爾型
boolean
使用一個變量的步驟
1.定義一個變量
2.給變量賦值
3.使用這個變量
int age;//定義一個變量
age=34;//給變量輔助
System.out.printin(age);//在屏幕上輸出變量的值
String name="zhangsan"http://定義一個變量啥繁,并馬上給其賦值
System.out.printin(name);//在屏幕上輸出其變量的值
定義幾個變量
手機的品牌(brand)是華為,價格(price)2500青抛,重量0.125kg旗闽,顏色紅
String brand="華為";
int price=2500;
double weight=0.125;
char color='紅';
System.out.println("手機品牌"+brand);
System.out.println("手機重量"+weight);
System.out.println("手機價格"+price);
System.out.println("手機顏色"+color);
獲取用戶輸入
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);
運算符
1.賦值也能算符
2.算數(shù)運算符
+适室,-,*,/,%
關(guān)系運算符
,
<,
==,(等于)
!=,(不等于)
=,
<=
表達(dá)式
例題
System.out.println("請輸入你的姓名");
String name=scanner.next();//獲得用戶從屏幕輸入的一個字符串
System.out.println("歡迎你"+name);
double pai = 3.14;
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入圓的半徑");
int radius =scanner.nextInt();// 獲得用戶輸入的半徑(整數(shù))
double zhouchang =2*pai*radius;
System.out.println("圓的周長是"+zhouchang);
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入Java成績");
int Javagrade = scanner.nextInt();
System.out.println("請輸入oracle成績");
int oraclegrade = scanner.nextInt();
System.out.println("請輸入html成績");
int htmlgrade = scanner.nextInt();
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入天數(shù)");
int day = scanner.nextInt();
System.out.println(day/7+"周零"+day%7+"天");
int rate = 20;
double thisyear = rate+9.8;
System.out.println(thisyear);
從控制臺輸入張三同學(xué)的成績嫡意,與李四的成績(80分)比較,輸出“張三的成績比李四的成績高嗎?” 的判斷結(jié)果
int lisi = 80;
Scanner scanner=new Scanner(System.in);
System.out.println("輸入張三同學(xué)的成績");
int zhangsan = scanner.nextInt();
boolean ishigh = zhangsan>lisi;
System.out.println("張三的成績比李四高嗎?"+ishigh);
商場推出幸運抽獎活動
抽獎規(guī)則:
顧客的四位會員卡號的3569
各位數(shù)字之和大于20捣辆,
則為幸運顧客蔬螟。
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入四位會員卡號");
int cardNo = scanner.nextInt();
int cardnoback = cardNo;
int gewei = cardNo%10;
cardNo = cardNo/10;
int shiwei = cardNo%10;
cardNo= cardNo/10;
int baiwei = cardNo%10;
cardNo = cardNo/10;
int qianwei = cardNo%10;
System.out.println("會員卡號"+cardnoback+"各位之和"+(gewei+shiwei+baiwei+qianwei));
boolean ISlucky = (gewei+shiwei+baiwei+qianwei)>20;
System.out.println("是幸運客戶嗎?"+ISlucky);
水仙花數(shù)
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入一個三位數(shù)");
int number = scanner.nextInt();
int numberback = number;
int gewei = number%10;
number = number/10;
int shiwei = number%10;
number= number/10;
int baiwei = number%10;
boolean folwernumber = (gewei*gewei*gewei+shiwei*shiwei*shiwei+baiwei*baiwei*baiwei)==numberback;
System.out.println("是水仙花數(shù)嗎汽畴?"+folwernumber);