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新手。
答案 0 :(得分:1)
你的if语句永远不会成立。 answer
是String
变量,而counter1
是int。
if (answer.equals(counter1)) {
...
来自docs:
public boolean equals(Object anObject)
将此字符串与指定对象进行比较。当且仅当参数不为null并且是一个String对象时,结果为true,表示与此对象相同的字符序列。