2012-11-19 97 views
1
package homework4; 
import java.util.Scanner; 
public class Prog4 { 
static Scanner scanner = new Scanner(System.in); 
public static void main(String[] args) 
{ CreditCardNumber[] cred1; 
    CreditCardNumber cred2 = getInput(); 
    Display("The complete number from your input:", cred2); 
    cred1 = getInputArray(); 
    DisplayArray(cred1); 
    TryAnother(cred1); 
} 

public static CreditCardNumber getInput() { 
    String ID; 
    String accNum; 
    CreditCardNumber tempCred;  
    System.out.printf("Please enter issuer ID:"); 
    ID = scanner.next(); 
    System.out.printf("Please enter account number:"); 
    accNum = scanner.next(); 
    tempCred = new CreditCardNumber(ID, accNum); 

    return tempCred; 
} 
public static void Display(String ch, CreditCardNumber cred2) 
{ 

    System.out.println(ch); 
    System.out.println(cred2.toString().replaceAll(".{4}", "$0 "));  
} 

public static CreditCardNumber[] getInputArray() 
{ 
    CreditCardNumber[] tempArray; 
    String tempID; 
    int size;  
    System.out.printf("Please enter size of the aray:"); 
    size = scanner.nextInt(); 
    if(size < 1) 
    { 
     size = 1; 
    } 
    tempArray = new CreditCardNumber[size]; 
    System.out.printf("Please enter issuer ID:"); 
    tempID = scanner.next(); 
    for(int i = 0; i < tempArray.length; i++) 
    { 
     tempArray[i] = new CreditCardNumber(); 
     tempArray[i].CreateCred(tempID); 
    } 

    return tempArray; 
} 

public static void DisplayArray(CreditCardNumber[] cred1) 
{ 
    for(int i = 0; i< cred1.length; i++) 
    { 
     Display("Credit Card # " + i+":" + '\n', cred1[i]); 
    } 
    System.out.println(); 
} 

public static boolean TryAnother(CreditCardNumber[] cred1) 
{ 
    String s; 
    System.out.printf("Get more credit card numbers? (n for no):"); 
    s = scanner.next(); 
    while(s.charAt(0) != 'N' && s.charAt(0) != 'n') 
    {          
      cred1 = getInputArray(); 
      DisplayArray(cred1);  
      if(s.charAt(0) != 'N' && s.charAt(0) != 'n') 
      TryAnother(cred1); 
    }  


    return false; 
} 
} 

時喜時,我回答沒有過程不會終止,而是提示用戶再次恩特數組的大小這樣我與Tryanother方法的問題:過程不會終止進入無

Please enter issuer ID:321321 
Please enter account number:654654654 
The complete number from your input: 
3213 2165 4654 6549 
Please enter size of the aray:7 
Please enter issuer ID:789789 
Credit Card # 0: 

7897 8987 8895 1653 
Credit Card # 1: 

7897 8935 5880 5197 
Credit Card # 2: 

7897 8997 8152 8134 
Credit Card # 3: 

7897 8996 1605 8067 
Credit Card # 4: 

7897 8953 2006 5521 
Credit Card # 5: 

7897 8964 3319 8191 
Credit Card # 6: 

7897 8986 1830 7068 

Get more credit card numbers? (n for no):y 
Please enter size of the aray:3 
Please enter issuer ID:345345 
Credit Card # 0: 

3453 4590 2392 9734 
Credit Card # 1: 

3453 4549 5025 1905 
Credit Card # 2: 

3453 4504 2989 0927 

Get more credit card numbers? (n for no):n 
Please enter size of the aray: 

你可以看到當我輸入'y'程序工作時,它提示用戶輸入數組的大小,但是當我輸入'n'而不是終止程序時提示用戶輸入數組的大小

我該如何解決這個問題?

預先感謝您

回答

2

只注意到你有你的s.charAt(0) != 'N' && s.charAt(0) != 'n' while循環中,並遞歸調用循環內的相同的功能。

它更改爲if(s.charAt(0) != 'N' && s.charAt(0) != 'n')

+0

否它不,這是我以前的,它不工作 – user1787811

+0

謝謝,我沒有意識到,通過將while循環放在那裏它不會再檢查條件時,它是在內循環 – user1787811

1

使用String.startsWith()方法。

while(!s.startsWith("n") && !s.startsWith("N")) 

但它應該仍然與您的方法一起工作,我正在通過它看。 :)

+0

不會讓它更復雜嗎?因爲如果s包含n和N,它將返回假 – user1787811

+0

@ user1787811 nope,如果s包含'n',它將不檢查第二個操作數。 。但你的方法仍應該工作..我在看你的週期 – PermGenError

+0

@MarcoLeogrande啊,yepp,我現在編輯我的代碼:) – PermGenError