gpt4 book ai didi

java - 创建彩票

转载 作者:行者123 更新时间:2023-12-01 06:25:24 26 4
gpt4 key购买 nike

生成三位数的彩票。程序提示用户输入一个三位数根据以下规则判断用户是否获胜:(规则1)。如果用户输入的彩票号码与抽奖号码的顺序完全匹配,则奖励为10,000 美元。(规则 2)。如果用户输入的所有数字都与彩票号码中的所有数字匹配,则奖励为3,000 美元。(规则 3)。如果用户输入的一位数字与彩票号码中的一位数字匹配,则奖励为1,000 美元。

我需要帮助让我的程序按照代码中看到的所有内容正常运行。没有数组,没有字符串,除了已经存在的东西之外什么都没有。我的问题是,我无法猜测 110 来忽略规则 2。我将其设置为 100 来测试所有规则。

import java.util.Scanner;

public class NewClass {
public static void main(String[] args) {
int lottery = 100;

// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits): ");
int guess = input.nextInt();

// Get digits from lottery
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = (lottery % 100) / 10;
int lotteryDigit3 = lottery % 10;

// Get digits from guess
int guessDigit1 = guess / 100;
int guessDigit2 = (guess % 100) / 10;
int guessDigit3 = guess % 10;

System.out.println("The lottery number is " + lottery);

// RULE1 Check the guess
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
// RULE2
else if ((guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3)
&& (guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3)
&& (guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3))
System.out.println("Match all digits: you win $3,000");
// RULE3
else if ((guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3)
|| (guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3)
|| (guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3))
System.out.println("Match one digit: you win $1,000");

else
System.out.println("Sorry, no match");
}
}

更新:

import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
int lottery = 456;

// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits): ");
int guess = input.nextInt();

// Get digits from lottery
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = (lottery % 100) / 10;
int lotteryDigit3 = lottery % 10;

// Get digits from guess
int guessDigit1 = guess / 100;
int guessDigit2 = (guess % 100) / 10;
int guessDigit3 = guess % 10;

System.out.println("The lottery number is " + lottery);

// Sum up both sets of digits to compare for 3 inconsecutive matches
int guessSum = guessDigit1 + guessDigit2 + guessDigit3;
int lotterySum = lotteryDigit1 + lotteryDigit2 + lotteryDigit3;

// RULE1 Check the guess
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
// RULE2
else if ((guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3)
&& (guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3)
&& (guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3)
&& guessSum == lotterySum)
System.out.println("Match all digits: you win $3,000");
// RULE3
else if ((guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3)
|| (guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3)
|| (guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3))
System.out.println("Match one digit: you win $1,000");

else
System.out.println("Sorry, no match");
}
}

这似乎最有效。我一直在循环使用数字来检验我的猜测。我没有遇到过错误的猜测。 &&guessSum == lotterySum) 仅与 RULE2 一起使用。

最佳答案

规则 2 的条件是错误的;它当前说(以伪代码):

IF  guessDigit1 is anyLotteryDigit
AND guessDigit2 is anyLotteryDigit
AND guessDigit3 is anyLotteryDigit

这显然不是您想要的,因为这意味着多个猜测数字可以匹配同一个彩票数字,就像您遇到的 110 匹配 100 code> - 由于 10 都在中奖号码中,因此即使不应该通过,它也会通过。

相反,您希望将每个数字与剩余数字相匹配。执行此操作的“正确”方法是使用 Set ,但听起来你还不能使用它们。你可以手动完成,只需要把所有的情况都写出来就行了。基本上,您将猜测的一位数字与彩票的一位数字进行比较,然后将剩余的两个猜测数字与剩余的两个彩票数字进行比较。对每个数字重复冲洗。

关于java - 创建彩票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28420536/

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