用户类
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
|
import java.util.Scanner;
public class User {
String name;
int choice;
public int user() {
Scanner input = new Scanner(System.in);
System.out.print( "请出拳: 1.剪刀 2.石头 3.布 (输入相应的数字):" );
choice = input.nextInt();
switch (choice) {
case 1 :
name = "用户出拳:剪刀" ;
break ;
case 2 :
name = "用户出拳:石头" ;
break ;
case 3 :
name = "用户出拳:布" ;
break ;
default :
System.out.println( "输入有误!\t请重新输入:" );
user();
break ;
}
System.out.println(name);
return (choice);
}
} |
电脑类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Computer {
String name;
int random;
public int Computer(){
random=( int )(Math.random()* 3 + 1 );
switch (random) {
case 1 :
name= "出拳:剪刀" ;
break ;
case 2 :
name= "出拳:石头" ;
break ;
case 3 :
name= "出拳:布" ;
break ;
}
return (random);
}
} |
对战类
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
import java.util.Scanner;
public class Duel {
Scanner input = new Scanner(System.in);
User user = new User();
Computer computer = new Computer();
String username, Computer, Computername;
int userchoice, count1, count2, count3, count4;
public void Start() {
System.out.println( "\t**************************"
+ "\n\t**** 猜拳 , 开始 ****"
+ "\n\t**************************" );
System.out.println( "------------------------------------------" );
System.out.println( "出拳规则: 1.剪刀 2.石头 3.布" );
System.out.print( "请选择对方角色(1.刘备 2.孙权 3.曹操):" );
userchoice = input.nextInt();
switch (userchoice) {
case 1 :
Computer = "您选择了,刘备对战" ;
Computername = "刘备" ;
break ;
case 2 :
Computer = "您选择了,孙权对战" ;
Computername = "孙权" ;
break ;
case 3 :
Computer = "您选择了,曹操对战" ;
Computername = "曹操" ;
break ;
default :
System.out.println( "输入有误!\t请重新输入:" );
Start();
}
System.out.println(Computer);
System.out.print( "请输入您的昵称:" );
username = input.next();
System.out.println(username + " vs " + Computername);
System.out.print( "是否开始游戏(y / n):" );
String choice = input.next();
if (choice.equals( "y" )) {
Test();
} else if (choice.equals( "n" )) {
System.out.println( "谢谢使用!再见!" );
} else {
System.out.println( "输入错误!程序异常!" );
System.exit( 0 );
}
}
public void Test() {
System.out.println( "------------------------------------------" );
user.user();
computer.Computer();
System.out.println(Computername + computer.name);
if (user.choice == computer.random) {
System.out.println( "结果:和局 ,真衰(-__-)!" );
count1++;
} else if (user.choice == 1 && computer.random == 2 || user.choice == 2
&& computer.random == 3 || user.choice == 3
&& computer.random == 1 ) {
System.out.println( "结果:( ^_^ ),你输了,真笨!" );
count2++;
} else {
System.out.println( "结果: (︶︿︶),你赢了,再来一把啊!" );
count3++;
}
count4++;
System.out.println( "------------------------------------------" );
System.out.print( "\n小伙伴!想不想在玩一次! ^_^ (y / n):" );
String choice = input.next();
if (choice.equals( "y" )) {
Test();
} else if (choice.equals( "n" )) {
End();
} else {
System.out.println( "输入错误!程序异常!" );
System.exit( 0 );
}
}
public void End() {
System.out.println( "------------------------------------------" );
System.out.println(username + " vs " + Computername);
System.out.println( "对战次数:" + count4);
System.out.println( "平局次数:" + count1);
System.out.println( "\n昵称\t分数" );
System.out.println(username + "\t" + count3);
System.out.println(Computername + "\t" + count2);
if (count3 == count2) {
System.out.println( "\n结果:打成平手,下次再和你一分高下!(-__-)" );
} else if (count3 > count2) {
System.out.println( "\n结果:呵呵,有点不服,下次记得在来!(︶︿︶)" );
} else {
System.out.println( "\n结果:( ^_^ )呵呵!笨笨,下次加油哦" );
}
System.out.println( "------------------------------------------" );
System.exit( 0 );
}
} |
测试类
1
2
3
4
5
6
7
8
9
10
11
|
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Duel duel = new Duel();
duel.Start();
}
} |
本文转自 Y幕徐 51CTO博客,原文链接:http://blog.51cto.com/765133133/1419468