I am new to Java and programming in general.

In my program, I use if statements to read specific user input. But, when I enter the code "75832", it processes it as if I entered "24231". Also, if I enter anything besides those numbers, it processes it as if I entered "24231". I've done some other basic programs like this before, but I don't know what I did or am doing wrong.

Any help?

static void gateMethod() throws InterruptedException
{

String stageCode;
int randomChunk;

Random generator = new Random();
Scanner input = new Scanner (System.in);

randomChunk = generator.nextInt(6);
System.out.println("Welcome to Datahub v.1");
System.out.println("\rEnter a phrase or number: ");
stageCode = input.nextLine();

if (stageCode.equals("24231"));
{
    if(randomChunk == 0)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 1/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();

    }
    else if(randomChunk == 1)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 2/6;");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();

    }
    else if(randomChunk == 2)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 3/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();
    }
    else if(randomChunk == 3)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 4/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();   
    }
    else if(randomChunk == 4)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 5/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();   
    }
    else if(randomChunk == 5)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 6/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();   
    }
}
if (stageCode.equals("75832"));
{
    System.out.println("");
    System.out.println("Data correct! Please wait...");
    Thread.sleep(2000);
    System.out.println("text");
    System.out.println("more text");
}   

}

有帮助吗?

解决方案

Remove the semicolon from the end of this line:

if (stageCode.equals("24231"));

and from this line:

if (stageCode.equals("75832"));

Java will treat the semicolon as the entire body of the if condition here. Then the block in braces following it will always be executed, because it's not attached to the if condition as you expected it to be.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top