简体   繁体   中英

For some reason, my loading "circle" will not working when I have it as a method or just normally in an if statement. Anyone know why?

Here is the code that I have for my Test program. I have all the code in order to avoid a runtime or syntax error but I don't know if this is a logic error

import java.util.Scanner;

public class Test {
  static void Load() throws InterruptedException {
    System.out.print("\r-");
    Thread.sleep(300);
    System.out.print("\r\\");
    Thread.sleep(300);
    System.out.print("\r|");
    Thread.sleep(300);
    System.out.print("\r/");
    Thread.sleep(300);
    System.out.print("\r-");
    Thread.sleep(300);
    System.out.print("\r\\");
    Thread.sleep(300);
    System.out.print("\r|");
    Thread.sleep(300);
    System.out.print("\r/");
    Thread.sleep(300);
    System.out.print("\r-");
    Thread.sleep(300);
    System.out.print("\r\\");
    Thread.sleep(300);
    System.out.print("\r|");
    Thread.sleep(300);
    System.out.print("\r/");
    Thread.sleep(300);
    System.out.print("\r-");
    Thread.sleep(300);
    System.out.print("\r\\");
    Thread.sleep(300);
    System.out.print("\r|");
    Thread.sleep(300);
    System.out.print("\r/");
    Thread.sleep(300);
    System.out.print("\r-");
    Thread.sleep(300);
    System.out.print("\r\\");
    Thread.sleep(300);
    System.out.print("\r|");
    Thread.sleep(300);
    System.out.print("\r/");
    Thread.sleep(300);
    System.out.print("\r-");
    Thread.sleep(300);
    System.out.print("\r\\");
    Thread.sleep(300);
    System.out.print("\r|");
    Thread.sleep(300);
    System.out.print("\r/\r");
    Thread.sleep(300);
 }
    public static void main(String[] args) 
  throws InterruptedException {
    // declaring variables
    String user1 = "";
    int password;
    String passyn;
    String account;
    String user2 = "";
    boolean isRunning = true;
    String tryAgain = "";
    int new1Password = 0;
    int new2Password;
    // my code
    while (isRunning) {
      Scanner sc = new Scanner(System.in);
      Load();
      System.out.println("\rWelcome to LOGIN");
      System.out.print("Enter your name(If you don't have an account, type none): ");
      user1 = sc.next();
      if (user1.equals("Dov")) {
        System.out.print("Enter your password: ");
        password = sc.nextInt();
        if (password == 1234) {
          Load();
          System.out.println("\rACCESS GRANTED");
          System.out.printf("Welcome %s", user1);
          isRunning = false;
        } else {
          System.out.print("ACCESS DENIED");
        }
      }
      // end of loop
      else if (user1.equals("None") || user1.equals("none")) {
        System.out.print("Do you have an account: ");
        passyn = sc.nextLine();
        if (passyn.equals("No") || passyn.equals("no")) {
          System.out.print("Type yes to sign up: ");
          account = sc.next();
          if (account.equals("No") || account.equals("No")) {
          } else {
            System.out.print("What is your new username: ");
            user2 = sc.nextLine();
            System.out.print("What is your password(Numbers only!!): ");
            new1Password = sc.nextInt();
            System.out.print("Would you like to return to the login screen: ");
            tryAgain = sc.next();
            if (tryAgain.equals("no") || tryAgain.equals("No")) {
              isRunning = false;
            }
          }
        }
      } else if (user1.equals(user2)) {
        System.out.print("What is your password: ");
        new2Password = sc.nextInt();
        if (new1Password == new2Password) {
        Load();
        System.out.println("\rACCESS GRANTED");
        System.out.printf("Welcome %s", user1);
        isRunning = false;
        } else {
          System.out.println("ACCESS DENIED");
        }
      } else {
        isRunning = false;
      }
    }
  }
}

It works as a loop continuously running until ACCESS GRANTED. The program works fine but my loading system never works. I was running in it in a different program, also as a method and it worked fine. I insert it into the Test.java program and nothing happens. Any thoughts? (I am using replit.com for my IDE)

The code worked fine for me too, as user9594794 mentioned in the comments.

The only suggestions I have are:

  1. Refactor your loading method:

     char[] sequence = {'-', '\\', '|', '/', '-', '\\', '|', '/'}; int n = 3; // or however many times you want the spinning text for (int i = 1; i < n; i++) { for (char c: sequence) { System.out.print("\r" + c); Thread.sleep(300); } }
  2. Take a good look at your loop and conditions and possibly refactor those as well. The comment "end of loop" doesn't correspond with the actual end of the loop. It is true that the loop may not have any more iterations after that point (user Dov and password 1234) but this may not be the case.

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