我正在尝试制作一个乐透的java程序。我提示用户输入他们的号码,然后生成一个随机数,如果你的号码匹配,你就赢了,但我得到的是“类型不匹配:无法从int转换为布尔值”。这是我到目前为止所拥有的。感谢您的帮助! 注意我是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");
}
}
答案 0 :(得分:0)
您需要将随机数存储在变量中并在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");
}
}
注意:我还修改了你的if语句。 (如果你得到56,你仍然是胜利者)