一,Random和Scanner
Random:生成隨機數字
Scanner:掃描器,可以獲取用戶在控制檯輸入的信息
Random 的用法:
==============================================================
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
//定義了變量i的數據類型是int類型,其值是123.
int i = 123;
//定義了變量r的數據類型是Random類型,
//其值是引用Random的一個對象的地址值。
Random r = new Random();
//nextInt()
//在int範圍內生成隨機數字
int j = r.nextInt();
long l = r.nextLong();
System.out.println(j);
//nextInt(n)
//數字n是決定生成數字的範圍
//範圍是[0,n)
i = r.nextInt(10);//[0,10)
System.out.println(i);
/*
* 隨機生成1-10之間的數(包括1和10)
*/
i = r.nextInt(10) + 1;
}
}
===================================================================================================================================
二,選擇語句
if...else語句
==================================================================
import java.util.Scanner;
public class IfTest2 {
public static void main(String[] args) {
/*
* 一杯汽水5元,凡購買2杯起,即可享受半價優惠。
*
* 小於 <
* 大於 >
* 小於等於 <=
* 大於等於 >=
* 除法 /
*/
int count = 0;//杯數
int price = 5;//單價
double total = 0;//總價
boolean flag = true;//標誌位
Scanner sc = new Scanner(System.in);
System.out.println("請輸入購買杯數:");
count = sc.nextInt();
//如果小於1杯
if(count<1){
flag = false;
System.out.println("必須至少購買1杯。");
}
//1杯的價錢
total = price;
//如果大於1杯
if(count>1){
total = total + (count-1)*price/2.0;
}
if(flag==true){
System.out.println("總額是:"+total);
}
}
}
====================================================================================================================================
switch...case語句
==================================================================
import java.util.Random;
public class SwitchDemo {
public static void main(String[] args) {
Random r = new Random();
int random = r.nextInt(6);//[0,5]
System.out.println(random);
System.out.println("美女:嘿,想約你去吃桂林米粉~~");
/*
* switch(){
*
* }
*
* 1.switch只能捕捉整數(byte,short,char,int)或整數表達式
* 2.case也只能處理常量
* 3.如果case分支中沒有break,會一直往下執行。
* 直到有break或default。
*
*/
switch(random){
case 0:
System.out.println("我:去吧!");
break;
case 1:
System.out.println("我:我在上班!");
break;
case 2:
System.out.println("我:我在LOL!");
break;
case 3:
System.out.println("我:我不想鳥你!");
break;
default :
System.out.println("我:我有老婆了!");
}
}
}
===================================================================================================================================
二,JAVA運算符
1.數學運算符
+ - * / %
2.自加自減運算符
前加加
後加加
前減減
後減減
====================================================================================================================================
/**
* 自增自減運算符
* @author Administrator
*
*/
public class IncreDecrementDemo {
public static void main(String[] args) {
/*
* 後加加
* 先使用,再自己加1
*/
int a = 1;
int b = a++;//後加加
System.out.println("a:"+a+" b:"+b);//a=2,b=1
/*
* 前加加
* 先自加1,再使用
*/
a = 1;
b = ++a;//前加加
System.out.println("a:"+a+" b:"+b);//a=2,b=2
int x = 3;
int y = 4;
int z = x++ + ++y;
System.out.println("x:"+x+" y:"+y+" z:"+z);//x=4,y=5,z=8
}
}
====================================================================================================================================
3.邏輯運算符
&& 與,並且
|| 或,或者
& 與,並且
| 或,或者
4.比較運算符
> >= < <= == !=
====================================================================================================================================
5.位運算符(二進制的運算)
& 按位與
| 按位或
^ 按位異或
~ 按位取反
====================================================================================================================================
/**
* 位運算符
* @author Administrator
*
*/
public class BitwiseDemo {
public static void main(String[] args) {
/*
* 0000 0101 5
* &
* 0000 1100 12
* ---------
* 0000 0100 4
*
*/
int a = 5 & 12;
System.out.println(a);//4
/*
* 0000 0101 5
* |
* 0000 1100 12
* ---------
* 0000 1101 13
*
*/
int a2 = 5 | 12;
System.out.println(a2);//13
/*
* 0000 0101 5
* ^
* 0000 1100 12
* ---------
* 0000 1001 9
*
*/
int a3 = 5 ^ 12;
System.out.println(a3);//9
System.out.println(~130);//-131
}
}
====================================================================================================================================
6.三目運算符
? : a>b?a:b (意思是先看看a是不是大於b,如果大於,則返回結果a,如果不大於(就是a<=b)返回結果b)
===================================================================================
/**
* 三目運算符
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
int a = 10 > 5 ? 99 : 100;
System.out.println(a);
/*
* 一本書共有126行
* 每一頁顯示 ? 行(不確定行數)
* 根據輸入的行數,
* 求出這個書的總頁數
*/
int rows = 126;
Scanner sc = new Scanner(System.in);
int pageRows = sc.nextInt();
int pages = rows%pageRows==0 ? rows/pageRows : rows/pageRows+1 ;
System.out.println(pages);
}
====================================================================================================================================