简体   繁体   English

类型不匹配:无法在乐透程序中从 int 转换为 boolean

[英]type mismatch: cannot convert from int to boolean in lotto program

I am trying to make a lotto java program.我正在尝试制作一个乐透 Java 程序。 I prompt the user to input their number and then generate a random number and if your number matches, you win but i'm getting the "type mismatch: cannot convert from int to boolean."我提示用户输入他们的号码,然后生成一个随机数,如果你的号码匹配,你就赢了,但我得到了“类型不匹配:无法从 int 转换为布尔值”。 Here's what I have so far.这是我到目前为止所拥有的。 Thank you for the help!感谢您的帮助! Note i'm very new to java请注意,我对 Java 很陌生

import java.util.Scanner;
import java.util.Random;
public class lottery
{
  public static void main (String [] args)
  {
    Scanner input=new Scanner(System.in);
    System.out.println("Please enter your number");
    Random randomNumber=new Random();
    System.out.println(" my random number is "+65);
    System.out.println("lotto number is "+randomNumber.nextInt(10)+1);
    if(65)
      System.out.println("You win 20000");
    else 
      Sytem.out.println("No winner");
    if(56)
      System.out.println("You win 10000");
    else
      System.out.println("No winner");
    if(6||5)
      System.out.println("You win 5000");
    else
      System.out.println("No winner");

  }
}

You need to store the random number in a variable and compare it in the if statements:您需要将随机数存储在变量中并在 if 语句中进行比较:

import java.util.Scanner;
import java.util.Random;
public class lottery
{
  public static void main (String [] args)
  {
    Scanner input=new Scanner(System.in);
    System.out.println("Please enter your number");
    Random randomNumber=new Random();
    int rand = randomNumber.nextInt(10) + 1
    System.out.println(" my random number is "+65);
    System.out.println("lotto number is "+ rand);
    if(rand == 65)
      System.out.println("You win 20000");
    else if(rand == 56)
      System.out.println("You win 10000");
    else if(rand == 6 || rand == 5)
      System.out.println("You win 5000");
    else
      System.out.println("No winner");
  }
}

Note: I fixed up your if statements a bit aswell.注意:我也稍微修正了你的 if 语句。 (You're still a winner if you get 56 right) (如果你答对了 56,你仍然是赢家)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM