JAVA While循环 - 如果语句不起作用,则为最后一个

时间:2017-01-15 21:27:18

标签: java loops while-loop

package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
    public static void main(String[] args) {
        System.out.println("What is the password?");
        Scanner new2 = new Scanner(System.in);
        int input = 0;

        while(input <= 5 )
        {
            String password = new2.nextLine();
            if(!password.equals("bluesky123")){
                System.out.println("Incorrect password");
                input++;
            }

            else if("bluesky123".equals(password)) {
                System.out.println("You got it right!");
                break;
            }
            else if(input == 5) {
                System.out.println("maximum number of attempts reached");
                break;
            }
        }    
    }

}
基本上,一旦我点击了5个循环,就会说“密码不正确”并且中断。不是“最大尝试”消息。

3 个答案:

答案 0 :(得分:3)

请允许我注释:

将始终评估此if语句:

        if(!password.equals("bluesky123")){
            System.out.println("Incorrect password");
            input++;
        }

只有在密码为&#34; bluesky123&#34;时才会评估此if语句。在这种情况下,它将始终评估为true

        else if("bluesky123".equals(password)) {
            System.out.println("You got it right!");
            break;
        }

永远不会评估此if语句。一旦if-else找到一个为true的语句,它将跳过该部分中的所有其他语句。

        else if(input == 5) {
            System.out.println("maximum number of attempts reached");
            break;
        }

在你的情况下,你应该考虑一个嵌套的if(即if if where another if)。

答案 1 :(得分:1)

我测试了你正在运行的代码!您需要做的唯一事情是将跟随线移动到while循环内部

 System.out.println("What is the password?");

执行此操作将打印&#34;密码错误&#34;然后它会再次打印 &#34;密码是什么?&#34;

因为它现在正在工作的方式似乎软件没有等待密码重新输入,实际上它是。

答案 2 :(得分:1)

public static final int NOTHING=0,BIRD_FLYING=1,BIRD_HIT=2,
                        TILE_LAND=0,TILE_WATER=1;

private int[][] gameBoard=new int[8][8]; // Store terrain etc.
private int[][] birds=new int[8][8]; // Store entities on the map
private int tileSize=32; // Store the size of the tile in pixels
private Image landImg,seaImg,flyingImg,hitImg; // Store the tile images (or use 1 tilesheet and segment it in the draw method)

private void hitTile(int x,int y)
{
    for(int i=-1;i<=1;i++)
    {
        for(int j=-1;j<=1;j++)
        {
            birds[hitX+i,hitY+j]=BIRD_HIT;
        }
    }
}

private void drawTheTiles()
{
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            // Draw background tiles first
            switch(gameBoard[i][j])
            {
                default:
                case TILE_LAND:
                    g.drawImage(landImg,i*tileSize,j*tileSize,this);
                    break;
                case TILE_SEA:
                    g.drawImage(seaImg,i*tileSize,j*tileSize,this);
                    break;
            }
            // Draw entities on tiles last
            switch(gameBoard[i][j])
            {
                default:
                case NOTHING:
                    // Don't draw anything
                    break;
                case BIRD_FLYING:
                    g.drawImage(flyingImg,i*tileSize,j*tileSize,this);
                    break;
                case BIRD_HIT:
                    g.drawImage(hitImg,i*tileSize,j*tileSize,this);
                    break;
            }
        }
    }
}

你的逻辑存在一些缺陷。如果

,你必须注意JAVA如何处理if / else

https://www.tutorialspoint.com/java/if_else_statement_in_java.htm