選擇菜單上的每個選項現在工作正常(感謝您的幫助Tarek Salah和dasblinkenlight)。現在的問題是,當我選擇需要我輸入新單詞的選項時(例如選項3,用戶必須輸入歌曲名稱),它會跳過該單詞並返回到菜單。有誰知道如何阻止這種情況的發生,以便用戶可以真正輸入內容?Java掃描儀如果功能不起作用
import java.util.Scanner;
public class JukeboxApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Jukebox jb = new Jukebox();
boolean check = false;
System.out.println("Please enter the corresponding number to perform said action.");
while (check == false) {
System.out.println("1: Add a song to the JukeBox\n" +
"2: Remove a song from the JukeBox\n" +
"3: Search for a specific song\n" +
"4: Display total price to play all songs\n" +
"5: Display the most expensive song\n" +
"6: Display the shortest song\n" +
"7: Display the most played song\n" +
"8: Display all songs in the JukeBox\n" +
"9: Display all songs by a specific artist\n" +
"10:\n" +
"11: Exit the JukeBox");
int num = sc.nextInt();
if (num == 1) {
System.out.println("Please enter the name of the artist");
String artist = sc.next();
sc.nextLine();
System.out.println("Please enter the title of the song");
String title = sc.nextLine();
System.out.println("Please enter the price of the song");
double price = sc.nextDouble();
System.out.println("Please enter the length of the song");
double length = sc.nextDouble();
Song s1 = new Song(artist, title, price, length);
jb.addSong(s1);
} else if (num == 2) {
System.out.println("Please enter the title of the song you would like to remove");
sc.nextLine();
jb.removeSong(sc.nextLine());
} else if (num == 3) {
System.out.println("Enter the title of the song you are searching for");
jb.searchSong(sc.nextLine());
} else if (num == 4) {
System.out.println(jb.calcTotal());
} else if (num == 5) {
System.out.println(jb.showMostExpensive());
} else if (num == 6) {
System.out.println(jb.showShortest());
} else if (num == 7) {
System.out.println(jb.mostPlayed());
} else if (num == 8) {
jb.displaySongs();
} else if (num == 9) {
System.out.println("Please enter the artist you are searching for");
System.out.println(jb.searchArtist(sc.nextLine()));
} else if (num == 10) {
System.out.println("");
} else if (num == 11) {
System.out.println("Thank you for using my JukeBox.");
check = true;
}
}
}
}
如果使用{} ELSEIF {} ... – Patrick