···
/**
java實現(xiàn)人機猜拳游戲
-
人和電腦分別出剪刀铃彰、石頭趴俘、布蜗巧,直到人戰(zhàn)勝電腦掌眠,游戲結束
*/
public class caiquan {
public static void main(String[] args) {while (true) { System.out.println("*******************************"); System.out.println("--------歡迎進入猜拳游戲--------"); System.out.println("請出拳:(1是剪刀,2是石頭幕屹,3是布)"); Scanner sc=new Scanner(System.in); int person=sc.nextInt(); //獲取用戶輸入 int computer=(int)(Math.random()*3)+1; //電腦隨機出拳 String per="用戶"; String com = "電腦"; //用戶出拳 switch(person){ case 1: per="剪刀"; break; case 2: per="石頭"; break; case 3: per="布"; break; } //電腦出拳 switch(computer){ case 1: com="剪刀"; break; case 2: com="石頭"; break; case 3: com="布"; break; } //根據出拳判斷輸贏 if(person==1&&computer==3||person==2&&computer==1||person==3&&computer==2){ System.out.println("你出的是("+per+") 電腦出的是("+com+")"); System.out.println(" 【你輸了蓝丙!再來一次吧】"); //System.out.println(); }else if (person==computer){ System.out.println("你出的是("+per+") 電腦出的是("+com+")"); System.out.println(" 【平局级遭!再來一次吧】"); // System.out.println(); }else{ System.out.println("你出的是("+per+") 電腦出的是("+com+")"); System.out.println(" 【恭喜你贏了!C斐尽挫鸽!】"); System.out.println("【你終于戰(zhàn)勝了電腦,游戲結束沧烈!】");; break; } }
}
}
···