2013-12-19 60 views
1

選擇菜單上的每個選項現在工作正常(感謝您的幫助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; 
     } 
    } 
} 

} 
+0

如果使用{} ELSEIF {} ... – Patrick

回答

2

這不工作的原因是,你可能是在提醒sc.nextInt()多次,當你希望你的用戶只輸入一個值。

您應該將結果存儲在一個變量中,並在所有的if語句中使用該變量。或者,您可以使用switch聲明。

var userEntry = sc.nextInt(); 
sc.nextLine(); // skip to the end of the line 
if (userEntry == 1) { 
    ... 
} else if (userEntry == 2) { 
    ... 
} else if (userEntry == 3) { 
    ... 
} else ... 

var userEntry = sc.nextInt(); 
sc.nextLine(); // skip to the end of the line 
switch (userEntry) { 
    case 1: 
     ... 
    break; 
    case 2: 
     ... 
    break; 
    case 3: 
     ... 
    break; 
    default: 
     ... 
    break; 
} 
+0

這適用於選擇我想要做的事情,但現在當它運行需要我輸入新單詞的if函數之一時,它不會讓我,只是退出if並循環回去。例如,選項3:用戶應能夠搜索歌曲,但會跳過該歌曲並返回菜單。 –

+0

@Michael_D這是因爲nextLine()會在菜單數字後面輸入行尾。你需要在'nextInt()'後面調用nextLine()'(見我的編輯)。 – dasblinkenlight

+0

你先生是救生員,非常感謝你。 –

0

改變,如果到別的,如果採取輸入nextInt()一次性在每個循環

int num = sc.nextInt() 
if (num == 1) { 

} 
else if (num == 2) { 

} 
else if (num == 3) { 

} 
.... 
+0

,對於選擇什麼,我想做的事情,但現在當它運行的,如果需要我的功能,進入一個作品新詞,它不會讓我,只是退出if和循環回來。 例如,選項3:用戶應該能夠搜索歌曲,但會跳過該歌曲並返回菜單。 –