1

这是我的测试程序的代码。我有所有代码以避免运行时或语法错误,但我不知道这是否是逻辑错误

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;
      }
    }
  }
}

它作为一个循环持续运行,直到 ACCESS GRANTED。该程序运行良好,但我的加载系统无法正常工作。我在另一个程序中运行它,也是一种方法,它运行良好。我将它插入到 Test.java 程序中,没有任何反应。有什么想法吗?(我在我的 IDE 中使用 replit.com)

4

1 回答 1

0

正如评论中提到的 user9594794 一样,代码对我来说也很好用。

我唯一的建议是:

  1. 重构你的加载方法:

     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. 好好看看你的循环和条件,并可能重构它们。注释“循环结束”与循环的实际结束不对应。确实,在该点之后循环可能不再有任何迭代(用户 Dov 和密码 1234),但情况可能并非如此。

于 2021-11-18T15:07:28.050 回答