本程序使用到了if else和switch语句:
1: import java.util.Scanner;2: public class 猜拳 {3: public static void main(String[] args) {4: System.out.println(" ★★★★猜拳★★★★");5: System.out.print("请出拳(1、剪刀,2、石头,3、布):");6: Scanner in=new Scanner(System.in);7: int People=in.nextInt();8: int Computer=(int)(Math.random()*3)+1;9: String MarkPeople="人";10: String MarkComputer="电脑";11: switch(People){//和以下的switch一样,为了输出什么拳头12: case 1:MarkPeople="剪刀";13: break;14:15: case 2:MarkPeople="石头";16: break;17:18: case 3:MarkPeople="布";19: break;20: }21: switch(Computer){22: case 1:MarkComputer="剪刀";23: break;24:25: case 2:MarkComputer="石头";26: break;27:28: case 3:MarkComputer="布";29: break;30: }31: //以下if(***部分)是出拳算法32: if(People==Computer){33: System.out.println("\n☆平局☆\n"+" 你出的是:"+MarkPeople+",电脑出的是:"+MarkComputer);34: }35: else if(People==1&&Computer==3||People==2&&Computer==1||People==3&&Computer==2){36: System.out.println("\n☆你赢了☆\n"+" 你出的是:"+MarkPeople+",电脑出的是:"+MarkComputer);37: }else{38: System.out.println("\n☆你输了☆\n"+" 你出的是:"+MarkPeople+",电脑出的是:"+MarkComputer);39: }40: }41: }
***部分为出拳的比较!从“人”的出拳输赢及平局来做三个if!
一个简单的猜拳游戏
时间: 2024-12-18 03:12:52