これは非常に基本的なプログラムのはずですが、Java は初めてです。Scanner を使用して複数の文字列をコンソールに入力し、それらを検出できるようにしたいと考えています。これまでのところ、入力部分を正しく取得することができました。文字列ではなく空のスペースが入力されたときに結果が表示されるようにプログラムを実行したかったのです。奇妙なことに、リターンを 2 回押したときにしか結果を得ることができませんでしたが、リターンを 1 回押した入力が 4 つ以上ある場合は機能します。私のカウンターは、入力された「コース」の数を数えて結果に表示するはずですが、測定値が不正確です。
import java.util.Scanner;
public class Saturn
{
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("For each course in your schedule, enter its building");
System.out.println("code [One code per line ending with an empty line]");
String input;
int counter = 0;
while (!(userInput.nextLine()).isEmpty())
{
input = userInput.nextLine();
counter++;
}
System.out.println("Your schedule consits of " + counter + " courses");
}
}