一、猜拳游戏:
package com.lovo; import java.util.Scanner; public class GuessGesticulate { public static void main(String[] args) { boolean goon = true; int me = 0; Scanner sc = new Scanner(System.in); Gesticulate ge = new Gesticulate(); do { do { System.out.print("1、石头 2、剪刀 3、布 你想出哪个?"); if (sc.hasNextInt()) { me = sc.nextInt(); } else { System.out.print("无效输入"); sc.nextLine(); } } while (me < 1 && me > 3); goon = ge.contrast(me); System.out.println(ge.getHint()); } while (goon); } }
二、猜数字游戏:
package com.lovo; public class Gesticulate { private int systemGesticulate = 0; private String hint = ""; public Gesticulate(){ this.random(); } public boolean contrast(int gesture) { if(gesture == systemGesticulate){ random(); if(gesture == 1){ hint="电脑也是石头,平局!!!"; }else if (gesture == 2){ hint="电脑也是剪刀,平局!!!"; }else{ hint="电脑也是布,平局!!!"; } }else if(systemGesticulate == 1){ if(gesture == 3){ hint ="电脑是石头,你赢了"; return false; }else if(gesture == 2){ hint ="电脑是石头,你输了"; return false; } }else if(systemGesticulate == 2){ if(gesture == 3){ hint ="电脑出的剪刀,你输了"; return false; }else if(gesture == 1){ hint ="电脑是剪刀,你赢了"; return false; } }else if(systemGesticulate == 3){ if(gesture == 1){ hint ="电脑是布,你输了"; return false; }else if(gesture == 2){ hint ="电脑是布,你赢了"; return false; } } return true; } public void random(){ this.systemGesticulate =(int) ( Math.random()*3+1 ); } public String getHint(){ return hint; } }
时间: 2024-10-21 10:18:08