Java初学者编程练习锦集 1

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

               

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;

           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值