结束时循环的字符串不起作用

发布于 2025-01-18 10:11:07 字数 1397 浏览 0 评论 0原文

我在尝试结束这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗳卜坏 2025-01-25 10:11:07

while (name.contains("exit")==false) 更改为 while(true)

并添加行 if (name.equals('exit') ) 在 name = scan.nextLine(); 之后break;

Change while (name.contains("exit")==false) to while(true)

and add line if (name.equals('exit')) break; after name = scan.nextLine();

许久 2025-01-25 10:11:07

首先您可以更改
name.contains(“ exit”)== false in !name.contains(“ exit”)

一个更好的解决方案是使用一个true循环,如果名称变量等于“退出”

while (true) {
   System.out.println("Please enter the name(exit to stop): ");
   name = scan.nextLine();
   if(name.equals("exit"))
      break;

注意,请注意,在这种情况下,您不应使用.contains()或<代码> .Equals 而不是.equalsignorecase()

First of all you could change
name.contains("exit")==falseinto !name.contains("exit")

A better solution would be to use a while true loop and break if the name variable is equal to "exit"

while (true) {
   System.out.println("Please enter the name(exit to stop): ");
   name = scan.nextLine();
   if(name.equals("exit"))
      break;

Notice that in cases like this you shouldn't use .contains() or .equals but rather .equalsIgnoreCase()

昔梦 2025-01-25 10:11:07

这应该有效

while(true){
        System.out.println("Please enter the name(exit to stop): ");
        name = scan.nextLine();

        if(name.equals("exit"))
            break;

        System.out.println("Please enter the deposit: ");
        double balance = scan.nextDouble();
        scan.nextLine();
        BankAccount newAccount = new BankAccount(name, balance);
        bank.add(newAccount);
}

This should work

while(true){
        System.out.println("Please enter the name(exit to stop): ");
        name = scan.nextLine();

        if(name.equals("exit"))
            break;

        System.out.println("Please enter the deposit: ");
        double balance = scan.nextDouble();
        scan.nextLine();
        BankAccount newAccount = new BankAccount(name, balance);
        bank.add(newAccount);
}
公布 2025-01-25 10:11:07

按如下方式修改 while 循环:

                while (true) {
                System.out.println("Please enter the name(exit to stop): ");
                name = scan.nextLine();
                if(name.equals("exit"))
                break;
                System.out.println("Please enter the deposit: ");
                double balance = scan.nextDouble();
                scan.nextLine();
                BankAccount newAccount = new BankAccount(name, balance);
                bank.add(newAccount);
            }

Modify your while loop as follows:

                while (true) {
                System.out.println("Please enter the name(exit to stop): ");
                name = scan.nextLine();
                if(name.equals("exit"))
                break;
                System.out.println("Please enter the deposit: ");
                double balance = scan.nextDouble();
                scan.nextLine();
                BankAccount newAccount = new BankAccount(name, balance);
                bank.add(newAccount);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文