生成一个三位数的彩票。程序提示用户输入一个三位数,然后根据以下规则确定用户是否获胜:(规则1)。如果用户输入与彩票号码完全匹配,则奖励为10000美元。(规则2)。如果用户输入的所有数字与彩票号码中的所有数字匹配,则奖励为3000美元。(第3条)。如果用户输入中的一位数字与彩票号码中的一个数字匹配,则奖励为1000美元。
我需要帮助,让我的程序按照代码中的所有内容正常运行。没有数组,没有字符串,除了已经存在的内容之外什么都没有。我的问题是,我不能得到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
一样——由于1
和0
都在中奖号码中,即使不应该,它也会通过。
相反,您希望将每个数字与剩余的数字相匹配。"正确"的方法是使用Set
,但听起来你还不能使用它们。你可以手动完成,只需要写下所有的案例就可以了。基本上,您将一位猜测数字与一位彩票数字进行比较,然后将剩余的两位猜测数字与剩余的两个彩票数字进行比较。对每个手指重复冲洗。
这里的问题是您有重复项,因此规则2的当前条件评估为true,为100<=>110
为了消除这种影响,通常的变体是对数字中的数字进行排序并进行比较,就像这样(伪语言):
if (sortDigits(lottery) == sortDigits(guess))
如果你不能使用数组,这里有一些简单的函数可以对3位数进行排序:
int sortDigits(int n) {
int d1 = n/100;
int d2 = (n/10)%10;
int d3 = n%10;
int min = Math.min(d1, Math.min(d2, d3));
int max = Math.max(d1, Math.max(d2, d3));
int mid = -1;
if (min == d1) mid = Math.min(d2, d3);
else if (min == d2) mid = Math.min(d1, d3);
else mid = Math.min(d1, d2);
return (min*100) + (mid*10) + max;
}
您的问题是用彩票的每个数字来检查猜测的每个数字。这就是你出现错误的原因,你需要记住你匹配了什么。要做到这一点,一个非常快速而肮脏的解决方案是检查是否有替身,并用一个永远不会匹配的值替换它们,比如-1:
if(guessDigit1 == guessDigit2){
guessDigit1 = -1;
}
else if (guessDigit1 == guessDigit3 ) {
guessDigit1 = -1;
}
else if (guessDigit2 == guessDigit3 ) {
guessDigit2 = -1;
}
请注意,这根本不是最好的解决方案!
更新:为了记住您检查过的内容,您必须实现许多if条件。其中一个的伪代码如下:
If guessDigit2 == lotteryDigit1
if guessDigit1 == lotteryDigit2
if guessDigit3 == lotteryDigit3
-> You found all digits
else if guessDigit1 == lotteryDigit3
if guessDigit3 == lotteryDigit2
-> You found all digits
当然,您必须实现此代码*3,因为您需要为guessDigit1==lotteryDigit1和guessDigid3==lotteryGigit1创建另外两个if。
正如@dimo414(以及其他答案)所提到的,您需要某种内存来检查哪些数字。下面的条件句会给出你想要的排列。如果matched1
、matched2
或matched3
最后是-1
,则不匹配。
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);
int matched1 = -1;
int matched2 = -1;
int matched3 = -1;
// RULE1 Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
}
// RULE2
else {
if (guessDigit1 == lotteryDigit1) {
matched1 = 1;
if (guessDigit2 == lotteryDigit2) {
matched2 = 2;
if (guessDigit3 == lotteryDigit3) {
matched3 = 3;
}
} else if (guessDigit2 == lotteryDigit3) {
matched2 = 3;
if (guessDigit3 == lotteryDigit2) {
matched3 = 2;
}
}
} else if (guessDigit1 == lotteryDigit2) {
matched1 = 2;
if (guessDigit2 == lotteryDigit1) {
matched2 = 1;
if (guessDigit3 == lotteryDigit3) {
matched3 = 3;
}
} else if (guessDigit2 == lotteryDigit3) {
matched2 = 3;
if (guessDigit3 == lotteryDigit1) {
matched3 = 1;
}
}
} else if (guessDigit1 == lotteryDigit3) {
matched1 = 3;
if (guessDigit2 == lotteryDigit1) {
matched2 = 1;
if (guessDigit3 == lotteryDigit2) {
matched3 = 2;
}
} else if (guessDigit2 == lotteryDigit2) {
matched2 = 2;
if (guessDigit3 == lotteryDigit1) {
matched3 = 1;
}
}
}
if (matched1 != -1 && matched2 != -1 && matched3 != -1) {
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");
}
}
}
}
这首先检查你猜测的第一位数字是否是彩票的第一位。如果是这样的话,它会保存matched1(用于您猜测的数字)和值1(用于彩票中的第一个数字)。然后,它将第二个猜测的数字与彩票的数字2和3进行比较。我们不会在这里再次将您的猜测的第二位数字与彩票的第一位数字进行核对,因为我们已经知道第一位猜测与第一位彩票匹配。如果第一个数字与第一个彩票号码不匹配,那么我们看看猜测的第一个数字是否是彩票号码的第二个数字,然后冲洗并重复。(如果是这样的话,那么对于彩票的第二位数字,matched1将是2。)
(matched1,matched2,matched3)的所有匹配值都是(1,2,3),(1,3,2),[2,1,3],[2,3,1],[3,1,2]和[3,2,1]。您可以看到所有这些排列是如何在上面的嵌套if语句中覆盖的。
我想我知道这一点,可能是因为110在规则2中和规则3中都是真的,这就是为什么它显示两个规则的输出。这是规则2 的布尔表达式
((guessDigit1 == lotteryDigit2
&& guessDigit2 == lotteryDigit3
&& guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit1
&& guessDigit3 == lotteryDigit2)
|| (guessDigit1 == lotteryDigit1
&& guessDigit2 == lotteryDigit3
&& guessDigit3 == lotteryDigit2)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit2
&& guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit2
&& guessDigit2 == lotteryDigit1
&& guessDigit3 == lotteryDigit3))
所有可能的答案都在那里。
我认为问题在于您没有考虑重复数字。你可以检查以确保所有数字加起来都是样本号,即:
1+1+0 = 2
1+0+0 = 1
1!=2
Move onto rule 3.
使用此代码:
guessDigit1 + guessDigit2 + guessDigit3 == lotteryDigit1 + lotteryDigit2 + lotteryDigit3
我不知道这是否能解决你所有的问题,但这只是一个开始,它应该能以"110"的价值解决你眼前的问题。祝你好运
相反,您希望将每个数字与其余数字进行匹配。"正确"的方法是使用Set,但听起来你还不能使用这些。你可以手动完成,只需要写下所有的案例就可以了。基本上,你将一位数的猜测与一位数的彩票进行比较,然后将剩下的两位数的猜测和剩下的两位彩票数字进行比较。对每个手指重复冲洗。
会是这样的吗:
else if ((guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3)
&& (guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit3)
&& (guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2))
System.out.println("Match all digits: you win $3,000");
这是我所能处理的——没有其他公式。用于提交代码的程序对输入非常挑剔。它只会接受它设定要接受的东西。