简体   繁体   中英

My program doesn't run the main method, why is that?

For some reason, my main method doesn't run. I can't figure out why.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

import static java.lang.System.out;

public class Main {
   // public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static Scanner input = new Scanner(System.in);
    public static Scanner stringInput = new Scanner(System.in);
    public static int UInput = input.nextInt();
    public static String[] usrNamePass = new String[3];

    public static void main(String[] args) throws IOException {
        out.println("Welcome to XYZ Bank! Please Login by typing  \n  Register by pressing 1");
        if(UInput == 1){
            registrationMsg();
        }
    }

    static void registrationMsg() throws IOException {
        out.println("Welcome to registration, please type your username followed by your password ");
        registration();
    }

    static void registration() throws IOException {
        out.println("Enter a username");
        usrNamePass[1] = input.next();
        out.println("Enter Password");
        usrNamePass[2] = input.next();
        out.println("Confirm your password");
        usrNamePass[3] = input.next();

        if (usrNamePass[2] == usrNamePass[3]){
            out.println("Are you sure you want your username to be " + usrNamePass[1]);
            Login();
        }
        else {
            out.println("Passwords do not match, please try again by pressing 1");
            if (UInput == 1){
                registration();
            }
        }

    }

 


}

I have no idea why it doesn't run the main method. This is the main file that was automatically created by my IDE; Intellij

I have done similar programs in the past and they all worked as they should've. I am confused as to why my main method doesn't even run.

public static int UInput = input.nextInt(); is the static initializer, so it's called before println in your main() method, therefore your app awaits for the user input before it prints anything to the console.

You may want to replace your code with something like this:

public static int UInput = 0;

...

    public static void main(String[] args) throws IOException {
        out.println("Welcome to XYZ Bank! Please Login by typing  \n  Register by pressing 1");
        UInput = input.nextInt();
        if (UInput == 1) {
            registrationMsg();
        }
    }

It is working fine, you need to input an int after running the code in order to execute it further.

这是它的外观

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM