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

来源:12-2 项目问题解析 1

林念志祥

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
  }

 }


写回答 关注

1回答

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

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

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530650 学习 · 6091 问题

查看课程

相似问题