Java Can';我过不了一会儿
嘿,伙计们,我有下面的代码,我将在下面发布 我无法通过第一个循环,而它总是说不正确 我输入1或2,结果显示不正确,有人能帮我吗Java Can';我过不了一会儿,java,while-loop,Java,While Loop,嘿,伙计们,我有下面的代码,我将在下面发布 我无法通过第一个循环,而它总是说不正确 我输入1或2,结果显示不正确,有人能帮我吗 /** * @(#)Assignment2.java * Display a menu to the user. Ask the user if they want to * 1 : Log In 2 : Exit * Ask the user for their Log In PIN * If correct display the customers name an
/**
* @(#)Assignment2.java
* Display a menu to the user. Ask the user if they want to
* 1 : Log In 2 : Exit
* Ask the user for their Log In PIN
* If correct display the customers name and current balance
* Display a menu to the user. Ask the user if they want to
* 1 : Lodge Money 2 : Withdraw money 3 : Change pin 4 : Exit
* This program should keep going until the user enters 4
* @author Eoin Gilmartin
* @version 1.00 2014/2/04
*/
import java.util.Scanner;
public class Assignment2 { // start of class
public static void main (String args[]) { // start of main
Scanner input = new Scanner(System.in); // needed to take inputs from the user
//declare variables
String customers[] = {"Amy","Tim","Bill","Joanne","Josh"};
int pincode[] = {123,234,345,456,567};
int balance[] = {400,225,100,360,600};
int overdraft[] = {1,1,0,0,1};
int lodgement=0, withdrawl=0, choice=0, option = 0, pin=0,pin2=0;
int strchoice=0, stroption=0, strpin=0;
int found =0;
int user=0;
String test="";
String length;
String menu = "\nPlease choose an option \n\n1: Log in\n2: Exit";
String menu2 = "\n\n\t\tl: Lodge Money\n\t\t2: Withdraw money\n\t\t3: Change pin\n\t\t4: Exit\n";
//welcome the user
System.out.println("\n*** Welcome to the Bank of Eoin *** " + menu); // take initial input
stroption = input.nextInt();
System.out.println(stroption);
while(stroption!=1 && stroption !=2){
System.out.println("Error, please enter numbers 1- 2");
// re-prompt the user
System.out.println(menu);
stroption =input.nextInt();
}//end while
if (option == 2){
System.out.println("\nThank you for using my bank, goodbye\n");
System.exit(0);
} // end option=2
else if (option == 1){
System.out.println("Please enter your PIN: ");
strpin = input.nextInt();
//while pin is not 3 digits long
test = String.valueOf(pin);
while(test.length()<3){
System.out.println("Error, Invalid PIN, please try again: ");//error message
strpin = input.nextInt();
}//end while
//check if input is a valid pin
for (int i= 0; i < customers.length; i++){
if (pincode[i] == pin ){
System.out.println("Correct");
System.out.println(customers[i]);
user=i;
found=1;
}//end inner if
}// end for loop
} // end option=1
if (found==0){
System.out.println("\nIncorrect\nGoodbye");
System.exit(0);
}// end found = 0
System.out.println("\nCustomer: " +customers[user] + "\nBalance " + balance[user]+" " +menu2);
strchoice = input.nextInt();
// while loop to validate input
int x=1;
while(x!=2){
if(strchoice!=1 || strchoice!=2 || strchoice!=3|| strchoice!=4){
System.out.println("Error, please enter numbers 1- 4");
// re-prompt the user
System.out.println(menu2);
strchoice =input.nextInt();
}//end if
else
x=2;
}//end while
//loop to keep the menu2 appearing
while (choice!= 4){
if (choice ==1){
System.out.print("\t\tEnter Lodgement: ");
lodgement= input.nextInt();
System.out.println("\t\tYour new balance is: " + (balance[user]+=lodgement)); //(+= is the same thing)
}
else if (choice == 2){
if(overdraft[user]==0){
System.out.print("\t\tEnter withdrawl: ");
withdrawl= input.nextInt();
if((balance[user]-withdrawl)<0){
System.out.println("\t\tERROR INSUFFICIENT FUNDS");
}
}else if(overdraft[user]==1){
System.out.print("\t\tEnter withdrawl");
withdrawl=input.nextInt();
System.out.println("\t\tYour new balance is: " + (balance[user]-= withdrawl));
}
}
else if (choice==3){
System.out.print("\t\tPlease enter your new pin : ");
pin2=input.nextInt();
pincode[user]=pin2;
System.out.println("\t\tYour new pin is: " + pincode[user]);
}
//prompt user
System.out.println("\n\tPlease enter a number 1-4" + menu2);
choice = input.nextInt();
} //end of while loop
System.out.println("\nThank you for using my bank, goodbye\n");
} // end of main
/**
*@(#)Assignment2.java
*向用户显示菜单。询问用户是否愿意
*1:登录2:退出
*询问用户的登录PIN
*如果正确,则显示客户名称和当前余额
*向用户显示菜单。询问用户是否愿意
*1:寄宿2:取款3:更改pin 4:退出
*此程序应一直运行,直到用户输入4
*@作者Eoin Gilmartin
*@version 1.00 2014/2/04
*/
导入java.util.Scanner;
公共类分配2{//类的开始
公共静态void main(字符串args[]){//main的开头
扫描仪输入=新扫描仪(System.in);//需要从用户获取输入
//声明变量
字符串客户[]={“Amy”、“Tim”、“Bill”、“Joanne”、“Josh”};
int pincode[]={12323434456567};
整数余额[]={40022510360600};
整数透支[]={1,1,0,0,1};
int-loddement=0,drawrel=0,choice=0,option=0,pin=0,pin2=0;
int-strchoice=0,stroption=0,strpin=0;
int=0;
int user=0;
字符串测试=”;
字符串长度;
字符串菜单=“\n请选择一个选项\n\n1:登录\n2:退出”;
String menu2=“\n\n\t\tl:Lodge Money\n\t\t2:draw Money\n\t\t3:Change pin\n\t\t4:Exit\n”;
//欢迎用户
System.out.println(“\n***欢迎来到Eoin银行***”+菜单);//进行初始输入
stroption=input.nextInt();
系统输出打印项次(选择);
while(stroption!=1&&stroption!=2){
System.out.println(“错误,请输入数字1-2”);
//重新提示用户
系统输出打印项次(菜单);
stroption=input.nextInt();
}//结束时
如果(选项==2){
System.out.println(“\n感谢您使用我的银行,再见\n”);
系统出口(0);
}//结束选项=2
否则如果(选项==1){
System.out.println(“请输入您的PIN:”);
strpin=input.nextInt();
//而pin的长度不是3位数
测试=字符串.valueOf(pin);
while(test.length()您缺少这一行option=stroption;
,请将它添加到第一个while循环之后,如下所示:
while(stroption!=1 && stroption !=2){
System.out.println("Error, please enter numbers 1- 2");
// re-prompt the user
System.out.println(menu);
stroption =input.nextInt();
}//end while
option = stroption; // <<<---added this line
if (option == 2){
System.out.println("\nThank you for using my bank, goodbye\n");
System.exit(0);
} // end option=2 ........
while(stroption!=1&&stroption!=2){
System.out.println(“错误,请输入数字1-2”);
//重新提示用户
系统输出打印项次(菜单);
stroption=input.nextInt();
}//结束时
option=stroption;//您在while
循环后使用了不正确的变量名来检查用户输入的选项
用户输入存储在选项
中,但您正在选中选项
。在条件中,将名称更改为选项
,它应该可以工作
if (stroption == 2){
System.out.println("\nThank you for using my bank, goodbye\n");
System.exit(0);
} // end option=2
else if (stroption == 1){
System.out.println("Please enter your PIN: ");
strpin = input.nextInt();
//while pin is not 3 digits long
test = String.valueOf(pin);
while(test.length()<3){
System.out.println("Error, Invalid PIN, please try again: ");//error message
strpin = input.nextInt();
}//end while
//check if input is a valid pin
for (int i= 0; i < customers.length; i++){
if (pincode[i] == pin ) {
System.out.println("Correct");
System.out.println(customers[i]);
user=i;
found=1;
}//end inner if
}// end for loop
} // end option=1
if(选项==2){
System.out.println(“\n感谢您使用我的银行,再见\n”);
系统出口(0);
}//结束选项=2
else if(stroption==1){
System.out.println(“请输入您的PIN:”);
strpin=input.nextInt();
//而pin的长度不是3位数
测试=字符串.valueOf(pin);
而(test.length()尝试将输入作为行读取,然后将其作为整数解析:
Scanner input = new Scanner(System.in);
System.out.println("Please enter numbers 1- 2");
String stroption = input.nextLine();
int intoption = -1;
while (true){
try{
intoption = Integer.parseInt(stroption);
}
catch(NumberFormatException ex){
System.out.println("Error, not an int!");
intoption = -1;
}
if (intoption != 1 && intoption != 2)
System.out.println("Error, please enter numbers 1- 2");
stroption = input.nextLine();
else
break;
}
你说的是while(stroption!=1&&stroption!=2){
while循环吗?你使用的是像Eclipse之类的IDE吗?在调试器中仔细检查你的代码,看看发生了什么。你可以更好地解释发生了什么。OP似乎不明白控制正在一路下降。