Skip to content

Instantly share code, notes, and snippets.

@Partoo
Last active December 15, 2015 11:28
Show Gist options
  • Select an option

  • Save Partoo/5253002 to your computer and use it in GitHub Desktop.

Select an option

Save Partoo/5253002 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package guess;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author partoo
*/
public class Guess {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n = 3; //定义生成随机数范围
Random random = new Random();
int indexNum = random.nextInt(n); //获取范围内的随机整数
String[] guessOption={"剪刀","石头","布"};
Scanner input=new Scanner(System.in);
int usrInput;//创建用户输入变量
String msg;//创建结果显示变量
msg = "";//初始化
while(true) {
try {
System.out.print("您要出的是?(0代表剪刀,1代表石头,2代表布)");
usrInput = input.nextInt();
if(usrInput >= 0 && usrInput <= 2) {
break;
}
System.out.println("** 您只能输入0~2之间的数字 **");
} catch (Exception e) {
System.out.println("** 格式错误!请输入数字 **");
input.next();
}
} //限定用户输入
switch (indexNum) {
case 0:
if (usrInput==0) {
msg = "平局";
}else if (usrInput==1) {
msg = "您赢了!";
}else if (usrInput==2) {
msg = "您输了!";
}
break;
case 1:
if (usrInput==0) {
msg = "您输了!";
}else if (usrInput==1) {
msg = "平局!";
}else if (usrInput==2) {
msg = "您赢了!";
}
break;
case 2:
if (usrInput==0) {
msg = "您赢了!";
}else if (usrInput==1) {
msg = "您输了!";
}else if (usrInput==2) {
msg = "平局!";
}
break;
}
System.out.println("电脑出的是:"+guessOption[indexNum]+",您输入的是:" + guessOption[usrInput]+","+msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment