林念志祥
2017-08-08 22:00
package com.leadingsoft;
public abstract class PlayC {
public abstract void player();
abstract int getNum();
}
package com.leadingsoft;
import java.util.Random;
public class Computer extends PlayC {
int re = 0;
@Override
public int getNum() {
Random r = new Random();
re = r.nextInt(3) + 1;
return re;// TODO Auto-generated method stub
}
@Override
public void player() {
if (re == 1) {
System.out.println("剪刀");// TODO Auto-generated method stub
} else if (re == 2) {
System.out.println("石头");
} else {
System.out.println("布");
}
}
}
package com.leadingsoft;
import java.util.Scanner;
public class People extends PlayC {
int num = 0;
@Override
public void player() {
System.out.println("请输入号码:");
Scanner input = new Scanner(System.in);
this.num = input.nextInt();
switch (num) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("石头");
break;
case 3:
System.out.println("布");
break;
}
}
@Override
public int getNum() {
return num;
}
}
package com.leadingsoft;
public class Zhu {
public static void main(String[] args) {
People po = new People();
int p = po.getNum();
po.player();
Computer co = new Computer();
int c = co.getNum();
co.player();
if (p == c) {
System.out.println("平局");
} else if ((c - p == 1) || (c - p == -2)) {
System.out.println("电脑赢了");
} else {
System.out.println("你赢了!!");// TODO Auto-generated method stub
}
}
People类里面的getNum 返回的值是0,把主函数里面的 int p = po.getNum();
po.player();这两句话换一下位置就OK了;
Java入门第二季 升级版
530553 学习 · 6091 问题
相似问题