gpt4 book ai didi

java - 如何制作pick 3彩票程序? (java)

转载 作者:行者123 更新时间:2023-12-02 02:11:34 25 4
gpt4 key购买 nike

在这里发表第一篇文章是因为我很沮丧。作业是创建一个程序,其中生成 0-9 的 3 个随机数,用户将输入 3 个数字。如果 1 个数字匹配,则用户赢得 10 美元,如果 2 个数字匹配,则赢得 100 美元,如果 3 个数字匹配,则赢得 1000 美元,如果全部 3 个数字匹配,则用户赢得 1000000 美元。由于某种原因,即使没有一个数字匹配,它也会奖励我 10 美元,并且还有其他问题。

我知道一定有更简单的方法。这是我的第一门 Java 类(class),所以我仍然是初学者,但这是我想出的代码:

public class TheLottery
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.println("enter a number from 0-9");
int num1 = input.nextInt();

System.out.println("enter a second number from 0-9");
int num2 = input.nextInt();

System.out.println("enter a third number from 0-9");
int num3 = input.nextInt();

Random r=new Random();
int a1=r.nextInt(10);

Random r1=new Random();
int a2=r.nextInt(10);

Random r2=new Random();
int a3=r.nextInt(10);

System.out.println("The winning numbers are "+ a1 + a2 + a3);
boolean winning1=true;
boolean winning2=true;
boolean winning3=true;

if (num1 == a1 || num1 == a2 || num1 == a3)
winning1 = true;
else
winning2 = false;

if (num2 == a1 || num2 == a2 || num2 == a3)
winning2 = true;
else
winning2 = false;

if (num3 == a1 || num3 == a2 || num3 == a3)
winning3 = true;
else
winning3= false;

if(winning1==true && winning2==true && winning3==true && num1 == a1 && num2==a2 && num3==a3)
System.out.println("YOU'RE A MILLIONAIRE!");
else if (winning1==true && winning2==true && winning3==true)
System.out.println("You win 1,000 dollars!");
else if ((winning1==true && winning2==true) || (winning1==true && winning3==true) ||(winning2==true && winning3==true))
System.out.println("You win 100 dollars!");
else if (winning1==true || winning2==true || winning3==true)
System.out.println("You win 10 dollars!");
else
System.out.println("You lose!");
}
}

最佳答案

@Adam S 指出了第一个问题。您在第一次尝试检查时分配了错误的 boolean 变量。

如果从一开始就记录胜利,而不是成功的尝试,事情会更容易。

        int wins = 0;

if (num1 == a1 || num1 == a2 || num1 == a3)
wins++;
if (num2 == a1 || num2 == a2 || num2 == a3)
wins++;
if (num3 == a1 || num3 == a2 || num3 == a3)
wins++;

if(wins == 3)
if(num1 == a1 && num2==a2 && num3==a3)
System.out.println("YOU'RE A MILLIONAIRE!");
else
System.out.println("You win 1,000 dollars!");
else if (wins == 2)
System.out.println("You win 100 dollars!");
else if (wins == 1)
System.out.println("You win 10 dollars!");
else
System.out.println("You lose!");

关于java - 如何制作pick 3彩票程序? (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49931647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com