进行了更正,需要实现 switch 语句并提示用户输入歌曲选择



我编写一个程序,通知用户有一架三角钢琴并为他们提供弹奏它的选项,但是,一旦用户选择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);
                }
            }
        }
    }

您没有在第二个系统输出之后捕获输入。你可以摆脱不必要的字符串变量(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!");
}


您也没有在这里使用 switch 语句,这与注释行中提到的不同 //change speed using switch statement.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;
}

我用我想出的新代码编辑了原始问题中发布的代码,以便它也完全按照我想要的方式工作。

相关内容

  • 没有找到相关文章

最新更新