大家好,我真的很困惑,它是关于一个彩票程序,该程序随机生成一个四位数的数字,并提示用户输入四位数的数字,而无需使用数组并遵循此规则
-如果用户输入的彩票号码与彩票号码完全相同,则奖励为$ 10000
-如果用户输入以不同顺序匹配四位数,则奖励为$ 5000
-如果用户输入以不同顺序匹配三位数,则奖励为$ 2000
-如果用户输入中的任何一位或两位数字与彩票相匹配,则奖励为$ 500
我为一个三位数的数字做了它,但是我不知道如何为没有arrays.This的四位数的数字做它:
import java.util.Scanner;
public class Programming {
public static void main(String[] args) {
// Generate a lottery
int lottery = (int) (Math.random() * 1000);
// 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 " + lotteryDigit1
+ lotteryDigit2 + lotteryDigit3);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
} else if ((guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit3)
|| (guessDigit1 == lotteryDigit2
&& guessDigit1 == lotteryDigit3 && guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit2 && guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit1
&& guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit2)) {
System.out.println("Match all digits: you win $5,000");
} 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");
}
}
}
感谢您的帮助
最佳答案
另一个编辑
抱歉,请花一些时间与您联系。忙了几天。根据要求,这里是完整的程序。请注意,这并不完全是您的原始程序。我删除了几处内容,并添加了更多内容。也许最重要的是,我更改了之前给您的功能。我最初回答了您的问题,试图只给您四位数的匹配,但是如果我要写出完整的内容,那么概括一下会更有意义。因为您的彩票奖励基于匹配位数的数字,所以使一个函数计算匹配的位数非常有意义。这大大简化了if-else代码。
import java.util.Scanner;
public class Programming {
/*
* get the number of matching digits between the guess and the
* answer, ignoring repeated matches
*/
public static int numberDigitsMatch(String guess, String answer) {
int numberMatch = 0;
int currentIndex = 0;
int matchingIndex;
while (currentIndex < guess.length()) {
// check if the current digit of the guess is in the answer
matchingIndex = answer.indexOf(guess.charAt(currentIndex));
if (matchingIndex < 0) {
currentIndex++;
}
else {
currentIndex++;
numberMatch++;
// remove the no longer relevant character from the answer
answer = answer.substring(0, matchingIndex) +
answer.substring(matchingIndex + 1);
}
}
return numberMatch;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// generate the winning number
int lotteryNumber = (int) (Math.random() * 10000);
String lotteryString = "" + lotteryNumber;
System.out.println("The lottery number is " + lotteryString);
// Prompt the user to enter a guess
System.out.print("Enter your lottery pick (four digits): ");
// get the user's guess
int guessNumber = in.nextInt();
String guessString = "" + guessNumber;
// NOTE: these guessDigit numbers are not necessary. I am leaving
// them here so you can see how to pick out individual digits
int guessDigit1 = guessNumber % 10;
int guessDigit2 = (guessNumber / 10) % 10;
int guessDigit3 = (guessNumber / 100) % 10;
int guessDigit4 = (guessNumber / 1000) % 10;
int lotteryDigit1 = lotteryNumber % 10;
int lotteryDigit2 = (lotteryNumber / 10) % 10;
int lotteryDigit3 = (lotteryNumber / 100) % 10;
int lotteryDigit4 = (lotteryNumber / 1000) % 10;
System.out.println("The lottery number is " + lotteryString);
int numMatchingDigits = numberDigitsMatch(guessString, lotteryString);
if (guessNumber == lotteryNumber) {
System.out.println("Exact match: you win $10,000");
}
else if (4 == numMatchingDigits) {
System.out.println("Match all digits: you win $5,000");
}
else if (3 == numMatchingDigits) {
System.out.println("Match three digits: you win $2,000");
}
else if (2 == numMatchingDigits) {
System.out.println("Match two digits: you win $500");
}
else if (1 == numMatchingDigits) {
System.out.println("Match two digit: you win $500");
}
else {
System.out.println("Sorry, no match");
}
}
}
编辑
看来我听不清楚。我相信有多种方法可以获取数字的给定数字,但是我认为这是最容易想到的。您用1000除以将千位数移到1的位置,然后使用模数运算符除去不在1的位置的任何内容。这只剩下数字的第四位。
public static int getFourthDigit(int num) {
return (num / 1000) % 10;
}
原版的
如果我理解正确,那么您的困难在于匹配不同的有序四位数。最明显的方法是检查所有四位数字的可能排序;它们不完全相等的只有15个。但是,输入15个条件很容易出错并且很无聊。如果您需要用五位数来完成此操作,那将是无聊又乏味的两倍。
这是通过使用
String
而不是int
避免此情况的函数。它反复检查猜测的第一个字符,并检查该字符是否在答案中。然后,它将匹配的字符从答案中删除,以避免出现1111
和1112
这样的情况,因为猜测中的每个字符也都在答案中,但它们不匹配。public static boolean digitsMatch(String guess, String answer) {
int matchingIndex;
while (guess.length() > 0) {
// check if the first digit of the guess is in the answer
matchingIndex = answer.indexOf(guess.charAt(0));
// if not, there cannot possibly be four matches
if (matchingIndex < 0) {
return false;
}
// look at the rest of the guess
guess = guess.substring(1);
// and remove the no longer relevant character from the answer
answer = answer.substring(0, matchingIndex) +
answer.substring(matchingIndex + 1);
}
return true;
}
关于java - Java没有数组的彩票程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39810343/