简体   繁体   English

出于某种原因,当我将其作为方法或通常在 if 语句中使用时,我的加载“圆圈”将不起作用。 有谁知道为什么?

[英]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.它作为一个循环持续运行,直到 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.我将它插入到 Test.java 程序中,没有任何反应。 Any thoughts?有什么想法吗? (I am using replit.com for my IDE) (我的 IDE 使用 replit.com)

The code worked fine for me too, as user9594794 mentioned in the comments.正如评论中提到的 user9594794 一样,代码对我来说也很好用。

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.确实,在该点之后循环可能不再有任何迭代(用户 Dov 和密码 1234),但情况可能并非如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的Paint方法运行了两次,我不明白为什么。我怎么能解决这个问题,有谁知道为什么会这样? - My Paint method is running twice and I have no idea why. How can I fix this, and does anyone know why this is happening? 由于某种原因,这崩溃了,我不知道为什么 - For some reason this is crashing and I have no idea why 我的程序中的 if 语句由于某种未知原因停止工作 - The if statement in my program stopped working for some unknown reason 有谁知道为什么我的 else if 语句不起作用? - Does anyone know why my else if statements are not working? 我有一个返回 int 的方法,它告诉我缺少返回语句。 可以在不重写方法的情况下解决这个问题吗? - I have a method that returns a int, and it tells me i'm missing a return statement. Can this be fixed without rewriting the method? 为什么我在 class 中的方法只绘制第一个圆圈,但如果我只是在 for 循环中使用填充方法,它会显示所有圆圈? - Why does my method inside a class only draw the first circle but if i just use filloval method inside a for loop it displays all circles? 任何人都知道为什么这些if else声明不起作用? - Anyone know why these if else statement wont work? 我的菜单由于某种原因无法正常工作 - My menu is not working for some reason 我的 actionPerformed 方法(Java)不起作用,我不知道为什么 - My actionPerformed method(Java) is not working and I have no clue why 为什么我检查是否有括号等的方法不起作用的任何原因? - Any reason why my method to check if there is even parenthesis etc is not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM