基础班学习的基础题 运用判断语句,循环语句,scanner,system.in键盘录入,静态方法调用,输出语句,random随机数据,ArrayList集合等基础知识完成与电脑石头剪刀布的小游戏。 完成题目的体会: 感觉到了java语言的魅力,直接把我对java语言的兴趣值提到了高潮。感觉自己很有成就感,虽然完成了一个小程序游戏,而且只能在控制台里面显示,但是呢我就很开心。我会好好地学习java这门语言了,它的魅力深深地打动着我。我会努力加油的!!!冲鸭!~
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
text8();
}
public static void text8() {
ArrayList<String> list1 = new ArrayList<>();
list1.add("石头");
list1.add("剪刀");
list1.add("布");
Random random = new Random();
int pc = random.nextInt(3);
String pc1 = "";
if (pc == 0) {
pc1 = "石头";
} else if (pc == 1) {
pc1 = "剪刀";
} else if (pc == 2) {
pc1 = "布";
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("剪刀石头布游戏(石头0,剪刀1,布2)");
System.out.println("请出手:");
int sc = scanner.nextInt();
String sc1 = "";
if (sc == 0) {
sc1 = "石头";
} else if (sc == 1) {
sc1 = "剪刀";
} else if (sc == 2) {
sc1 = "布";
} else {
sc1 = "放弃比赛";
}
System.out.println("你 " + sc1);
System.out.println("电脑 " + pc1);
if (sc == pc) {
System.out.println("平局");
} else if ((sc == 0 && pc == 1) || (sc == 1 && pc == 2) || (sc == 2 && pc == 0)) {
System.out.println("你赢了");
} else if ((sc == 1 && pc == 0) || (sc == 2 && pc == 1) || (sc == 0 && pc == 2)) {
System.out.println("你输了");
} else {
System.out.println("电脑获胜!");
}
System.out.println("是否继续游戏:(继续:0/任意键结束游戏)");
int s1 = scanner.nextInt();
if (s1 != 0) {
System.out.println("游戏游戏结束!");
break;
} else {
System.out.println("游戏继续!");
}
}
}
}
|