问答详情
源自:12-2 项目问题解析 1

谁能帮我看一下运行错在哪?能运行出来,就是结果不理想。谢谢

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
  }

 }


提问者:林念志祥 2017-08-08 22:00

个回答

  • 丨小布丨
    2017-08-09 11:06:41

    People类里面的getNum 返回的值是0,把主函数里面的  int p = po.getNum();
      po.player();这两句话换一下位置就OK了;