对于相同的输入,程序在给定条件下的行为不一致

时间:2018-10-28 17:05:07

标签: java if-statement logic conditional-statements

这是我制作的游戏的功能。它是流行的棋盘游戏Mastermind的复制品。

我用数字1-8替换了8种颜色。不允许重复,并且计算机生成的代码也严格遵守这些规则。

当用户输入的代码中包含数字0或9或不是4位数字的代码时,我已经设置了出错条件。

但是,该程序并不总是会向用户执行的错误给出错误。

例如:

1)用户输入0439。

程序给出错误。

2)用户输入412906。

程序很好用。

这些都是 非常 不一致 ,因为下次您运行该程序时,对于相同的输入,您不会得到相同的错误。

有人可以帮我解决问题吗?

注意:请最后删除quit()函数代码,因为它与程序中的另一个类相关。只需删除最后一个if块即可。

我尝试搜索它,但是找不到与我的疑问类似的问题,而且我的朋友都无法帮助我。

public void master()throws IOException,InterruptedException
{        
    int i,j,c=0,a,k=0;
    String str,ch;
    String code[]=new String[4];

    System.out.println("\t\t\tWelcome to Mastermind\n");
    System.out.println("\nThe computer will generate a 4 digit number consisting of digits from 1 to 8 without repetition and you will have to crack the code in 10 attempts.\n0,9,letters and special characters are not allowed and if entered,the computer will ask you to try again.\nKey description-\n □ means that a digit in the number entered by the user is present in the code but is at the wrong position.\n ■ means that a digit in the number entered by the user is present in the code and is at the correct position.\nIn both the cases,the user will not be told to which digit the key given by the computer is referring to.\nDuplicates are not allowed and are not present in the code generated by the computer.\nIf you try to enter a number with repeating digits,it will not be accepted and you will be told to try again.");
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter \"play\" to start");
    String play=in.readLine();
    if(play.equalsIgnoreCase("play"))
    {            
        code[0]=Integer.toString((int)(Math.random()*7)+1);// generates first no. of code
        do           
        {
            code[1]=Integer.toString((int)(Math.random()*7)+1);// generates second no. of code
        }while(code[1].equals(code[0]));

        do           
        {
            code[2]=Integer.toString((int)(Math.random()*7)+1);// generates third no. of code
        }while(code[2].equals(code[1]) || code[2].equals(code[0]));

        do           
        {
            code[3]=Integer.toString((int)(Math.random()*7)+1);// generates fourth no. of code
        }while(code[3].equals(code[2]) || code[3].equals(code[1]) || code[3].equals(code[0]));

        System.out.println("The game begins");
        for(a=1;a<=10;a++)
        {
            System.out.println("Attempt "+a);
            str=in.readLine();   

            if(str.length()!=4)  
            {
                System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number without repetitions");
                str=in.readLine();
            }

            else
            {
                for(i=0;i<4;i++)
                {
                    c=(int)(str.charAt(i));
                    if(c>56||c<49) // checks if input is appropriate by comparing ASCII value of the input
                    {
                        System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number");
                        str=in.readLine(); 
                    }
                    else
                    {
                        for(i=0;i<4;i++)
                        {
                            for(j=0;j<4;j++)
                            {
                                if(i!=j)
                                {
                                    if(str.charAt(i)==str.charAt(j))
                                    {
                                        System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number");
                                        str=in.readLine();
                                        break;
                                    }
                                }
                            } 
                        }
                    }
                }  
            }

            if((code[0]+code[1]+code[2]+code[3]).equals(str))
            {
                k=1;
                break;
            }
            for(i=0;i<4;i++)
            {
                ch=Character.toString(str.charAt(i));
                for(j=0;j<4;j++)
                {                        
                    if(code[j].equals(ch))
                    {
                        if(i==j) 
                        {
                            System.out.print("■");
                        }
                        else
                            System.out.print("□");
                    }                        
                }
            }
            System.out.println();
        } 
        if(k!=1)
            System.out.println("The code is "+code[0]+code[1]+code[2]+code[3]);
        else
            System.out.println("You did it!!!");
    }
    System.out.println("Thank You");
    if(!play.equalsIgnoreCase("play"))
        quit(); 
}

2 个答案:

答案 0 :(得分:0)

if(str.length()!=4)  
{
    System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number without repetitions");
    str=in.readLine();
}

在这里,您只检查一次长度,然后在不再次检查长度的情况下又进行了一次输入,还跳过了检查数字的整个过程,因为您将其放在else子句中,如果第一个输入的长度不是4。

这可以做到:

while(true){
    str = in.readLine();
    if(str.length() != 4){
        // wrong length detected -  print error message
        continue;
    }
    boolean wrongChars = false;
    for(char c : str.toCharArray())
        if(c>56||c<49){
            wrongChars = true;
            break;
        }
    if(wrongChars){
        // wrong characters detected - print error message
        continue;
    }
    boolean duplicateChars = false;
    for(int i=0; i<4; i++)
        for(int j = i + 1; j < 4; j++)
            if(str.charAt(i)==str.charAt(j))
                duplicateChars = true;
    if(duplicateChars){
        // duplicate digits detected - print error message
        continue;
    }
    break; // all input checks passed - break the loop
}

答案 1 :(得分:0)

您的代码不一致是因为:

if(str.length()!=4)  
            {
                System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number without repetitions");
                str=in.readLine();
            }

考虑一下。用户正在尝试使用数字412906进行首次尝试,该数字无效。然后,您无需任何验证即可接受新输入。

这里应该发生的是,该过程再次以for递增一(+1)