Java 比较用户输入之间随机生成的数字
我是一名cs学生,我有一项作业,我不确定如何完成。提示如下: “开发一个Java控制台应用程序,用于一个简单的游戏,猜一个秘密的五位数代码(从10000到99999的随机数)。当用户输入对代码的猜测时,程序输出两个值:猜测中位于正确位置的位数和这些位数的总和。例如,如果密码为53840,用户猜测83241,则数字3和4位于正确位置。因此,程序应以2作为响应(正确数字的数量)和7(正确数字的总和)。允许用户猜测,直到他/她得到正确的数字。” 基本上,我一直关注的是如何找出哪些数字是共同的正确数字,并将它们相加Java 比较用户输入之间随机生成的数字,java,random,Java,Random,我是一名cs学生,我有一项作业,我不确定如何完成。提示如下: “开发一个Java控制台应用程序,用于一个简单的游戏,猜一个秘密的五位数代码(从10000到99999的随机数)。当用户输入对代码的猜测时,程序输出两个值:猜测中位于正确位置的位数和这些位数的总和。例如,如果密码为53840,用户猜测83241,则数字3和4位于正确位置。因此,程序应以2作为响应(正确数字的数量)和7(正确数字的总和)。允许用户猜测,直到他/她得到正确的数字。” 基本上,我一直关注的是如何找出哪些数字是共同的正确数字,
Random rand = new Random();
int secretNumber = rand.nextInt(99999 - 10000 + 1) + 10000;
System.out.println(secretNumber);
Scanner consoleScanner = new Scanner(System.in);
int guess;
do {
System.out.print("Please enter a 5-digit code (your guess): ");
guess = consoleScanner.nextInt();
if (guess == secretNumber)
System.out.println("****HOORAY! You solved it. You are so smart****");
else if (guess > 99999 || guess < 10000)
System.out.println("Guess must be a 5-digit code between 10000 and 99999.\n");
} while (guess != secretNumber);
Random rand=new Random();
int secretNumber=rand.nextInt(99999-10000+1)+10000;
系统输出打印号(secretNumber);
扫描仪控制台扫描仪=新扫描仪(System.in);
智力猜测;
做{
System.out.print(“请输入5位代码(您的猜测):”;
guess=consoleScanner.nextInt();
如果(猜测==秘密数字)
System.out.println(“****万岁!你解决了它。你太聪明了****”;
否则如果(猜测>99999 | |猜测<10000)
System.out.println(“Guess必须是介于10000和99999之间的5位代码。\n”);
}while(猜测!=秘密数字);
非常感谢您的帮助。返回整数的字符串表示形式。您可以比较从
integer.toString(secretNumber)
和integer.toString(guess)返回的字符串
逐字符确定哪些数字不同。返回整数的字符串表示形式。您可以比较从integer.toString(secretNumber)
和integer.toString(guess)返回的字符串
一个字符接一个字符来确定哪些数字不同。您有一个数字。我将其称为blarg。假设blarg是双精度的。
您还有一个名为“输入”的数字
String blargString = Double.toString(blarg);
String inputString = Double.toString(input);
ArrayList<Integer[]> indexNumberList = new ArrayList<Integer[]>();
int n = 0;
for (char c : blargString.toCharArray()) {
n++;
if (c == inputString.toCharArray()[n]) {
Integer[] entry = new Integer[2];
entry[0] = n;
entry[1] = Character.getNumericValue(c);
indexNumberList.add(entry);
}
}
String blargString=Double.toString(blarg);
字符串inputString=Double.toString(输入);
ArrayList indexNumberList=新的ArrayList();
int n=0;
for(char c:blargString.toCharArray()){
n++;
如果(c==inputString.ToCharray()[n]){
整数[]项=新整数[2];
条目[0]=n;
条目[1]=字符.getNumericValue(c);
indexNumberList.add(条目);
}
}
现在,您有了一个整数对列表。请随意使用它。对于每一对,条目[0]是数字中的位置,索引,条目[1]是值。您有一个数字。我将其称为blarg。假设blarg是双精度的。 您还有一个名为“输入”的数字
String blargString = Double.toString(blarg);
String inputString = Double.toString(input);
ArrayList<Integer[]> indexNumberList = new ArrayList<Integer[]>();
int n = 0;
for (char c : blargString.toCharArray()) {
n++;
if (c == inputString.toCharArray()[n]) {
Integer[] entry = new Integer[2];
entry[0] = n;
entry[1] = Character.getNumericValue(c);
indexNumberList.add(entry);
}
}
String blargString=Double.toString(blarg);
字符串inputString=Double.toString(输入);
ArrayList indexNumberList=新的ArrayList();
int n=0;
for(char c:blargString.toCharArray()){
n++;
如果(c==inputString.ToCharray()[n]){
整数[]项=新整数[2];
条目[0]=n;
条目[1]=字符.getNumericValue(c);
indexNumberList.add(条目);
}
}
现在,您有了一个整数对列表。您可以随意使用它。对于每一对,条目[0]是数字中的位置,索引,条目[1]是值。您可以使用整数、模数和除法来获得所需的数字 53840%100000/10000=5
53840%10000/1000=3
循环并比较您可以使用整数、模数和除法来获得所需的数字 53840%100000/10000=5
53840%10000/1000=3
循环并比较以下是我解决该问题的方法。我的解决方案很快,但可能很幼稚。将用户输入的数字和生成的数字转换为字符串,然后转换为每个5字节的两个数组。扫描数组并一次比较两个对应的字节。让用户知道数字的位置是正确的如果两个对应的字节相等,则正确地执行essed。下面,我将向您展示如何获得所需的字节数组
byte[] a = Integer.toString(guess).getBytes();
byte[] b = Integer.toString(secretNumber).getBytes();
下面是我如何着手解决这个问题的。我的解决方案很快,但可能很幼稚。将用户输入的数字和生成的数字转换为字符串,然后转换为两个数组,每个数组有5个字节。扫描数组并一次比较两个对应的字节。如果两个字节都是数字,则让用户知道正确猜测了数字的位置相应的字节是相等的。下面,我将向您展示如何获得所需的字节数组
byte[] a = Integer.toString(guess).getBytes();
byte[] b = Integer.toString(secretNumber).getBytes();
所以你需要比较两个5位数的数字。 我建议您使用循环执行此操作:
//Make copies so we can modify the value without changing
// the original ones.
int tempGuess = guess;
int tempSecret = secretNumber;
//Create variables for the output
int numCorrect = 0;
int sumCorrect = 0;
for(int i = 0; i < 5; i++) //for each of the digits
{
//Get the last digit of each number and remove it from the number:
int lastGuess = tempGuess%10;
tempGuess/=10;
int lastSecret = tempSecret%10;
tempSecret/=10;
//Compare both digits:
if(lastGuess == lastSecret)
{
//Found a match: Increas number of found by one
numCorrect++;
//Add value of digit to sum
sumCorrect += lastGuess;
}
}
//numCorrect now contains the number of matching digits
//sumCorrect now contains the sum of matchig digits
//复制副本,以便我们可以修改值而不更改
//原版的。
int tempGuess=猜测;
int tempSecret=secretNumber;
//为输出创建变量
int numCorrect=0;
int-sumCorrect=0;
对于(int i=0;i<5;i++)//对于每个数字
{
//获取每个数字的最后一位,并将其从数字中删除:
int lastGuess=tempGuess%10;
tempGuess/=10;
int lastSecret=tempSecret%10;
tempSecret/=10;
//比较两个数字:
if(lastGuess==lastSecret)
{
//找到匹配项:将找到的数量增加1
numCorrect++;
//将数字的值与总和相加
sumCorrect+=lastGuess;
}
}
//numCorrect现在包含匹配位数
//sumCorrect现在包含匹配数字的和
因此您需要比较两个5位数字。
我建议您使用循环执行此操作:
//Make copies so we can modify the value without changing
// the original ones.
int tempGuess = guess;
int tempSecret = secretNumber;
//Create variables for the output
int numCorrect = 0;
int sumCorrect = 0;
for(int i = 0; i < 5; i++) //for each of the digits
{
//Get the last digit of each number and remove it from the number:
int lastGuess = tempGuess%10;
tempGuess/=10;
int lastSecret = tempSecret%10;
tempSecret/=10;
//Compare both digits:
if(lastGuess == lastSecret)
{
//Found a match: Increas number of found by one
numCorrect++;
//Add value of digit to sum
sumCorrect += lastGuess;
}
}
//numCorrect now contains the number of matching digits
//sumCorrect now contains the sum of matchig digits
//复制副本,以便我们可以修改值而不更改
//原版的。
int tempGuess=猜测;
int tempSecret=secretNumber;
//为输出创建变量
int numC