2017-06-29 297 views
-4

只是一個功能很少的銀行的代碼,我只是試圖學習循環的方式。似乎得到「不兼容的操作數類型字符串和整數」錯誤的所有行上有一個if,否則如果。不兼容的操作數類型字符串和int

import java.util.Scanner; 

public class Bank 
{ 
//create variables 
int pac; 
int confirm; 
int pin; 
int Bal_or_Exit; 
public static void main(String args[]) 
{ 
    //Receive any PAC and create if loop for continue or exit 
    Scanner in = new Scanner(System.in); 
    System.out.println("Please Enter your Personal Access Code (P.A.C)"); 
    String pac = in.nextLine(); 
    System.out.println(pac + " is the P.A.C you have just entered."); 
    System.out.println("Press 1 to continue, Press 2 to cancel"); 
    String confirm = in.nextLine(); 
if(confirm == 1) 
//if loop created for confirm or exit...create another if loop for a pin of 0207 
      { 
      System.out.println("Please Enter your Pin"); 
      String pin = in.nextLine(); 

    if(pin == 0207) 
//if loop created for pin, only access if pin=0207..access granted and 
option of viewing Account Balance or Exit    
      { 
       System.out.println("Welcome!"); 
       System.out.println("Press 1 for Balance"); 
       System.out.println("Press 2 to Exit"); 
       String Bal_or_Exit = in.nextLine(); 
//if 1 is pressed, display balance of €2965.33     
      if(Bal_or_Exit == 1) 
       { 
        System.out.println("Your balance is €2965.33"); 
       } 
//if 2 is pressed, display goodbye message     
      else if(Bal_or_Exit == 2) 
       { 
        System.out.println("GoodBye, Have a Nice a Day!"); 
       } 
//if anything else is pressed display error message     
       else 
       { 
        System.out.println("We're Sorry, An Error has Occured"); 
       } 
       } 
//if pin is anything except 0207 , display wrong pin message    
     else 
     { 
      System.out.println("The PIN you Have entered is incorrect"); 
     } 
     } 
//if confirm = 2 (exit), display exit and goodbye message 
else if(confirm == 2) 
{ 
System.out.println("You have selected exit"); 
System.out.println("Have a Nice Day!"); 
} 
//if confirm is not = 1 or 2, display error message 
else 
{ 
    System.out.println("We're Sorry, An Error has Occured"); 
} 
} 
} 
+3

首先第一,Java的**不是**的Javascript – Frakcool

+0

'如果(針== 0207) ',哇,慢慢地。一個'int'沒有前導零,它只會保存'207'。因此,您不應該使用'int'來表示PIN。 – domsson

+0

這是作業/作業嗎?我認爲你應該諮詢你的導師/同學/課本/教程。 – domsson

回答

1

您有錯誤,由於Scanner#nextLine()返回String,所以,當你撥打:

String confirm = in.nextLine(); 

confirmString,然後你想比較:

if(confirm == 1) 

在其他字:

if (String == int) 

您應該:

+0

謝謝!這解決了這個問題:) – Jakebrady

+0

很高興幫助:) – Frakcool

1

不能將字符串與整數進行比較,因爲它們是兩種不同的數據類型。您必須將字符串轉換爲一個整數才能完成此操作。 像這樣:

if(Integer.parseInt(confirm ) == 1) 

另外,您可以將其存儲在字符串變量之前投的用戶輸入。

int confirm = Integer.parseInt(in.nextLine()); 

您還可以將用戶輸入讀取爲整數而不是字符串。

int confirm = in.nextInt(); 

對於價值0207這將是更明智的把它作爲比較,因爲0領先,如果你比較它作爲一個整數此信息會迷路的字符串。要比較字符串,你可以使用equals()方法。

if(pin.equals("0207")) 
1

您的代碼有多個問題。以if(pin == 0207)爲例:

  • pin是一個字符串,因此它不能相比的一些像
  • 串需要通過equals()進行比較,而不是==
  • 0207八進制直譯,即十進制這將是數135

要修復到pin.equals("0207")和TH這種變化pin == 0207 e其他字符串比較也相應地如confirm == 1

您也可以嘗試將字符串解析爲數字,例如Integer.parseInt(confirm) == 1,但由於0207可能是因爲它的原因,因此無論如何您都需要使用String

相關問題