1. 三元表達式的嵌套
int b=0 ;
int a = 1;
System.out.println( b = b == a?0:1);
舉例子:
int a=10;
int b=9;
int c=8;
//求三個變量的最大值
System.out.println(a>b?a:(b>c?b:c));
//嵌套的三目循環執行順序由外及裏
而單目三元表達式等價
int b;
boolean a = false;
if(a){
b = 0;
}else{
b = 1;
}
2. 生成一個隨機數
方法1:產生一個0到1的隨機數
System.out.println("Math.random()=" + Math.random());
// 結果是個double類型的值,區間爲[0.0,1.0)
int num = (int) (Math.random() * 3);
// 注意不要寫成(int)Math.random()*3,這個結果爲0,因爲
//(int)Math.random()*3等於((int)Math.random())*3
//先執行了強制轉換
方法2:
package baowenli;
import java.util.Random;
public class Lianxi1 {
public static void main(String[] args) {
/*想要獲取一個範圍內的隨機數(例如26,隨機數可能是0-25),首先需要一個種子(其實就是一個數值)。
每個種子會對應這個範圍內(0-26)的唯一的一個隨機數。
47這個種子在26這個範圍內,所對應的隨機數爲24,所以每次隨機得出的結果都爲24.
(注意:47在26這個範圍內對應的是24,這個是死的,固定的,無論你執行多少次,它還是24)
至於爲什麼種子47會對應24,這個涉及到java封裝的算法,有興趣可以深入瞭解。*/
Random r = new Random(46);
System.out.println("隨機數列爲:");
//第一個for循環
for (int i = 0; i < 5; i++) {
int ran1 = r.nextInt(10);
System.out.print(ran1+"\t");
}
//第二個for循環
// System.out.println();
// for (int i = 0; i < 5; i++) {
// int ran1 = r.nextInt(10);
// System.out.print(ran1+"\t");
// }
}
}
產生的隨機數列;
當把第一個for屏蔽,執行第二個for後
package com.wdzl.demo06;
import java.util.Random;
public class Lianxi1 {
public static void main(String[] args) {
/*想要獲取一個範圍內的隨機數(例如26,隨機數可能是0-25),首先需要一個種子(其實就是一個數值)。
每個種子會對應這個範圍內(0-26)的唯一的一個隨機數。
47這個種子在26這個範圍內,所對應的隨機數爲24,所以每次隨機得出的結果都爲24.
(注意:47在26這個範圍內對應的是24,這個是死的,固定的,無論你執行多少次,它還是24)
至於爲什麼種子47會對應24,這個涉及到java封裝的算法,有興趣可以深入瞭解。*/
Random r = new Random(46);
System.out.println("隨機數列爲:");
//第一個for循環
// for (int i = 0; i < 5; i++) {
// int ran1 = r.nextInt(10);
// System.out.print(ran1+"\t");
// }
//第二個for循環
System.out.println();
for (int i = 0; i < 5; i++) {
int ran1 = r.nextInt(10);
System.out.print(ran1+"\t");
}
}
}
**隨機數種子的作用:**第幾個位置產生的隨機數是固定的,不會更改。
//不帶種子的:Random r = new Random();
//帶種子的:Random r = new Random(5);
總結一下:
**Random的對象有兩種構建方式:
- 帶種子
- 不帶種子
不帶種子的方式將會返回隨機的數字,每次運行結果不一樣。無論程序運行多少次,帶種子方式構建的Random對象會返回一樣的結果。
Random() | 構成一個隨機數 |
---|---|
Random(long seed) | 用種子seed構造一個隨機數生成器 |
下面給幾個例子:
1. 生成[0,1.0)區間的小數:double d1 = r.nextDouble();
2. 生成[0,5.0)區間的小數:double d2 = r.nextDouble() * 5;
3. 生成[1,2.5)區間的小數:double d3 = r.nextDouble() * 1.5 + 1;
4. 生成100之間的整數:int n = r.nextInt(100);
5. 生成[0,10)區間的整數:
//方法一
int n2 = r.nextInt(10);
//方法二
n2 = Math.abs(r.nextInt() % 10);
3. switch語句
Jdk4版本之前,
Switch( )括號裏的值是int(包括 byte char short int)
jdk5版本 -----枚舉型
jdk7版本之後------是 string 型
default位置 可以任意,可以沒有default語句
4. Ascii碼值
0——48
A——65
a——97
5.循環3條件
- 初始化條件變量
- 條件表達式
- 改變循環條件
6. while
7. do-while;
8. for循環
這裏僅對for循環做強調
for循環的表達式一般如下:
for(表達式1;表達式2;表達式3){
表達式4;
}
執行的順序爲:
1)第一次循環,即初始化循環。
首先執行表達式1(一般爲初始化語句),再執行表達式2(一般爲條件判斷語句),判斷表達式1是否符合表達式2的條件,如果符合,則執行表達式4,否則,停止執行,最後執行表達式3.
2)下次的循環:
首先執行表達式2,判斷表達式3是否符合表達式2的條件;如果符合,繼續執行表達式4,否則停止執行,最後執行表達式3.如果往復,直到表達式3不再滿足表達式2的條件。
9.continue 和 break;
break和continue的區別問題
1. break是跳出循環體
2. continue是終止本次循環,繼續進入下次循環
break常用於switch結構和循環結構中
continue一般用於循環結構中
**相同點:跳出循環體後面語句的剩餘部分**
10.可變長參數類型
- 可變長參數類型:jdk1.5開始
public int add(int...num) {
int sum = 0;
for (int i = 0; i < num.length; i++) {
sum +=num[i];
}
return sum;
}
public float add1(float...num) {
float sum=0;
for (int i = 0; i < num.length; i++) {
sum += num[i];
}
return sum;
}
public String add2(String...num) {
String sum="";
for (int i = 0; i < num.length; i++) {
sum += num[i];
}
return sum;
}
public char add3(char...num){
char sum=' ';
for (char i = 0; i < num.length; i++) {
sum += num[i];
}
return sum;
}
11.無返回值的方法
- 可以出現return 但是不能返回具體的值
12.進制轉換:
//十進制轉爲其他進制
一個十進制數轉換爲二進制
一個十進制數轉換爲八進制
一個十進制數轉換爲十六進制
System.out.println(b10+"的二進制:"+Integer.toBinaryString(b10));
System.out.println(b10+"的八進制:"+Integer.toOctalString(b10));
System.out.println(b10+"的十六進制:"+Integer.toHexString(b10));
將數字類型的字符串轉換爲整形:
int number = Integer.parseInt("1234");
例題1:打印三角形
package progect1_2;
public class Lianxi7 {
public static void main(String[] args) {
for(int i = 0;i<4;i++) {
for(int j = 0;j< 4-i-1;j++) {
System.out.print(" ");
}
for(int j = 0;j< 2*(i+1)-1;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
例題2:打印對稱三角形
package diertian;
public class Zuoye11 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < 2*i-1; j++) {
System.out.print("*");
}
for (int j = 0; j < 41-2*(2*i); j++) {
System.out.print(" ");
}
for (int j = 0; j < 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
例題3:打印菱形
package progect1_2;
public class Lianxi8 {
public static void main(String[] args) {
for(int i = 0;i<4;i++) {
for(int j = 0;j<4-i-1;j++) {
System.out.print(" ");
}
for(int j = 0;j<2*(i+1)-1;j++) {
System.out.print("*");
}
System.out.println();
}
for(int i = 0;i<3;i++) {
for(int j = 0;j<i+1;j++) {
System.out.print(" ");
}
for(int j=0;j<5-2*i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
例題4:打印空心菱形
package progect1_2;
public class Lianxi9 {
public static void main(String[] args) {
//i的值決定上面半個菱形的行數
//更改1
for(int i = 0;i<15;i++) {
//這個for循環裏的4必須和上面的4對應
//更改2
for(int j =0;j<15-i-1;j++) {
System.out.print(" ");
}
for(int j =0;j<(2*(i+1)-1);j++ ) {
if(j>=1&&j<2*i) {
System.out.print("-");
}else {
System.out.print("*");
}
}
System.out.println();
}
//第二個for循環裏i的值比第一個i的值要小1
//更改3
for(int i = 0;i<14;i++) {
for(int j=0;j<i+1;j++) {
System.out.print(" ");
}
//注意:更改 這個for循環裏的7必須和上面for循環裏的 7對應
//更改4
for(int j = 0;j<(2*14-1)-2*i;j++) {
//更改5
if( j>=1&&j<((2*14-2)-2*i)) {
System.out.print("-");
}else {
System.out.print("*");
}
}
System.out.println();
}
}
}