我一直在尝试复制名为mastermind的游戏的基本程序。到目前为止,我已经能够获得4个数据点和4个用户输入作为整数,但是我在比较4个数据点和4个用户输入时遇到了问题。目标是有4个rng(范围从1-9)并且用户一次猜测4个数字。给出的唯一暗示是一个数字是正确的。到目前为止,我已经能够制作一个效率低下的代码,用于比较4个变量ex(a = rng1,b = rng2,c = rng3,d = rng4)和用户输入。如何有效地将用户输入与生成的所有rng进行比较。在这种情况下,我希望能够将s1与poop进行比较; poop2; poop3;和poop4。我不想写另外100行代码来做到这一点。提前致谢。代码如下。
import java.util.*;
public class RandomNumberGenerator{;
private static Scanner in;
public static void main(String[] args){
Random r = new Random();
float s;
in = new Scanner(System.in);
int poop = (r.nextInt(10-1)+1);
int poop1 = (r.nextInt(10-1)+1);
int poop2 = (r.nextInt(10-1)+1);
int poop3 = (r.nextInt(10-1)+1);
for (int i = 0; i < 80; i++)
{
System.out.println("Enter a number from 1-9");
s = in.nextFloat();
float s1 = in.nextFloat();
float s2 = in.nextFloat();
float s3 = in.nextFloat();
if(poop == s && poop1 == s1 && poop2 == s2 && poop3 == s3) {
System.out.println("You are correct HeHe XD");
break;
}
if(poop > s && poop1 == s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop < s && poop1 == s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop > s && poop1 == s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 > s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 < s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 > s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 < s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 == s2 && poop3 > s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 == s2 && poop3 < s3){
System.out.println("You have 1 wrong");
}
if(poop != s && poop1 != s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 2 wrong");
}
if(poop != s && poop1 == s1 && poop2 != s2 && poop3 == s3){
System.out.println("You have 2 wrong");
}
if(poop != s && poop1 == s1 && poop2 == s2 && poop3 != s3){
System.out.println("You have 2 wrong");
}
if(poop == s && poop1 != s1 && poop2 != s2 && poop3 == s3){
System.out.println("You have 2 wrong");
}
if(poop == s && poop1 != s1 && poop2 == s2 && poop3 != s3){
System.out.println("You have 2 wrong");
}
if(poop == s && poop1 != s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 == s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 != s1 && poop2 == s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 != s1 && poop2 != s2 && poop3 == s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 == s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 != s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 4 wrong");
}
}
}
}
答案 0 :(得分:1)
将变量放在List
中,然后您可以contains
查看元素是否在List
中。
实际上你在mastermind中得到两个数字作为反馈,有多少是正确的数字,有多少也在正确的位置。
由于mastermind中可能存在重复项,因此您可以从copy
创建List
原始guess
以及每个正确的remove
List
。否则,如果主编number
该编号,则猜测所有相同的contains
将是正确的。
int size = 4;
List<Integer> poops = new ArrayList<>();
for(int i =0; i<size; i++){
poops.add((r.nextInt(10-1)+1));
}
...
List<Integer> guesses= new ArrayList<>();
for(int i =0; i<size; i++){
guesses.add(in.nextInt());
}
int correctNumberCount = 0;
List<Integer> poopsCopy = new ArrayList<>(poops);
for(Integer guess: guesses){
if(poopsCopy.contains(guess)){
correctNumberCount ++;
//For handling duplicates
poopsCopy.remove(guess);
}
}
int correctCount = 0;
int counter = 0;
for(Integer guess: guesses){
if(guess == poops.get(counter)){
correctCount++;
}
counter++;
}
//Inform the user about correctNumberCount and correctCount
答案 1 :(得分:1)
您不会使用名为p1,p2,p3,...
的变量来执行此操作您退后一步,了解数组的概念!
然后创建两个数组(最好使用 int ;不要浮点数!)。 第一个数组带有程序认为的4个值;第二个数组中填充了来自用户的4个值。
然后你迭代两个数组;计算&#34;用户&#34;中有多少元素数组在&#34;计算机&#34;中给出。阵列。真的:你从来没有应该编写包含这么多if / else语句的代码。这样的代码绝对不可能阅读或维护。不要这样做。如初。
或者,您可以查看集合,这些是更复杂的数据结构。在您的情况下,您可以使用Integer对象的List / ArrayList。当你对那些东西感兴趣时;使用您最喜欢的搜索引擎并阅读它。
答案 2 :(得分:1)
您可以使用两个ArrayList存储4个数据点和用户输入。然后使用ArrayList.contains()
进行比较。你不必写很多if condition
。请参阅下面的代码:
public static void main(String[] args) {
final int totalPoints = 4;
Random r = new Random();
in = new Scanner(System.in);
ArrayList<Integer> poops = new ArrayList<Integer>(totalPoints);
ArrayList<Integer> userinputs = new ArrayList<Integer>(totalPoints);
for (int i=0; i<totalPoints; i++) {
int poop = (r.nextInt(10-1)+1);
System.out.println("Random datapoints: " + poop);
poops.add(poop);
}
System.out.println("Enter a number from 1-9");
for (int i=0; i<totalPoints; i++) {
int s = in.nextInt();
userinputs.add(s);
}
int correct = 0;
for (int i=0; i<4; i++) {
if (poops.contains(userinputs.get(i))) {
correct++;
}
}
System.out.println("You have " + correct + "correct numbers");
}
答案 3 :(得分:1)
根据您的要求,您只需要获得每个输入组合的错误数量,而不是错误的位置。因此,您无需列举所有可能性。
您可以尝试以下代码,.state('home', {
url: '/',
templateUrl: 'home.html',
controller: 'homeCtrl'
})
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard/index.html',
controller: 'dashboardCtrl'
})
是表示允许用户输入的数量的数字。
我使用size
来存储数字。将一系列数据保存到容器中是一个好习惯,这样您就可以轻松地使用List
操作它们,loop
是计算机最强大的功能之一。
loop
答案 4 :(得分:-1)
怎么样:
import java.util.*;
public class RandomNumberGenerator{;
private static Scanner in;
public static void main(String[] args){
Random r = new Random();
float s;
in = new Scanner(System.in);
int[] poops = new int[4];
for(int i=0;i<poops.length;i++){
poops[i]=r.nextInt(10-1)+1;
}
float[] s = new float[4];
for (int i = 0; i < 80; i++){
System.out.println("Enter a number from 1-9");
int error =0;
for(int j=0;j<poops.length;j++){
if(poop[j] != in.nextFloat()){
error++;
}
}
if(result == 0){
System.out.println("You are correct HeHe XD");
break;
}else{
System.out.println("You have "+error+" wrong");
}
}
}