类、属性、方法
方法
public
public void 方法名(){
方法体
}
//void:无返回
方法是一个行为,需要返回/无返回
private
返回值
返回值练习
arr+if+return
//注意运算类型
if+return
属性、方法、返回值练习
分析
//Person.java
package test03;
import java.util.Scanner;
public class Person {
public String name;
public int score;
Scanner input = new Scanner(System.in);
//出拳方法
public int showFist() {
int fist = 0;
while(true) {
//控制台输入
System.out.println("请出拳:1.石头 2.剪刀 3.布");
fist = input.nextInt();
if(fist == 1) {
System.out.println(name + "出拳:石头");
break;
}else if(fist == 2) {
System.out.println(name + "出拳:剪刀");
break;
}else if(fist == 3) {
System.out.println(name + "出拳:布");
break;
}else {
System.out.println("输入错误,重输!");
}
}
return fist;
}
}
//Computer.java
package test03;
public class Computer {
public String name;
public int score;
public int showFist() {
int fist = (int)(Math.random() * 3 + 1);
if(fist == 1) {
System.out.println(name + "出拳:石头");
}else if(fist == 2) {
System.out.println(name + "出拳:剪刀");
}else if(fist == 3) {
System.out.println(name + "出拳:布");
}else {
System.out.println("请重输");
}
return fist;
}
}
//Manager.java
package test03;
import java.util.Scanner;
public class Manager {
Scanner input = new Scanner(System.in);
Person per = new Person(); //?
Computer com = new Computer();
//让谁出拳
int perFist = 0;
int comFist = 0;
//临时存储
//初始化方法
public void init() {
System.out.println("****************************");
System.out.println("**********人机猜拳比赛*******");
System.out.println("****************************");
System.out.println("请输入你的名字:");
per.name = input.next();
while(true) {
System.out.println("请输入你的对手:1.张三 2.李四 3.王五");
int result = input.nextInt();
if(result == 1) {
com.name = "张三";
break;
}else if(result == 2) {
com.name = "李四";
break;
}else if(result == 3) {
com.name = "王五";
break;
}else {
System.out.println("输入错误,重新输入!");
}
}
System.out.println(per.name + " VS " + com.name);
start();
}
//开始比赛
public void start() {
String choice = "y";
while(choice.equals("y")) {
perFist = per.showFist();
comFist = com.showFist();
chack();
System.out.println("是否继续?(y/n)");
choice = input.next();
}
showResult();
}
//判定每局结果
public void chack() {
if(perFist ==1 && comFist == 2 || perFist == 2 && comFist == 3 || perFist == 3 && comFist == 1) {
System.out.println("恭喜你赢了一局");
per.score ++;
}else if(perFist == comFist) {
System.out.println("平局");
}else {
System.out.println("很遗憾," + com.name + "输了一局");
com.score++;
}
}
//宣布比赛结果
public void showResult() {
System.out.println("****************************");
System.out.println(per.name + "" + per.score);
System.out.println(per.name + "" + per.score);
if(per.score > com.score) {
System.out.println("恭喜你赢得了最终的胜利");
}else if(per.score == com.score) {
System.out.println("两位选手不相上下");
}else {
System.out.println("很遗憾" + com.name + "赢得了最终的胜利");
}
System.out.println("****************************");
}
}
//Test.java
package test03;
public class Test {
public static void main(String[] args) {
// Person per = new Person();
// int fist = per.showFist();
// System.out.println(fist);
// Computer com = new Computer();
// int fist = com.showFist();
// System.out.println(fist);
Manager mgr = new Manager();
mgr.init();
}
}