我正在用Java创建一个简单的项目来接收和显示大学名称。当我将值0输入到变量val
时,它不允许我通过扫描仪接收输入。但我可以在使用val++
时输入值。请查看我的代码并解释其背后的逻辑。具体来说,我遇到了if (val == 0)
循环的问题。
package com.example.java;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Please click to enter your name: ");
Scanner scn = new Scanner(System.in);
String str = scn.nextLine();
System.out.println("Ooh so your name is " + str);
StringBuilder sb = new StringBuilder();
System.out.println("How may universities are you expecting decisions from ");
int val = scn.nextInt();
if (val == 0) {
val++;
System.out.println("ooh nice. Enter the university you chose");
for (int i = 0; i <= val; i++) {
String waitlist = scn.nextLine();
sb.append(waitlist);
}
val--;
System.out.println("So you are going to "+sb +"!! congrats");
}
if (val >= 4) {
System.out.println("Damnnn!! " + val + " universities!! that's pretty steep");
System.out.println("Enter names of pending universities " + "\n");
for (int i = 0; i <= val; i++) {
String waitlist = scn.nextLine();
sb.append(waitlist + "\n");
}
System.out.println("So you are waiting for the following universities: " +"\n");
System.out.println(sb + "All the best mate!");
} if (val < 4 && val != 0) {
System.out.println("ooh so its just " + val + " universities, the decision should be just round the corner");
System.out.println("Enter names of pending universities " + "\n");
for (int i = 0; i <= val; i++) {
String waitlist = scn.nextLine();
sb.append(waitlist + "\n");
}
System.out.println("So you are waiting for the following universities: " +"\n");
System.out.println(sb + "All the best mate!");
}
}
}
答案 0 :(得分:0)
修复你的大学数量和你的for循环,这样的东西,仍然检查它:
try (Scanner scn = new Scanner(System.in);) {
System.out.println("Please click to enter your name: ");
String str = scn.nextLine();
System.out.println("Ooh so your name is " + str);
System.out.println("How may universities are you expecting decisions from ");
int val = scn.nextInt();
scn.nextLine();
// 0 or negative
if (val <= 0) {
// no universities... that's pretty pessimistic
} else if (val == 1) {
System.out.println("ooh nice. Enter the university you chose");
String university = scn.nextLine();
System.out.println();
System.out.println("So you are going to " + university + "!! congrats");
} else if (val < 4) {
System.out.println("ooh so its just " + val
+ " universities, the decision should be just round the corner");
System.out.println("Enter names of pending universities: ");
StringBuilder sb = new StringBuilder();
// from i = 0 to i < val (NOT i <= val)
// 0 to val would be val iterate val + 1 times
for (int i = 0; i < val; i++) {
String waitlist = scn.nextLine();
sb.append(waitlist).append(System.lineSeparator());
}
System.out.println();
System.out.println("So you are waiting for the following universities: ");
System.out.println(sb);
System.out.println("All the best mate!");
} else if (val >= 4) {
System.out.println("Damnnn!! " + val + " universities!! that's pretty steep");
System.out.println("Enter names of pending universities: ");
StringBuilder sb = new StringBuilder();
// from i = 0 to i < val (NOT i <= val)
// 0 to val would be val iterate val + 1 times
for (int i = 0; i < val; i++) {
String waitlist = scn.nextLine();
sb.append(waitlist).append(System.lineSeparator());
}
System.out.println();
System.out.println("So you are waiting for the following universities: ");
System.out.println(sb);
System.out.println("All the best mate!");
}
}
}