繁体   English   中英

如果功能不起作用,则进入Java扫描程序

[英]Java scanner in if functions not working

现在,选择菜单上的每个选项都可以正常工作(感谢您获得Tarek Salah和dasblinkenlight的帮助)。 现在的问题是,当我选择一个要求输入新词的选项时(例如,选项3,用户必须输入歌曲的名称),它会跳过该选项并返回菜单。 有谁知道如何阻止这种情况发生,以便用户可以实际输入内容?

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;
        }
    }
}

}

这样做不起作用的原因是,当您希望用户仅输入一个值时,您可能多次调用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;
}

将if更改为else if,并在每个循环中输入一次nextInt()

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

}
else if ( num == 2 ) {

}
else if ( num == 3 ) {

}
 ....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM