1. 程式人生 > >Java初學者程式設計練習錦集(1)

Java初學者程式設計練習錦集(1)

Java程式設計練習:

1. 下面有一串字串,處理後讓其倒序輸出。

原字串:i am a student

輸出:tneduts a ma i

程式碼如下:

public class StringPractic {

}

輸出結果入下:

tneduts a ma i

2.下面的一串字元,請統計其中‘0’到‘9’的各個數字的個數。 原字串:91254782354987012345978

輸出:0:1 1:2 2:3 3:2 4:3 5:3 7:3 8:3 9:3

程式程式碼如下:

public class Digit {

}

執行結果如下:

0:1 1:2 2:3 3:2 4:3 5:3 6:0 7:3 8:3 9:3 public static void main(String args[]){ } String s = "91254782354987012345978"; int a[] = new int[10]; for(int i=0;i<s.length();i++){ } for(int j=0;j<10;j++) System.out.printf("%d:%d ",j, a[j]); a[s.charAt(i)-'0']++; public static void main(String args[]){ } String s = "i am a student"; for(int i=s.length()-1;i>=0;i--){ System.out.print(s.charAt(i)); }

3.public class New {

}

執行結果:

使用後置運算子的結果為:1

使用前置運算子的結果為:2 public static void main(String args[]){ int a = 1; int b = 1; System.out.println("使用後置運算子的結果為:"+(a++)); System.out.println("使用前置運算子的結果為:"+(++b)); }

4.public class New {

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;


2*2=4 2*1=2

1*1=1

6.用for語句修改9*9乘法表:

public class New {

} public static void main(String args[]){ for(int i=1;i<10;i++){ } for(int j=1;j<10;j++){ } System.out.println(" "); if(j<=i){ } System.out.print(i+"*"+j+"="+(i*j)+" "); }

執行結果如下:

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36

7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49

8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64

9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

7.等邊三角形:

public class New {

}

執行結果如下:

1

212 public static void main(String args[]){ char s[] = {' ',' ',' ',' ',' ',' ',' ',' ',' ','1',' ',' ',' ',' System.out.println(s); for(int i=2;i<10;i++){ } s[0]='*'; s[18]='*'; System.out.println(s); s[10-i]=(char)('0'+i); s[8+i]=(char)('0'+i); System.out.println(s); ',' ',' ',' ',' ',' '}; }

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;


32123

4321234

543212345

65432123456

7654321234567

876543212345678

98765432123456789

*98765432123456789*

8.用for改寫如下:

public class Old {

public static void main(String args[]){ } for(int i=1;i<10;i++){ for(int j=1;j<11-i;j++){ } for(int s=i;s>0;s--){ } for(int s=2;s<i+1;s++){ } System.out.println(); System.out.print(s); System.out.print(s); System.out.print(' '); } System.out.println("*98765432123456789*"); }

執行結果:

1

212

32123

4321234

543212345

65432123456

7654321234567

876543212345678

98765432123456789

*98765432123456789*

9.用for迴圈為陣列賦值

程式碼如下:

public class Xunhuan {

public static void main(String args[]){ int a[] = new int[10]; for(int i=0;i<a.length;i++){ a[i]=i+1;

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;




} } System.out.println("陣列中各個元素的值為:"+a[i]); }

執行結果:

陣列中各個元素的值為:1

陣列中各個元素的值為:2

陣列中各個元素的值為:3

陣列中各個元素的值為:4

陣列中各個元素的值為:5

陣列中各個元素的值為:6

陣列中各個元素的值為:7

陣列中各個元素的值為:8

陣列中各個元素的值為:9

陣列中各個元素的值為:10

10.利用陣列列印26個引文字母:

public class Array {

}

執行結果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ結束 public static void main(String args[]){ char[] a; a = new char[26]; for(int i=0;i<26;i++){ } } a[i] = (char)('A'+i); System.out.print(a[i]); if(a[i]=='Z'){ System.out.println("結束"); }

11.火山機器人程式:

class VolcanoRobot{

String status; int speed; float temperature; void checkTemperature(){ } void showAttributes(){ if(temperature > 660){ } status = "returning home"; speed = 5;

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;


} } System.out.println("陣列中各個元素的值為:"+a[i]); }

執行結果:

陣列中各個元素的值為:1

陣列中各個元素的值為:2

陣列中各個元素的值為:3

陣列中各個元素的值為:4

陣列中各個元素的值為:5

陣列中各個元素的值為:6

陣列中各個元素的值為:7

陣列中各個元素的值為:8

陣列中各個元素的值為:9

陣列中各個元素的值為:10

10.利用陣列列印26個引文字母:

public class Array {

}

執行結果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ結束 public static void main(String args[]){ char[] a; a = new char[26]; for(int i=0;i<26;i++){ } } a[i] = (char)('A'+i); System.out.print(a[i]); if(a[i]=='Z'){ System.out.println("結束"); }

11.火山機器人程式:

class VolcanoRobot{

String status; int speed; float temperature; void checkTemperature(){ } void showAttributes(){ if(temperature > 660){ } status = "returning home"; speed = 5;


} System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + temperature);

public static void main(String args[]){

}

}

執行結果:

Temperature: 510.0

Incresing speed to 3.

Status: exploring

Speed: 3

Temperature: 510.0

Changing temperature to 670.

Status: exploring

Speed: 3

Temperature: 670.0

Checking the temperature.

Status: returning home

Speed: 5

Temperature: 670.0 VolcanoRobot dante = new VolcanoRobot(); dante.status = "exploring"; dante.speed = 2; dante.temperature = 510; dante.showAttributes(); System.out.println("Incresing speed to 3."); dante.speed = 3; dante.showAttributes(); System.out.println("Changing temperature to 670."); dante.temperature = 670; dante.showAttributes(); System.out.println("Checking the temperature."); dante.checkTemperature(); dante.showAttributes();

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;


} } System.out.println("陣列中各個元素的值為:"+a[i]); }

執行結果:

陣列中各個元素的值為:1

陣列中各個元素的值為:2

陣列中各個元素的值為:3

陣列中各個元素的值為:4

陣列中各個元素的值為:5

陣列中各個元素的值為:6

陣列中各個元素的值為:7

陣列中各個元素的值為:8

陣列中各個元素的值為:9

陣列中各個元素的值為:10

10.利用陣列列印26個引文字母:

public class Array {

}

執行結果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ結束 public static void main(String args[]){ char[] a; a = new char[26]; for(int i=0;i<26;i++){ } } a[i] = (char)('A'+i); System.out.print(a[i]); if(a[i]=='Z'){ System.out.println("結束"); }

11.火山機器人程式:

class VolcanoRobot{

String status; int speed; float temperature; void checkTemperature(){ } void showAttributes(){ if(temperature > 660){ } status = "returning home"; speed = 5;


java免費學習直播公開課思維交流群:175161984(←長按可複製)獲取學習資料可

} public static void main(String args[]){ } int a = 3; int b = 4; System.out.println("使用條件運算子顯示"); String s = (a<b)?"a小於b":"a大於b"; System.out.println(s); System.out.println("使用if條件語句顯示"); if(a<b){ System.out.println("a小於b"); } else{ System.out.println("a大於b"); }

執行結果:

使用條件運算子顯示

a小於b

使用if條件語句顯示

a小於b

5.9*9乘法表

public class New {

}

執行結果如下:

9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9 8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8 7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7

6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6

5*5=25 5*4=20 5*3=15 5*2=10 5*1=5

4*4=16 4*3=12 4*2=8 4*1=4

3*3=9 3*2=6 3*1=3 public static void main(String args[]){ } int i = 9; int j = 9; while(i>=1){ } while((j<=i)&&(j>0)){ } System.out.println(" "); i--; j = i; System.out.print(i+"*"+j+"="+i*j+" "); j--;

} } System.out.println("陣列中各個元素的值為:"+a[i]); }

執行結果:

陣列中各個元素的值為:1

陣列中各個元素的值為:2

陣列中各個元素的值為:3

陣列中各個元素的值為:4

陣列中各個元素的值為:5

陣列中各個元素的值為:6

陣列中各個元素的值為:7

陣列中各個元素的值為:8

陣列中各個元素的值為:9

陣列中各個元素的值為:10

10.利用陣列列印26個引文字母:

public class Array {

}

執行結果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ結束 public static void main(String args[]){ char[] a; a = new char[26]; for(int i=0;i<26;i++){ } } a[i] = (char)('A'+i); System.out.print(a[i]); if(a[i]=='Z'){ System.out.println("結束"); }

11.火山機器人程式:

class VolcanoRobot{

String status; int speed; float temperature; void checkTemperature(){ } void showAttributes(){ if(temperature > 660){ } status = "returning home"; speed = 5;