2013-11-03 40 views
0

我對計算機編程非常陌生,並且正在嘗試創建一個程序來讀取用戶輸入中的最後三個字母,然後檢查它是否是正確的代碼解鎖一個虛擬保險箱。當用戶輸入本應該退出的方式時,我似乎無法讓程序關閉。在靜態上下文中引用非靜態變量「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); 
      } 
     } 
    } 
} 

}

這是該計劃的第二部分。這是一個被稱爲鎖定

/** 
* Gives the current status of the lock 
*/ 
public class Lock 
{ 
private String code; 
/** 
* Constructs the correct code for the lock 
*/ 
public Lock() 
{ 
    code = "smf"; 
} 

/** 
* 
* 
* @return  An integer to describe whether lock is open or not 
*/ 
public int getLockStatus(String c) 
{ 
     if (c.equalsIgnoreCase(code)){ 
      return 1; 
     } 
     else{ 
      return 0; 
     }  
} 
+0

我建議你重寫代碼,以免它受到如此的折磨。例如。 while循環中的System.exit(0)沒有任何意義。你的錯誤是很常見的,我建議你谷歌它。 –

回答

0

我肯定不看,如果用戶輸入1中的任何代碼退出類,即使提示說。您可以修復,通過newInput = newInput.substring(0,1);後添加以下代碼:

if (newInput.equals(close)) 
    System.exit(0); 

順便說一句,該代碼中有大約20個不同的錯誤。我會救你的麻煩和編寫代碼爲您提供:

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."); 
} 

研究這個代碼,並找出它是如何工作的,並認爲這是編寫簡單的代碼的一課。

如果您堅持要使用Lock類,則只需要更改一行。看看你能否弄清楚。