ning_rookie 2021-10-19 15:43 采纳率: 50%
浏览 36
已结题

谁能帮我看一下这个代码吗,为什么不管我输入多大的数字,他永远显示电脑赢

import java.util.Scanner;
public class Dice {
static int player;

static int computer;

static String winner;
    public static void main(String[] args) {
Scanner input =new Scanner(System.in);
String player=input.next();//获取用户输入

computer = 1 + (int) ((Math.random() * 7));//电脑随机出数

diceRoll();

System.out.println("Computer rolled a " + computer);

System.out.println("And the winner is" + winner);

}

//根据数字判断输赢
public static void diceRoll() {
if(player > computer){
System.out.println("You won!");

winner = "Player";

}else{
System.out.println("You lost!");

winner = "Computer";

}

}

}
  • 写回答

4条回答 默认 最新

  • 享耳为郭 2021-10-19 16:02
    关注

    你的playe在判断方法里是没有值得 所以不可能大于电脑 你可以这样 player=input.nextInt() 将 static int player 的int 改为String 类型的 对比的时候转型就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月27日
  • 已采纳回答 10月19日
  • 创建了问题 10月19日

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决