【问题标题】:Strings and While loops字符串和 While 循环
【发布时间】:2012-03-09 22:16:24
【问题描述】:

我认为我在使用字符串和 while 循环时遇到了问题。当我运行这个程序并输入一个动作时,程序什么也不做。它没有退出,它只是坐在那里。这就是为什么我认为这是我的 while 循环的问题。但我认为它也可能在 while 循环之前与我的字符串一起使用。我是否正确声明了这些字符串?或者我在while循环中比较它们是错误的还是什么?感谢您的帮助。

import java.util.Scanner;

public class HW2tester3 {

    public static void main(String args[]) {

        MotorBoat ex3 = new MotorBoat();
        Scanner keyboard = new Scanner(System.in);
        System.out.printf("Input how fast the motorboat was going: ");
        int s = keyboard.nextInt();
        ex3.ChangeSpeed(s);
        System.out.printf("Input the time the motorboat ran: ");
        int t = keyboard.nextInt();
        ex3.OperatingTime(t);

        // Ask the user what action he or she wants to take
        System.out.printf("If you want your distance travelled type:" + 
                          " distance\n");
        System.out.printf("If you want how much fuel you used type: fuel\n");
        System.out.printf("If you want to refuel type: refuel\n");
        System.out.printf("If you are finished type: done\n");
        System.out.printf("What would you like to do? ");

        // Compares the input with the defined strings and preforms the
        //   the action requested
        String a = keyboard.nextLine();
        String b = "done";
        String c = "distance";
        String d = "fuel";
        String e = "refuel";
        if (a != b) {
            while (a != b) {
                a = keyboard.nextLine();
                if (a == c) {
                    ex3.getDistance();
                }
                else if (a == d) {
                    ex3.getFuelUsed();
                }
                else if (a == e) {
                    ex3.Refuel();
                }
            }
        }
        if (a == b) {
            System.exit(0);
        }
    }
}

【问题讨论】:

  • 既然a不等于b,那会发生什么? if 语句的哪个分支被执行?那么接下来会发生什么? keyboard.nextLine() 被评估了多少次?请更新问题,并从if( a!=b) 行开始逐步分析。

标签: java string while-loop


【解决方案1】:

a == b不比较两个字符串的值,而是ab代表同一个对象。 != 也一样。

您想使用a.equals(b) 而不是a == b

【讨论】:

  • 哇哦,我也刚学会了。有点尴尬哈哈。我知道 .equals 的运作方式有点像 == ,但我如何让它像 != 一样运作?
【解决方案2】:

a == b 检查ab 是否是同一个对象,字符串并不总是这样。请改用string.equals()

另外,使用可以区分的变量名称。 abcd不是好的变量名,经常会让你感到困惑。

话虽如此,试试这个:

String input = "";

do {
  input = keyboard.nextLine();

  if (input.equals("distance")) {
    ex3.getDistance();
  } else if (input.equals("fuel")) {
      ex3.getFuelUsed();
  } else if (input.equals("refuel")) {
      ex3.Refuel();
  }
} while (!input.equals("done"));

System.exit(0);

【讨论】:

  • 感谢 do-while 循环!我不知道为什么我没有想到!但这在“ while (!input.equals("done")); ”的条件下的“输入”上会出现“找不到符号”错误,我假设是因为输入是在 do 块中声明的?
  • 可能。尝试在 do {} while 块之外声明 String input; 并使用 input = keyboard.nexLine();
【解决方案3】:

在 java 中你不能用 == 或 != 比较字符串

使用a.equals(b)!a.equals(b)

【讨论】:

    【解决方案4】:

    “==”运算符可用于测试原始值是否相等(即 int、char、boolean...)。

    但是,当您使用“==”运算符比较两个对象引用变量时,您实际上是在测试这两个引用是否指向同一个对象。

    Rectangle box1 = new Rectangle(5, 10, 20, 30); 
    Rectangle box2 = box1;
    Rectangle box3 = new Rectangle(5, 10, 20, 30);
    

    比较:

    box1 == box2; // true;
    

    比较:

    box1 == box3; // false;
    

    使用equals(Object)方法比较对象的内容,如果两个对象的内容相同,则返回true。

    String a = "distance";
    String b = "done";
    if(a.equals(b)){
      //code...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2014-04-26
      相关资源
      最近更新 更多