Java实验二

《Java程序设计》课程第二周实验题目和代码。

第一题

实现用户从键盘上输入 3 个 1-100 的整数;每输入一次,检查当 前输入是否为有效输入,直到有效次数为 3 结束程序,统计三次 有效输入和输出;要求能够给出无效提示和正确输入次数提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//实现用户从键盘上输入 3 个 1-100 的整数;每输入一次,检查当 前输入是否为有效输入,直到有效次数为 3 结束程序,统计三次 有效输入和输出;要求能够给出无效提示和正确输入次数提示。
import java.util.Scanner;
import java.util.InputMismatchException;

public class ValidCount {

public static void main(String []args) {
//定义变量
Scanner sc = new Scanner(System.in);
int count=0,n=-1;
int []a= new int[3];
boolean k;

//统计三次 有效输入和输出
while(count!=3) { //循环结束条件:有效输入为3次

System.out.println("请输入1-100的整数:");

try {
n = sc.nextInt(); //接收从控制台的数据
if (n>=1 && n<=100) { //若为1-100之间的整数,则count+1
a[count]=n;
count++;
System.out.println("正确输入"+count+"次");
}
else
System.out.println("输入的不是1-100的整数!");

}
//捕获输入异常
catch(InputMismatchException e){
System.out.println("输入异常:"+e.toString());
sc.next();
}

}
System.out.println("输入的有效数是:"+a[0]+" 和 "+a[1]+" 和 "+a[2]);
}

}

第二题

输出 1-100 之内前 5 个能被 3 整除的数;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 输出 1-100 之内前 5 个能被 3 整除的数; 

public class ZhengChu {

public static void main(String []args) {

int i,count=0;
for(i=1;i<=100;i++){
if(i%3==0&&count<5) {
System.out.println(" "+i+ " ");
count++;
}

}
}

}

第三题

请按照下图剪刀石头布的游戏规则,采用 switch 分支结构实现人 与机器进行剪刀石头布游戏。游戏规则和信息提示如下:
(1) 每次游戏时,程序提示从命令行输入 1、2 或 3,分别作为 剪刀、石头和布; (2) 机器由程序随机生成剪刀、石头或布等三种中的一种;
(3) 根据游戏规则(石头赢剪刀,剪刀赢布,布赢石头),输出人与机器游戏的结果;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//请按照下图剪刀石头布的游戏规则,采用 switch 分支结构实现人 与机器进行剪刀石头布游戏。游戏规则和信息提示如下: (1) 每次游戏时,程序提示从命令行输入 1、2 或 3,分别作为 剪刀、石头和布; (2) 机器由程序随机生成剪刀、石头或布等三种中的一种; (3) 根据下图游戏规则,输出人与机器游戏的结果; 
import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors{

public static void main(String []args) {
//定义变量
int n=-1,m;
Scanner sc= new Scanner(System.in);
Random rand = new Random();

System.out.println("请输入1,2,3,分别代表剪刀,石头,布");

while(sc.hasNextInt()) { //输入end结束游戏

m=rand.nextInt(3)+1; //生成1,2,3随机数
n=sc.nextInt(); //读取控制台数据

//用户的猜拳情况
switch(n) {
case 1:
System.out.println("您出剪刀");break;
case 2:
System.out.println("您出石头");break;
case 3:
System.out.println("您出布");break;
}

//电脑的猜拳情况,即随机数
switch(m) {
case 1:
System.out.println("电脑出剪刀");break;
case 2:
System.out.println("电脑出石头");break;
case 3:
System.out.println("电脑出布");break;
}

//随机数与控制台数据比较
if(m==n)
System.out.println("旗鼓相当,平局!");
if(m==n+1||n==m+2)
System.out.println("很遗憾,您输了!");
if(n==m+1||m==n+2)
System.out.println("您赢了!");
System.out.println("退出游戏,请输入end");
}
System.out.println("已退出游戏!");
}
}

第四题

请在 Eclipse IDE 开发环境下输入如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test { 
public static void main(String args[ ]) {
int count= 0 ;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if ( i==1 && j==1 )
break ;
count++;
}
}
System.out.println(count);
}
}

(1) 请测试输出结果,并解释为何如此?
(2) 将程序中黑体的 break 替换为 continue,请测试输出结果, 并解释为何如此?

(1)break若在双层循环中,则是跳出内层循环,继续执行外层循环。
在本例中则是执行了i=0,j=01,2,;i=1,j=0;i=2,j=0,1,2; 共3+1+3=7次
验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Test {
public static void main(String args[ ]) {
int count= 0 ;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if ( i==1 && j==1 )
break;
count++;
System.out.println("i="+i+" , "+"j="+j);
}
}
System.out.println(count);
}

}

(2)continue在退出循环时,只是不执行当前的这一次循环后面的内容,不会跳出内层循环。
结合本例来看,除了i=1,j=1这次循环不执行以外,其他都执行,故次数为9-1=8。

验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Test {
public static void main(String args[ ]) {
int count= 0 ;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if ( i==1 && j==1 )
continue;
count++;
System.out.println("i="+i+" , "+"j="+j);
}
}
System.out.println(count);
}

}

打赏
  • © 2025 Aoxue
  • Hexo Theme Ayer by shenyu
    • PV:
    • UV:

请我喝杯咖啡吧~

支付宝
微信