javabean的剪刀石头布游戏
1个回答
展开全部
闲着没事,改了下,类分好了,随机数产生,修改了。望lz多思考。
import java.util.Scanner;
class Person {
int n = 0;
public int input() {
System.out.println("请输入:石头,剪刀,布");
Scanner sc = new Scanner(System.in);
String s = sc.next();
if(s.equals("石头")){
n = 1;
}else if(s.equals("剪刀")){
n = 2;
}else if(s.equals("布")){
n = 3;
}else if(s.equals("exit")){
System.out.print("系统退出了");
System.exit(0);
}
return n;
}
}
class Computer {
public int random() {
int h = (int)(Math.random()*3+1);
return h;
}
}
public class Game {
public void result(int n,int m){
if(n == m){
System.out.println("平了");
}else{
if(m==1){
if(n==2){
System.out.println("你输了");
}else if(n ==3){
System.out.println("你赢了");
}
}else if(m == 2) {
if(n == 1){
System.out.println("你赢了");
}else if(n ==3){
System.out.println("你输了");
}
}else if( m ==3 ){
if(n==1){
System.out.println("你输了");
}else if(n == 2){
System.out.println("你赢了");
}
}
}
}
public static void main(String[] args) {
while(true){
Person p = new Person();
Computer c = new Computer();
Game g = new Game();
g.result(p.input(), c.random());
}
}
}
import java.util.Scanner;
class Person {
int n = 0;
public int input() {
System.out.println("请输入:石头,剪刀,布");
Scanner sc = new Scanner(System.in);
String s = sc.next();
if(s.equals("石头")){
n = 1;
}else if(s.equals("剪刀")){
n = 2;
}else if(s.equals("布")){
n = 3;
}else if(s.equals("exit")){
System.out.print("系统退出了");
System.exit(0);
}
return n;
}
}
class Computer {
public int random() {
int h = (int)(Math.random()*3+1);
return h;
}
}
public class Game {
public void result(int n,int m){
if(n == m){
System.out.println("平了");
}else{
if(m==1){
if(n==2){
System.out.println("你输了");
}else if(n ==3){
System.out.println("你赢了");
}
}else if(m == 2) {
if(n == 1){
System.out.println("你赢了");
}else if(n ==3){
System.out.println("你输了");
}
}else if( m ==3 ){
if(n==1){
System.out.println("你输了");
}else if(n == 2){
System.out.println("你赢了");
}
}
}
}
public static void main(String[] args) {
while(true){
Person p = new Person();
Computer c = new Computer();
Game g = new Game();
g.result(p.input(), c.random());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询