在switch语句中使用类 [英] Use a class in a switch statement
问题描述
我差不多完成了我的申请,但我有一个小问题。我有一个类(UserChoices),它包含多个变量和函数,我打算在switch语句中使用该类。但声明错误,它说'不兼容类型:UserChoices无法转换为int'
I'm almost done making my application, but I have a slight problem. I have a class(UserChoices) which contains multiple variables and functions and I was planning to use the class in a switch statement. But the statement is wrong, it says 'Incompatible type: UserChoices cannot be converted to int'
package bankapp;
import java.util.*;
import java.text.*;
class UserChoice{
//Variables for logging in
private String user, pass; //username and password where users type in
private String username = "Kate", password = "abc456"; //username and password of the account
//Variables for loan calculator
private int amount, tenure; //the amount and duration of loan
private double interest, MonthlyInterest; //the annual interest and monthly interest
double payment; //monthly payment
private int choose;
//Variables for deposit & withfraw
private int balance, Amount; //balance in the account & the amount of deposit/withdrawal
private String acc, pin; //account number & PIN number
private String AccNumber = "4344789", PinNumber = ""; //account number & PIN number of the account
Scanner scanner = new Scanner(System.in);
public void SignIn(){ //For logging in
System.out.println("Enter your username");
user = scanner.next();
System.out.println("Enter your password");
pass = scanner.next();
if ((user .equals(username)) && (pass .equals(password)))
System.out.println("You've successfuly signed in");
else
System.out.println("Sign in details incorrect");
}
public void Loan(){ //Personal Loan and Housing Loan
System.out.println("Enter the amount you want to borrow");
amount = scanner.nextInt();
System.out.println("Enter the tenure of your loan");
tenure = scanner.nextInt();
System.out.println("Enter the interest rate as a decimal");
interest = scanner.nextDouble();
MonthlyInterest = Math.pow(1+(interest/12), - tenure);
payment = (amount * (interest/12)) / (1 - MonthlyInterest);
DecimalFormat decimal = new DecimalFormat("##.##");
System.out.println("This is the amount you have to pay monthly :" + decimal.format(payment));
System.out.println("-------------------------------");
System.out.println("Do you wish to apply for the loan \n1 Yes \n2 No");
choose = scanner.nextInt();
if (choose ==1)
System.out.println("You have applied for the loan");
else
System.out.println("Your application have been cancelled.");
}
public void Deposit(){ //To deposit into account
System.out.println("Enter account number");
acc = scanner.next();
if (acc .equals(AccNumber))
{ System.out.println("Enter the amount you wish to deposit");
amount = scanner.nextInt();
}
else
System.out.println("Error: account number invalid");
}
public void withdraw(){ //To withdraw from account
System.out.println("Enter pin number");
pin = scanner.next();
if (pin .equals (PinNumber))
{
System.out.println("How much would you like to withdraw ?");
amount = scanner.nextInt();
}
else
System.out.println("Error: Invalid PIN number");
}
}
public class BankApp {
public static void main(String[] args) {
UserChoice choices = new UserChoice();
System.out.println("What would you like to do ? \n1 Sign in \n2 Apply for a Personal Loan \n3 Apply for a Housing Loan \n4 Deposit \n5 Withdraw");
switch (choices){
case 1:
choices.SignIn();
break;
case 2:
choices.Loan();
break;
case 3:
choices.Loan();
break;
case 4:
choices.Deposit();
break;
case 5:
choices.withdraw();
break;
default:
System.out.println("Error: Invalid entry");
}
}
}
我尝试过:
我尝试使用开关(选项== 1),这在上一次在另一个项目中工作。但不是这个。
What I have tried:
I tried using switch(choices == 1),this worked last time in another project. But not this one.
推荐答案
如果查看文档: switch语句(The Java™Tutorials) [ ^ ]关于这个问题非常明确:
If you look at the documentation: The switch Statement (The Java™ Tutorials)[^] it's pretty clear on the subject:
开关使用byte,short,char和int原始数据类型。它也适用于枚举类型(在枚举类型中讨论),String类,以及一些包含某些基本类型的特殊类:Character,Byte,Short和Integer
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer
因为你的类不满足任何类型标准,你不能在开关中使用它。
老实说,你所做的事情看起来非常错误:你的班级处理账户操纵(存款,取款) ,等等)不应直接与用户接口:如果有两个不同的帐户怎么办?用户如何知道他正在影响哪一个?用户界面应与帐户机制分开,并且只有在获取并验证后才能将信息传递给帐户处理类。
Since your class doesn't meet any of those criteria, you cannot use it in a switch.
To be honest, what you are doing looks pretty wrong: your class that deals with the account manipulation (deposits, withdrawals, and so forth) shouldn't be directly interfacing to the user: what if there are two different accounts? How would the user know which one he is affecting? The user interface should be separate from the "account mechanics", and should just pass info to the account handling class once it's been acquired and validated.
这篇关于在switch语句中使用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!