产生一个三位数的彩票。该程序提示用户输入一个三位数的数字,然后
根据以下规则确定用户是否获胜:
(规则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一起使用。 最佳答案
我认为问题在于您没有考虑重复数字。您可以检查以确保所有数字加起来都等于样本编号,即:
1+1+0 = 2
1+0+0 = 1
1!=2
Move onto rule 3.
使用此代码:
guessDigit1 + guessDigit2 + guessDigit3 == lotteryDigit1 + lotteryDigit2 + lotteryDigit3
我不知道这是否可以解决您的所有问题,但这只是一个开始,应该以110值解决您的直接问题。祝好运!
关于java - 制作彩票,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28420536/