public class Demo01 {
public static void main(String[] args) {
/*
* 你同桌和你要玩游戏.
* 1 剪刀,2 石头,3 布
*/
// 判断结果.
// 1 剪刀,2 石头,3 布
int a = 1;// 剪刀
int b = 2;// 石头
if (a == 1 && b == 3 || a == 2 && b == 1 || a == 3 && b == 2) {// 你赢了.
System.out.println("你赢了");
} else if (a == 1 && b == 2 || a == 2 && b == 3 || a == 3 && b == 1) {// 你输了
System.out.println("你输了");
} else {// 平局
System.out.println("平局");
}
// int a = 5;
// int b = 2;
// 5比2大.
}
}
package com.jh.test01;
import java.util.Scanner;
/**
*
* 用户名
* 属性: 姓名,积分.
* 功能:出拳的功能.
*/
public class User {
// 属性:
// 姓名
String name;
// 积分--分数
int score;
// 出拳的功能。
/*
* 1 剪刀,2 石头,3 布
* 1: 返回值类型。int
* 2:参数列表 -- 无
*/
/**
* "1 剪刀,2 石头,3 布"
* @return 出的拳
*/
public int userHand() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你出的小拳拳");
System.out.println("1 剪刀,2 石头,3 布");
int num = sc.nextInt();
// 等值判断
switch (num) {
case 1:
System.out.println("您输出了剪刀");
break;
case 2:
System.out.println("您输出了石头");
break;
case 3:
System.out.println("您输出了布");
break;
default:
break;
}
return num;
}
}
package com.jh.test01;
import java.util.Random;
/**
* 电脑类。 属性:姓名,积分 功能:出拳
*
* @author
*
*/
public class Computer {
// 属性:
// 姓名
String computerName;
// 积分
int computerScore;
// 出拳。
/*
* 1 剪刀,2 石头,3 布
* 1:返回值类型 。int
* 2:参数列表:无
*/
/**
* 1 剪刀,2 石头,3 布
* @return 电脑出的拳
*/
public int computerHand() {
// 生成1 -- 3的随机数。
Random r = new Random();
int num = r.nextInt(3) + 1;
// 根据生成的随机数值做等值判断,
// 根据规则输出对于出的什么东东。
// 等值判断
switch (num) {
case 1:
System.out.println("电脑输出了剪刀");
break;
case 2:
System.out.println("电脑输出了石头");
break;
case 3:
System.out.println("电脑输出了布");
break;
default:
break;
}
// 返回电脑出的拳。
return num;
}
}
package com.jh.test01;
public class Test {
public static void main(String[] args) {
// 创建User对象。
User user = new User();
int userHand = user.userHand();
System.out.println(userHand);
// 调用电脑出的拳
Computer computer = new Computer();
int computerHand = computer.computerHand();
System.out.println(computerHand);
}
}