我正在为项目制作银行机器代码,每当您登录时都会出错。正在执行此操作的代码部分是:
if(pincheck == pin){
loggedin = true;
pincheck = 0;
do{
System.out.println("Welcome, " + name);
System.out.println("");
System.out.println("Your account balance is $" + balance);
System.out.println("");
System.out.println("Press 1 to deposit funds");
System.out.println("Press 2 to withdraw funds");
System.out.println("Press 3 to log out");
System.out.println("");
options = in.nextInt();
switch (options) {
case 1: System.out.println("How much would you like to deposit?"); // deposit
deposit = in.nextFloat();
balance = balance + deposit;
deposit = 0;
System.out.println("You have deposited funds into your account."); // withdraw
System.out.println("");
break;
case 2: System.out.println("How much would you like to withdraw?");
withdraw = in.nextFloat();
balance = balance - withdraw;
withdraw = 0;
System.out.println("You have removed funds from your account.");
System.out.println("");
break;
case 3: System.out.println("Logging out..."); // log out
System.out.println("");
loggedin = false;
break;
default:System.out.println("Please enter a valid number"); // Invalid number
break;
}
}while(loggedin = true);
正在发生的事情是登录你需要输入一个数字,作为pincheck,如果它等于存在的引脚它将登录你。我可以登录但是当我按3退出时,它会打印退出并打印欢迎,整个事情再次开始。有谁可以指出我被困在哪里?
答案 0 :(得分:2)
=
是一个赋值运算符,因此您只需指定值(即设置loggedin=true
),该值始终为true
(因为您设置为true
)。
因此,您没有检查循环中的实际条件,因此您需要更正while
条件,如下所示,它使用==
运算符(用于计算条件表达式):
while(loggedin == true); //use == for condition evaluation