结束时循环的字符串不起作用
我在尝试结束这个 while 循环时遇到问题。当它运行时,在名称部分输入“exit”似乎没有注册,并且代码继续运行,就好像“exit”是输入的名称一样。我想要发生的是输入“exit”,以便循环可以结束。有什么帮助吗?这是该类的代码:
import java.util.*;
import java.io.*;
public class BankTest {
public static void main(String args[]) {
List<BankAccount> bank = new ArrayList<BankAccount>();
Scanner scan = new Scanner(System.in);
//enter into list the accounts and end with exit typed in
String name = "";
while (name.contains("exit")==false) {
System.out.println("Please enter the name(exit to stop): ");
name = scan.nextLine();
System.out.println("Please enter the deposit: ");
double balance = scan.nextDouble();
scan.nextLine();
BankAccount newAccount = new BankAccount(name, balance);
bank.add(newAccount);
}
//find highest account amount
double largestMon = bank.get(0).getBalance();
String largestPer = bank.get(0).getName();
for (int i=0;i<bank.size();i++) {
double compare = bank.get(i).getBalance();
if (compare>largestMon) {
largestMon = compare;
largestPer = bank.get(i).getName();
}
}
System.out.println(largestPer+" has the largest account with $");
}
}
我已经尝试在 != 和 equals() 之间切换来比较字符串。
I have a problem with trying to end this while loop. When it runs, typing "exit" for the name part doesn't seem to register and the code continues as if "exit" was a name typed in. What I intended to happen is for "exit" to be typed in so that the loop can end. Any help? Here is the code for the class:
import java.util.*;
import java.io.*;
public class BankTest {
public static void main(String args[]) {
List<BankAccount> bank = new ArrayList<BankAccount>();
Scanner scan = new Scanner(System.in);
//enter into list the accounts and end with exit typed in
String name = "";
while (name.contains("exit")==false) {
System.out.println("Please enter the name(exit to stop): ");
name = scan.nextLine();
System.out.println("Please enter the deposit: ");
double balance = scan.nextDouble();
scan.nextLine();
BankAccount newAccount = new BankAccount(name, balance);
bank.add(newAccount);
}
//find highest account amount
double largestMon = bank.get(0).getBalance();
String largestPer = bank.get(0).getName();
for (int i=0;i<bank.size();i++) {
double compare = bank.get(i).getBalance();
if (compare>largestMon) {
largestMon = compare;
largestPer = bank.get(i).getName();
}
}
System.out.println(largestPer+" has the largest account with quot;);
}
}
I've already tried switching between != and equals() compare the strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
while (name.contains("exit")==false)
更改为while(true)
并添加行
if (name.equals('exit') ) 在
name = scan.nextLine();
之后break;Change
while (name.contains("exit")==false)
towhile(true)
and add line
if (name.equals('exit')) break;
aftername = scan.nextLine();
首先您可以更改
name.contains(“ exit”)== false
in!name.contains(“ exit”)
一个更好的解决方案是使用一个true循环,如果名称变量等于“退出”
注意,请注意,在这种情况下,您不应使用
.contains()
或<代码> .Equals 而不是.equalsignorecase()
First of all you could change
name.contains("exit")==false
into!name.contains("exit")
A better solution would be to use a while true loop and break if the name variable is equal to "exit"
Notice that in cases like this you shouldn't use
.contains()
or.equals
but rather.equalsIgnoreCase()
这应该有效
This should work
按如下方式修改 while 循环:
Modify your while loop as follows: