Java 锁定程序非静态变量问题和关闭问题

Java 锁定程序非静态变量问题和关闭问题,java,Java,我对计算机编程非常陌生,我正在尝试创建一个程序,读取用户输入的最后三个字母,然后检查它是否是解锁虚拟保险箱的正确代码。当用户输入“1”时,我似乎无法让程序关闭,这应该是退出的方式。在静态上下文中引用非静态变量“lock”也有一个问题,有什么方法可以解决这个问题吗?谢谢你的帮助 /** * Gets the last three lettersof the user's input. * * @author (Sean F.) * @version (1.0) */ import j

我对计算机编程非常陌生,我正在尝试创建一个程序,读取用户输入的最后三个字母,然后检查它是否是解锁虚拟保险箱的正确代码。当用户输入“1”时,我似乎无法让程序关闭,这应该是退出的方式。在静态上下文中引用非静态变量“lock”也有一个问题,有什么方法可以解决这个问题吗?谢谢你的帮助

/**
 * Gets the last three lettersof the user's input.
 * 
 * @author (Sean F.) 
 * @version (1.0)
 */
import java.util.Scanner;
public class ComboInput
{
public  static void main(String[] args)
{
    System.out.println("\f");
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a letter");
    System.out.print("\n");
    String input = "";
    int comboLength = 0;
    input = in.nextLine();
    input = input.substring(0,1);
    comboLength ++;
    Lock q = new Lock();
    int lock = q.getLockStatus(input);
    String close = "1";
    while (comboLength < 3){
        System.out.println("\f");
        System.out.print("Your letters being used are:");
        System.out.print("\n");
        System.out.println(input);
        System.out.print("Enter another letter or type \"1\" to exit");
        System.out.print("\n");
        String newInput = in.nextLine();
        newInput = newInput.substring(0,1);
        input = input+newInput;
        comboLength++;
    }
    while (input.length()>=3){
        while((input.substring(2)).equals(close)){           
            System.out.println("\f");
            System.out.println("You gave up, lock is still locked");
            System.exit(0);
        }
        while(!((input.substring(2)).equals(close))){
            while (lock == 0){
                while (comboLength >= 3){
                    System.out.println("\f");
                    System.out.print("Your letters being used are:");
                    System.out.print("\n");
                    System.out.println(input.substring(comboLength-3));
                    System.out.print("Enter another letter or type \"1\" to exit");
                    System.out.print("\n");
                    String newInput = in.nextLine();
                    newInput = newInput.substring(0,1);
                    input = input+newInput;
                    comboLength++;
                    int lock2 = q.getLockStatus(input);
                    lock = lock2;
                }  
            }
            while (lock == 1){
                System.out.println("\f");
                System.out.println("Your Combination is Correct. Unlocked");
                System.exit(0);
            }
        }
    }
}

如果用户输入1,我肯定看不到任何要退出的代码,即使提示是这样的。您可以通过在
newInput=newInput.substring(0,1)之后添加此代码来修复此问题

if (newInput.equals(close))
    System.exit(0);
顺便说一下,该代码中有大约20个不同的bug。我将为您省去麻烦并为您编写代码:

import java.util.Scanner;

public class ComboInput {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the combination, or 1 to exit: ");
    String combo = in.readLine();
    if (combo.equals("1"))
        System.exit(0);
    if (combo.equals("smf")) 
        System.out.println("That is the correct combination. The safe is open.");
    else 
        System.out.println("Incorrect combination.");
}

学习这个代码,找出它是如何工作的,并把它看作是编写简单代码的一个教训。


如果您坚持使用
类,那么只有一行需要更改。看看你能不能弄明白

我建议你重写代码,这样就不会那么痛苦了。while循环中的System.exit(0)没有任何意义。你的错误很常见,我建议你用谷歌搜索一下。
import java.util.Scanner;

public class ComboInput {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the combination, or 1 to exit: ");
    String combo = in.readLine();
    if (combo.equals("1"))
        System.exit(0);
    if (combo.equals("smf")) 
        System.out.println("That is the correct combination. The safe is open.");
    else 
        System.out.println("Incorrect combination.");
}