java实现剪⼑⽯头布_JAVA⼩游戏代码(剪⼑⽯头布)/*
* 创建⼀个类Game,⽯头,剪⼑,布的游戏。
*/
public class Game {
/**
* @param args
*/
String[] s ={"⽯头","剪⼑","布"};
//获取电脑出拳
String getComputer(int i)
{
String computerGuess = s[i];
return computerGuess;
}
//判断⼈出拳是否为⽯头,剪⼑,布
boolean isOrder(String guess)
{
boolean b = false;
for(int x = 0;x < s.length; x++)
{
if(guess.equals(s[x]))
{
b = true;
break;
}
}
return b;
}
//⽐较
void winOrLose(String guess1,String guess2)
{
if(guess1.equals(guess2))
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。平了");
}else if(guess1.equals("⽯头"))
{
if(guess2.equals("剪⼑"))
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。You Win!"); }else
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。You Lose!"); }
}else if(guess1.equals("剪⼑"))
{
if(guess2.equals("布"))
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。You Win!"); }else
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。You Lose!"); }
}else if(guess1.equals("布"))
{
简单的java游戏代码if(guess2.equals("⽯头"))
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。You Win!"); }else
{
System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。You Lose!"); }
}
}
public static void main(String[] args) {
Game my = new Game();
Game computer = new Game();
Game game = new Game();
String myGuess = javax.swing.JOptionPane.showInputDialog("输⼊⽯头,剪⼑,布:"); String computerGuess = Computer((int)(Math.floor(Math.random()*3))); if(my.isOrder(myGuess))
{
game.winOrLose(myGuess, computerGuess);
}else
{
System.out.println("你输⼊的不是⽯头,剪⼑,布!");
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。