为了账号安全,请及时绑定邮箱和手机立即绑定

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

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 回答

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

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信