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日