我做了银行帐户课程:
package bankaccount;
public class BankAccount {
long accountNo=0+(long)(Math.random()*999999999999999999L);
String name;
double balance=0;
double amount;
public double deposit(double amount){
balance=balance+amount;
return balance;
}
public double withdraw(double amount){
balance=balance-amount;
return balance;
}
public void BalancInquiry(){
System.out.println("your ammount of balance is: "+balance+" SAR ");
}
public void PrintInfo(){
System.out.println("------------------------------------\n");
System.out.println("Account Nymber: "+accountNo);
System.out.println("Account Name: "+name);
System.out.println("Balance: "+balance+" SAR");
}
}
在驱动程序类中我有问题,名称的input.nextLine()不允许我插入值!它将它传递给下一行,这是输出:
run: Do you want to create an Count? if yes choose 1 if no choose
2 1
Creating your account: Enter your name: You have finished createing
your account! Thank you.
------------------------------------
choose operation do you want to do:
1 Add new palance
2 withdraw palance
3 show your palance
4 print your account info
这是驱动程序类代码:
package bankaccount;
import java.util.Scanner;
public class BankAccountTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int AccountMaking = 0;
int newPalance=0;
int operation=0;
System.out.println("Do you want to create an Count? \n if yes choose 1 \n if no choose 2");
AccountMaking=input.nextInt();
switch(AccountMaking){
case 1:
BankAccount account1= new BankAccount();
System.out.println("Creating your account:");
System.out.println("Enter your name: ");
account1.name=input.nextLine();
System.out.println("You have finished createing your account! \n Thank you. ");
System.out.println("------------------------------------\n");
System.out.println("choose operation do you want to do: \n 1 Add new palance \n 2 withdraw palance"
+ " \n 3 show your palance\n 4 print your account info");
operation=input.nextInt();
switch (operation){
case 1:
System.out.println("Enter the amount of balance you want to Add: ");
account1.amount=input.nextDouble();
System.out.println(account1.deposit(account1.amount));
System.out.println("Your new planace has added! \n Thank you. ");
break;
case 2:
System.out.println("Enter the amount of balance you want to withdraw: ");
account1.amount=input.nextDouble();
System.out.println(account1.withdraw(account1.amount));
System.out.println("Your new planace has withdrawn! \n Thank you. ");
break;
case 3:
account1.BalancInquiry();
System.out.println("Your balance has shown! \n Thank you. ");
break;
case 4:
account1.PrintInfo();
break;
default:
System.out.println("we don't have this operation, choose one of the number of operations above! \n Thank ");
break;
}
default:
System.out.println("Thank you for using this application \n GoodBye! ");
}
}
}
注意:我使用nextLine(),因为我想输入全名,我尝试使用next但是当我输入完整的neme并且仅用于名字时显示错误,这是我使用时显示的错误next()表示多个单词:
<!-- language: lang-none -->
run:
Do you want to create an Count?
if yes choose 1
if no choose 2
Zahraa Maher
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at bankaccount.BankAccountTest.main(BankAccountTest.java:14)
/Users/Zahraa_maher/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 6 seconds)
我希望你能帮助我找到解决方案:(