2016-06-13 16 views
0

當用戶註銷時,用戶可能會選擇重新登錄。對於重新登錄:如果用戶錯誤地獲取3次密碼,程序將終止。計數器出錯

System.out.println("You have logged out"); 
System.out.print("Please Enter Pin: "); 
pin2 = sc.nextInt(); 
while (pin != pin2){ 
    while (ctr < 2){ 
    System.out.print("Please Enter Pin: "); 
    pin2 = sc.nextInt(); 
    ctr++; 
    } 
} 
+0

你可以刪除沒有必要的一切,並確切地告訴我們你的問題是什麼?也許你想要的是'while(pin == pin2)' – 2016-06-13 11:08:57

+0

這個代碼有什麼問題? 5點後不停止 –

+1

擺脫那個標籤!現在就做... *不寒而慄*。如果你想結束循環,把你在while條件中檢查的布爾值設置爲false。 – Fildor

回答

0

就像評論中所說的一樣,避免使用標籤和/或轉到代表。 使用類似這樣的東西:

import java.util.Scanner; 

public class AtmMachine { 

public static void main(String[] args){ 

    int actualPin = -1; 
    int sel = 0, pin, pin2, check, ctr = 0, dep, with, bal = 10000; 
    Scanner sc = new Scanner(System.in); 

    while(actualPin == -1) 
    { 
    System.out.print("Please enter a new pin: "); 
    pin = sc.nextInt(); 

    System.out.print("Please verify your new pin: "); 
    pin2 = sc.nextInt(); 
    if(pin == pin2) actualPin = pin; 
    else System.out.println("Error"); 
    } 

    boolean logged = false; 

    while (true){ 

    if(logged){ 
    System.out.println("What would you like to do?"); 
    System.out.println("1 - Check Balance"); 
    System.out.println("2 - Deposite"); 
    System.out.println("3 - Withdraw"); 
    System.out.println("4 - Change Pin"); 
    System.out.println("5 - End Transaction"); 
    sel = sc.nextInt(); 
    switch (sel){ 
     case 1: 
      System.out.println("Your current balance is "+ bal); 
      break; 
     case 2: 
      System.out.println("How much would you want to deposite? "); 
      dep = sc.nextInt(); 
      bal= dep+bal; 
      System.out.println("Your new current balance is "+ bal); 
      break; 
     case 3: 
      System.out.println("How much would you want to Withdraw? "); 
      with = sc.nextInt(); 

      if(with > bal){ 
      System.out.println("You do not have that amount on your account! Please enter again."); 
      } 
      else{ 
      System.out.println("You withdrew "+ with); 
      bal = bal-with; 
      System.out.println("Your new current balance is "+ (bal)); 
      } 
      break; 
     case 4: 
      System.out.print("Please enter a new pin: "); 
      pin = sc.nextInt(); 

      System.out.println("Please verify your new pin: "); 
      pin2 = sc.nextInt(); 
      if(pin == pin2) actualPin = pin; 
      else System.out.println("Error"); 
      break; 
     case 5: 
      logged = false; 
      break; 
     default: 
      break; 
    } 
    else{ 
     System.out.println("Please Enter Pin: "); 
     sel = sc.nextInt(); 
     logged = actualPin == sel; 
    } 
    } 
    } 
} 
1

如果我正確理解你的問題,你會希望有這樣的事情:

while (pin == pin2) { 
     System.out.println("What would you like to do?"); 
     System.out.println("1 - Check Balance"); 
     System.out.println("2 - Deposite"); 
     System.out.println("3 - Withdraw"); 
     System.out.println("4 - Change Pin"); 
     System.out.println("5 - End Transaction"); 
     sel = sc.nextInt(); 

     switch (sel) { 
      case 1: 
       System.out.println("Your current balance is " + bal); 
       break; 
      case 2: 
       System.out.println("How much would you want to deposite? "); 
       dep = sc.nextInt(); 
       bal = dep + bal; 
       System.out.println("Your new current balance is " + bal); 
       break; 
      case 3: 
       System.out.println("How much would you want to Withdraw? "); 
       with = sc.nextInt(); 

       if (with > bal) { 
        System.out.println("You do not have that amount on your       account! Please enter again."); 
       } else { 
        System.out.println("You withdrew " + with); 
        bal = bal - with; 
        System.out.println("Your new current balance is " + (bal)); 
       } 
       break; 
      case 4: 
       System.out.print("Please enter a new pin: "); 
       pin = sc.nextInt(); 

       System.out.println("Please verify your new pin: "); 
       pin2 = sc.nextInt(); 
       break; 
      case 5: 
       System.out.println("Please Enter Pin: "); 
       pin = sc.nextInt(); 
       break; 
      default: 
       break; 
     } 
    } 

基本上,我已經刪除了loop標籤,這是沒有必要的,我認爲這是一個不好的風格。我也改變了這個while條件,所以只要用戶輸入與他在開始時確認的相同的引腳,程序就會運行。此外,我認爲最好在印刷說明後閱讀sel的價值,而不是像以前那樣。

+0

請輸入一個新的針:1234 請驗證您的新針:1234 您想做什麼? 1 - 查詢餘額 2 - 地層堆積 3 - 提取 4 - 更改PIN碼 5 - 結束事務您的目前餘額爲10000 多少錢你想要存入? – greenninja

+0

多數民衆贊成什麼時候運行它。其實與我的一樣。我不明白爲什麼它會自動轉到案例2,而不是再次詢問用戶想要做什麼 – greenninja