2016-02-20 31 views
-4
private void CreateAccount() { 
     // TODO Auto-generated method stub 
     String firstName, accountType=""; 
     double initialDesposit = 0;      
     boolean valid = false; 
     int Current = 1; 
     int Saving = 2; 
     while (!valid){     //Only can select current or saving account 
      System.out.println("Enter account Type"); 
      System.out.println("1:Current");   
      System.out.println("2:Saving"); 
      accountType = keyboard.nextLine(); //User Input of the Account Type 


      if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals(1) || accountType.equals(2)){ 
       valid = true; //If selection is true 
      }else{ 
       System.out.println("Invalid"); 
      } 
     } 

我想让它,当用户显示选项,他们可以选择数字或字母的选项,而不是输入“保存”用户只需按“2”并记录为保存。如何接受整数或字符串JAVA

+0

字符串不能与整数,试试这个ACCOUNTTYPE .equals(“1”)accountType.equals(“2”) –

+0

因此,查找'accountType.equalsIgnoreCase(“Current”)|| accountType.equalsIgnoreCase( “1”)'? – MadProgrammer

回答

2

我不知道,你的问题是什么,因为你的帖子中不包含一个,但我认为,至少有一个错误,如果你子句中:

if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals(1) || accountType.equals(2)) 

由于accountType是字符串,将其与整数进行比较将始终失败。你可以像下面这样写下来,以便将它与字符串进行比较:

if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals("1") || accountType.equals("2")) 

这应该工作,因为你得到的字符串作为输入。

0

您可以使用Scanner类:

Scanner sc = new Scanner(System.in); 
String userInput = ""; 

if (sc.hasNext()) { 
    userInput = sc.next(); 
} 

if (userInput.equals("Current") || userInput.equals("1")) { 
    // option 1 
} else if (userInput.equals("Saving") || userInput.equals("2")) { 
    // option 2 
} else { 
    // bad or no input 
} 
0
private void CreateAccount() { 
     // TODO Auto-generated method stub 
     String firstName, accountType=""; 
     double initialDesposit = 0;      
     boolean valid = false; 
     int Current = 1; 
     int Saving = 2; 
     while (!valid){     //Only can select current or saving account 
      System.out.println("Enter account Type"); 
      System.out.println("1:Current");   
      System.out.println("2:Saving"); 
      accountType = keyboard.nextLine(); //User Input of the Account Type 


      if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")|| accountType.equals("1") || accountType.equals("2")){ 
       valid = true; //If selection is true 
      }else{ 
       System.out.println("Invalid"); 
      } 
     } 
-1
private void CreateAccount() { 
     // TODO Auto-generated method stub 
     String firstName, =""; 
     int accountType = 0; 
     double initialDesposit = 0;      
     boolean valid = false; 
     int Current = 1; 
     int Saving = 2; 
     while (!valid){     //Only can select current or saving account 
      System.out.println("Enter account Type"); 
      System.out.println("1:Current");   
      System.out.println("2:Saving"); 
      Scanner sc = new Scanner(System.in); 
      accountType = sc.nextInt(); //User Input of the Account Type 


      if (accountType == 1 || accountType == 2){ 
       valid = true; //If selection is true 
      }else{ 
       System.out.println("Invalid"); 
      } 
     } 
+0

从OP的问题“选择数字或字母的选项” – Tunaki

+0

只需将数字放在引号中,然后将其作为字符串类型... Geesh – bene96

+0

不可以。这不是你如何比较Java中的字符串。 – Tunaki

0

我认为这是你在找什么:

private void CreateAccount() { 
    // TODO Auto-generated method stub 
    String firstName, accountType = ""; 
    double initialDesposit = 0; 
    boolean valid = false; 
    int Current = 1; 
    int Saving = 2; 
    Scanner sc = new Scanner(System.in); 
    boolean isInt = false; 
    while (!valid) {     //Only can select current or saving account 
     System.out.println("Enter account Type"); 
     System.out.println("1:Current"); 
     System.out.println("2:Saving"); 
     accountType = sc.nextLine(); //User Input of the Account Type 

     isInt = isInteger(accountType); 

     if (isInt) { 
      if (Integer.parseInt(accountType) == 1 || Integer.parseInt(accountType) == 2) { 
       valid = true; //If selection is true 
      } else { 
       System.out.println("Invalid"); 
      } 
     } else { 
      if (accountType.equalsIgnoreCase("Current") || accountType.equalsIgnoreCase("Saving")) { 
       valid = true; //If selection is true 
      } else { 
       System.out.println("Invalid"); 
      } 
     } 

    } 
} 

public boolean isInteger(String check) { 
    for (int i = 0; i < check.length(); i++) { 
     if (!Character.isDigit(check.charAt(i))) { 
      return false; 
     } 
    } 
    return true; 
} 
相关问题