【问题标题】:Java: can you change the value of the same string every time it loops?Java:你能在每次循环时改变同一个字符串的值吗?
【发布时间】:2014-09-12 08:35:55
【问题描述】:

我正在用 java 做一个非常基本的计算器程序,它要求用户输入,并根据他输入的内容(如果“添加”或其他)将两个值相加或相减。

问题是,我希望它循环,所以在每个方程完成并显示结果后,它会再次询问用户是否要加法或减法。有没有什么办法,不使用数组,每次程序循环时改变String的值?

import java.util.Scanner;

public class BasicCalculator {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        for(int i = 0; i<999; i++) {
            System.out.println("Do you want to add or subtract?");
            String answer1 = input.nextLine();
            if (answer1.equalsIgnoreCase("add")) {
                System.out.println("Enter first value");
                int addv1 = input.nextInt();
                System.out.println("Enter second value");
                int addv2 = input.nextInt();
                int ans = addv1 + addv2;
                System.out.println("The answer is: " + ans);
            } else {
                System.out.println("Enter first value");
                int subv1 = input.nextInt();
                System.out.println("Enter second value");
                int subv2 = input.nextInt();
                int ans2 = subv1 - subv2;
                System.out.println("The answer is: " +ans2);
            }
        }
    }
}

【问题讨论】:

标签: java string loops for-loop


【解决方案1】:

每次给 String 变量 answer1 赋值时,它都会引用一个新的 String 对象,而对旧 String 对象的引用会丢失。所以,不,你没有改变同一个字符串的值(你不能,因为字符串是不可变的),你正在改变变量所指的字符串。

【讨论】:

  • 这根本没有解决问题。您是否只是在问题中看到“更改”和“字符串”并直接跳到答案而不查看整个问题?
【解决方案2】:

我不清楚您的问题是什么,但我怀疑是这样的:您输入的每一行都以“换行”字符结尾。 nextInt 函数将从输入中检索下一个整数,但它们不会“使用”换行符。因此,当你回到循环顶部并说

    String answer1 = input.nextLine();

它将为您提供 当前 行上的所有内容,直到换行符。这实际上意味着它会给你一个空字符串,因为这是在前一个整数被消耗后留在当前行的所有内容。

如果您的程序无法运行,请尝试在循环末尾添加一个额外的nextLine()

    for(int i = 0; i<999; i++) {
        System.out.println("Do you want to add or subtract?");
        String answer1 = input.nextLine();
        if (answer1.equalsIgnoreCase("add")) {
           // etc.
        }
        input.nextLine();  // ADD THIS
    }

这将“消耗”剩余的换行符,这样当你回到顶部时,它实际上会让用户输入另一行。

P.S. 请参阅 David 的回答,它为您提供了一种更好的方法来决定何时离开循环。 (但您仍然需要添加nextLine.

【讨论】:

  • 感谢所有答案,但这基本上是我所需要的。干杯。
【解决方案3】:

问题是在你调用int subv2 = input.nextInt();之后,缓冲区有一个换行符,所以下面的循环迭代,input.nextLine()只是一个空行。要解决此问题,请在每次迭代后添加对 nextLine() 的调用。

【讨论】:

  • 错字:input.nextLine,不是answer1.nextLine
【解决方案4】:

虽然其他张贴者认为“字符串是不可变的”是正确的,但您可以使用“无限”do..while 循环来实现您想要的效果,该循环检查break 的条件,如下所示:

do {
        System.out.println("Do you want to add, subtract, or quit?"); // changed this
        String answer1 = input.nextLine();
        if (answer1.equalsIgnoreCase("quit")) {
            // check for "quit" value, and call break if that's what the user wants
            break;
        }
        if (answer1.equalsIgnoreCase("add")) {
            System.out.println("Enter first value");
            int addv1 = input.nextInt();
            System.out.println("Enter second value");
            int addv2 = input.nextInt();
            int ans = addv1 + addv2;
            System.out.println("The answer is: " + ans);
        } else { // this will catch anything that's not "add" or "quit".  You might want to guard against that
            System.out.println("Enter first value");
            int subv1 = input.nextInt();
            System.out.println("Enter second value");
            int subv2 = input.nextInt();
            int ans2 = subv1 - subv2;
            System.out.println("The answer is: " +ans2);
        }
    } while (true); // will repeat forever until `break` is called

【讨论】:

  • 这并不能解决实际问题。但是,指出需要另一种终止循环的方法绝对有用。
  • 谢谢。我想有一种方法让用户终止循环是个好习惯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多