Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一种改进的数组检查方法_Java_Arrays - Fatal编程技术网

Java 一种改进的数组检查方法

Java 一种改进的数组检查方法,java,arrays,Java,Arrays,我有下面的方法,从用户接收一个数字数组,从计算机接收一个数字数组。然后检查用户输入的数字是否正确,以及是否位于计算机生成的数字的正确位置。例如,如果用户输入2341,计算机号码是2358,结果数组将是2200(两个正确的数字在正确的位置,两个错误的数字)。该方法工作得很好,我想知道如何使它更简洁,并减少代码 @Override public String checkValues(String [] userInput, int [] computerNumber) { int fla

我有下面的方法,从用户接收一个数字数组,从计算机接收一个数字数组。然后检查用户输入的数字是否正确,以及是否位于计算机生成的数字的正确位置。例如,如果用户输入2341,计算机号码是2358,结果数组将是2200(两个正确的数字在正确的位置,两个错误的数字)。该方法工作得很好,我想知道如何使它更简洁,并减少代码

@Override
public String checkValues(String [] userInput, int [] computerNumber)
  {
   int flag = 0;
   String [] validate = {""};
   //Results can be put inside an int array 
   //int  [] results = new int[4];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (Integer.parseInt(userInput[i]) == computerNumber[j]) {
               if (i==j) {
                   validate[0]+="2";
                   flag++ ;
                }
            }
        }
    }
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (Integer.parseInt(userInput[i])==computerNumber[j] && (i!=j)) {
                validate[0]+="1";
                flag++;
            }
        }
    }
switch(flag) 
 {
     case 0: validate[0]+="0000"; break;
     case 1: validate[0]+="000"; break;
     case 2: validate[0]+="00"; break;
     case 3: validate[0]+="0"; break;
 }
    return validate[0];
}
@覆盖
公共字符串校验值(字符串[]用户输入,整数[]计算机编号)
{
int标志=0;
字符串[]验证={”“};
//结果可以放在int数组中
//int[]结果=新的int[4];
对于(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
if(Integer.parseInt(userInput[i])==computerNumber[j]){
如果(i==j){
验证[0]+=“2”;
flag++;
}
}
}
}
对于(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
if(Integer.parseInt(userInput[i])==computerNumber[j]&&(i!=j)){
验证[0]+=“1”;
flag++;
}
}
}
开关(标志)
{
案例0:验证[0]+=“0000”;中断;
案例1:验证[0]+=“000”;中断;
案例2:验证[0]+=“00”;中断;
案例3:验证[0]+=“0”;中断;
}
返回验证[0];
}

*****全班***

/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装策划2;
导入java.util.*;
导入java.util.regex.*;
/**
*
*@作者sarth
*/
公共类MM实现MasterMindInterface{
字符串[][]网格=新字符串[10][4];
/*
*DrawGame Void方法绘制网格
*/
@凌驾
公共游戏
{
System.out.println(“---------------------------”);
System.out.println(“|“+”X“+”|“+”X“+”|“+”X“+”|“+”X“+”);
System.out.println(“-------------------------------”;
对于(int i=0;i对于(inti=1;i而言,问题在于用户输入2458与2358的对比可能会返回3033。
因此,至少有一个必须首先计算匹配项:

public String checkValues(String[] userInput, int[] computerNumber) {
    int right = 0;
    int wrong = 0;
    for (int i = 0; i < computerNumber.length; ++i) {
        if (i >= userInput.length) {
            ++wrong;
        } else if (userInput[i].equals(computerNumber[i])) {
            ++right;
        } else {
            ++wrong;
        }
    }
    //Would have been nice: return new int[] { right, wrong };
    String rightText = String.valueOf(right);
    ...
}
公共字符串校验值(字符串[]用户输入,int[]计算机编号){
int right=0;
int错误=0;
对于(int i=0;i=userInput.length){
++错;
}else if(userInput[i].equals(computerNumber[i])){
++对,;
}否则{
++错;
}
}
//很好:返回新的int[]{对,错};
String rightText=String.valueOf(右);
...
}

还有一点要做。

更好地询问我不知道它的存在!对不起,在你询问代码审查之前,你应该创建一个完整的类,包含一个主方法和几个示例输入和输出。最重要的是,更好地描述该方法的用途,因为它不太清楚。看起来它最多可以返回20位数字:从第一个循环加上第二个循环的4x4。您的解释仍然不清楚。您说计算机生成一个1到8之间的唯一4位数字。然后您说它可以生成0000、1111或2222。这些数字不在1到8之间。还有,“唯一”是什么意思?0000 1111或2222是比较用户数字和计算机数字后的可能结果。例如:Comp number 2 3 4 1,用户输入4 1 7 8,结果将为1100,因为用户输入了两个正确的数字,但它们位于错误的位置。很抱歉,前面不清楚。这基本上是一个有数字感兴趣的智囊团游戏。我将继续像其他人建议的那样,用完整的类和主要方法确定主要主题的日期Yes CodeReview很好,但是要准备一点,自动格式化你的代码等等。我添加了完整的类和带有完整描述的主要主题
package mastermind2;

/**
 *
 * @author sarth
 */
public class MasterMind2 {


    public static String [][] board = new String[10][4];
    public static int [] tempComp;
    public static  String [] computerNum = new String[10];

    public static void main(String[] args) 
    {
      //Calling the MM class with all methods
      MM mastermind = new MM();
     //Initialize the board
      for (int i = 0; i<10;i++) {
          for (int j = 0; j< 4; j++) {
              board[i][j] = "";
          }
      }

      for (int i = 0; i < 10; i++) 
      {
        computerNum[i]= "    ";
      }
     //Call the draw game method
      mastermind.drawGame();
      tempComp = mastermind.generateCompNum();
        //give user 10 
        for (int i = 9; i > 0; i--) 
        {
            int temp[] = mastermind.newGame();
                for(int j = 0; j< 4; j++) 
                {
                    board[i][j]+=temp[j];
                }
                computerNum[i] = mastermind.checkValues(board[i], tempComp);

                if(Integer.parseInt(computerNum[i])==2222)
                {
                    mastermind.finalUpdateGame(board, computerNum, tempComp);
                }
                else 
                {
                    mastermind.updateGame(board,computerNum);
                }
        }
    }
}
public String checkValues(String[] userInput, int[] computerNumber) {
    int right = 0;
    int wrong = 0;
    for (int i = 0; i < computerNumber.length; ++i) {
        if (i >= userInput.length) {
            ++wrong;
        } else if (userInput[i].equals(computerNumber[i])) {
            ++right;
        } else {
            ++wrong;
        }
    }
    //Would have been nice: return new int[] { right, wrong };
    String rightText = String.valueOf(right);
    ...
}