在我的程序中,用户必须选择他们想要做的事情,然后点击选择旁边的数字,然后点击回车。
现在我已经拥有了它,所以任何不是选择的数字都会出错,但现在我想确保如果用户输入一个字母中的错误,例如“fadhahafvfgfh”
这是我的代码......
import java.util.Scanner;
public class AccountMain {
public static void selectAccount(){
System.out.println("Which account would you like to access?");
System.out.println();
System.out.println("1 = Business Account ");
System.out.println("2 = Savings Account");
System.out.println("3 = Checkings Account");
System.out.println("4 = Return to Main Menu");
menuAccount();
}
public static void menuAccount(){
BankMain main = new BankMain();
BankMainSub sub = new BankMainSub();
BankMainPart3 main5 = new BankMainPart3();
Scanner account = new Scanner(System.in);
int actNum = account.nextInt();
if (actNum == 1){
System.out.println("*Business Account*");
sub.businessAccount();
}
else if (actNum == 2){
System.out.println("*Savings Account*");
main.drawMainMenu();
}
else if (actNum == 3){
System.out.println("*Checkings Account*");
main5.checkingsAccount();
}
else if (actNum == 4){
BankMain.menu();
}
}
}
答案 0 :(得分:5)
您可以使用Scanner#hasNextInt()。
if(account.hasNextInt())
account.nextInt();
如果使用nextInt()方法可以将此扫描器输入中的下一个标记解释为指定基数中的int值,则返回true。扫描仪不会超过任何输入。
如果用户没有输入有效,那么你可以说再见,下次见到你。
int actNum = 0;
if(account.hasNextInt()) {
//Get the account number
actNum = account.nextInt();
}
else
{
return;//Terminate program
}
否则,您可以显示错误消息并要求用户重试有效的帐号。
int actNum = 0;
while (!account.hasNextInt()) {
// Out put error
System.out.println("Invalid Account number. Please enter only digits.");
account.next();//Go to next
}
actNum = account.nextInt();
答案 1 :(得分:2)
Scanner account = new Scanner(System.in);
int count = 0;
while (true and count < 3) {
if (!account.hasNextInt()) {
int actNum = account.nextInt();
break;
} else {
System.out.println("Enter an integer");
count++;
account.next();
}
}
答案 2 :(得分:1)
我认为接受的答案是正确的,但我们应该在代码中使用java功能,例如异常处理
//所以我们的代码看起来应该是这样的
public static void main(String[] args) {
int choice=0;
Scanner scanner = new Scanner(System.in);
StringReverse objReverse = new StringReverse();
System.out.println("Menu");
System.out.println("1.Reverse String");
System.out.println("2.Exit");
do {
System.out.println("Enter your choice");
try {
choice = scanner.nextInt();
switch(choice) {
case 1 :
System.out.println("Enter the string to reverse");
String inputString = new String();
inputString = scanner.next();
String reversedString = objReverse.reverse(inputString );
System.out.println("Reversed String is " + reversedString);
break;
case 2 :
System.out.println("Exiting...");
return;
default :
System.out.println("Default choice");
break;
}
} catch(Exception e) {
System.out.println("Please enter valid integer input");
scanner.next();
}
} while(choice != 2);
}
答案 3 :(得分:0)
扫描程序有hasNextInt()函数,如果下一个标记是整数,则返回true。因此,在调用nextInt()
之前验证hasNextInt()
是否为真。如果失败,则向用户显示一条消息,要求他输入整数。注意,整数不一定需要符合您的要求范围,因此请确保您还有最终else
通知用户他输入的数字无效。
提示:使用Switch Case。
答案 4 :(得分:0)
您可以使用Scanner.hasNextInt()
或Integer.parseInt()
。
Scanner account = new Scanner(System.in);
String actNum = account.next();
try {
Integer.parseInt(actNum);
} catch(ParseException ex) {
sysout("please enter only numeric values")
}