已更正,需要执行 switch 语句并提示用户输入歌曲选择

correction made, needed to implement a switch statement and prompt for user input on song selection

我正在编写一个程序,通知用户有一架三角钢琴并让他们选择弹奏它,但是,一旦用户选择 y 表示是,它就会询问他们想弹奏什么歌曲,打印对第一首歌曲的响应,而不是让用户选择以根据用户的选择提示不同的响应。

下面是可以编译的代码,但无法按照我的预期运行:

import java.util.*;
public class MyPiano {
    private static final String m = null;
    private static final String b = null;
    private static final String c = null;
    private static final String f = null;
    private static final String r = null;
    private static final String d = null;
    //Initialize class variables.
    private int numOfKeys;
    private String pianoMake;
    private String pianoModel;
    private boolean tuned;

    //A constructor that has specific variables assigned to it.
    public MyPiano (int numOfKeys, String pianoMake, String pianoModel, boolean tuned) {
        this.numOfKeys = numOfKeys;
        this.pianoMake = pianoMake;
        this.pianoModel = pianoModel;
        this.tuned = tuned;
        }
        //Created the output that will be displayed to the user. 
    public String toString() {
        return "There is a beautiful " + numOfKeys + " key " + pianoMake + ", " + pianoModel + " in the living room." + 
    "\nIs it tuned?: " + tuned;
        }

    public static void main(String args[]) {
        //Create an instance of household item method.
        MyPiano piano = new MyPiano (88, "Steinway & Sons", "Model M studio grand", true);

        //Output the status of the household item.
        System.out.println(piano.toString());
        Scanner input = new Scanner(System.in);

        char letsPlayASong;

        System.out.println("Would you like to take a stab at playing a song on the piano? Press Y or y for yes and N or n for no.");
        char a = input.next().trim().charAt(0);     
        if(a == 'Y' || a == 'y') {
        //change speed using switch statement
            System.out.print("Which song would you like to play?(Beethoven's Moonlight Sonata, Beethoven's Fur Elise, or Chopin's March Funebre): Type m for Moonlight Sonata, f for Fur Elise, or r for March Funebre.");
            if(b == m) {
                System.out.println("This song is played in C# minor and the first three notes are: G#, C#, and E");
                char b = input.next().trim().charAt(0);
                } else if(c == f) {
                System.out.println("This song was written in A minor with the first two notes being E and D#");
                char c = input.next().trim().charAt(0);
                } else {
                    if(d == r) {
                        System.out.println("This classic, otherwise known as The Funeral March, is to be played in Bb minor, however it can be very tricky!");
                        char d = input.next().trim().charAt(0);
                        }
                    }
        } else {
            if (a == 'N' || a == 'n'); {
                System.exit(0);
                }
            }
        }
    }

您没有在第二个 sysout 之后捕获输入。并且您可以摆脱不必要的字符串变量(m、f、r 等)。

应该是这样的:

System.out.println("Which song would you like to play?(Beethoven's Moonlight Sonata, Beethoven's Fur Elise, or Chopin's March Funebre): Type m for Moonlight Sonata, f for Fur Elise, or r for March Funebre.");
char b = input.next().trim().charAt(0); 
if(b == 'm') {
   System.out.println("This song is played in C# minor and the first three notes are: G#, C#, and E");
} 
else if(b == 'f') {
   System.out.println("This song was written in A minor with the first two notes being E and D#");
} 
else if(b == 'r') {
   System.out.println("This classic, otherwise known as The Funeral March, is to be played in Bb minor, however it can be very tricky!");
}


与注释行 //change speed using switch statement 中提到的不同,您在这里也没有使用 switch 语句。 switch 语句是这样的:

System.out.println("Which song would you like to play?(Beethoven's Moonlight Sonata, Beethoven's Fur Elise, or Chopin's March Funebre): Type m for Moonlight Sonata, f for Fur Elise, or r for March Funebre.");
char b = input.next().trim().charAt(0); 
switch(b) {
    case 'm': System.out.println("This song is played in C# minor and the first three notes are: G#, C#, and E");
              break;
    case 'f': System.out.println("This song was written in A minor with the first two notes being E and D#");
              break;
    case 'r': System.out.println("This classic, otherwise known as The Funeral March, is to be played in Bb minor, however it can be very tricky!");
              break;
}

我用我想出的新代码编辑了我原来问题中发布的代码,使其完全按照我想要的方式工作。