-3
import java.io.*;

public class AdamHmwk4 {
   public static void main(String[] args) throws IOException {

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      int counter1;
      int counter2;
      int counter3;
      String answer = "";

      System.out.println("Welcome to Adam's skip-counting program!");
      System.out.println("Please input the number you would like to skip count by.");
      counter1 = Integer.parseInt(br.readLine());

      if (answer.equals(counter1)) {
         System.out.println("Please input the number you would like to start at.");
         counter2 = Integer.parseInt(br.readLine());

         if (answer.equals(counter2)) {
            System.out.println("Please input the number you would like to stop at.");
            counter3 = Integer.parseInt(br.readLine());

            if (answer.equals(counter3)) {
               System.out.println("This is skip counting by");
               System.out.println(counter1);
               System.out.println(",starting from");
               System.out.println(counter2);
               System.out.println("and ending at");
               System.out.println(counter3);

            }
         }
      }
   }
}

当我编译并运行这段代码时,第一部分执行得很好,但是当我为它输入一个数字时,下一个用户输入部分不显示。请记住,我是 Java 新手。

4

1 回答 1

1

你的 if 语句永远不会是真的。answer是一个String变量,whilecounter1是一个 int。

if (answer.equals(counter1)) {
   ...

文档

公共布尔等于(对象anObject)

将此字符串与指定对象进行比较。当且仅当参数不为 null 并且是表示与此对象相同的字符序列的String 对象时,结果才为真。

于 2013-08-13T21:52:19.077 回答