-2

Im writing a program that informs the user that there is a grand piano and gives them the option to play it, however, once the user chooses y for yes it asks the question what song do they want to play however, prints the response to the first song, rather than allowing the user to choose in order to prompt the different responses based on user's choice.

Below is the code, which compiles, however does not work the way I'm intending:

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);
                }
            }
        }
    }
  • 1
    why are you having b,c,d variables? You can do that with just one variable and using switch statements...also you are reading the value to the variables b,c,d in the if clause that doesn't seem right. Can you re-check the code you posted here. – kondu Feb 19 '16 at 05:19
  • I figured it out eventually after messing around with the code only to find out someone had responded to my question. If you take a look at what I reposted maybe it'll make more sense now that I got it to work/do exactly what i wanted – 3monkeys1gorilla Feb 19 '16 at 06:19
  • Hi there. Thanks for wanting to provide an answer. This is normally something that goes below the question, but since this closed, you can add it as an addendum to the original question. Please do not replace the original question though, since an answer with no question makes no sense to new readers. – halfer Mar 06 '16 at 18:59

2 Answers2

1

You are not capturing the input after the second sysout. And you can get rid of unnecessary String variables (m, f, r, etc).

It should be like this:

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!");
}


You also are not using switch statement here unlike mentioned in a comment line //change speed using switch statement. A switch statement would be like this:
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;
}
Sameer Mirji
  • 2,135
  • 16
  • 28
  • thank you for your answers, I accepted yours even though i got it to work properly and the way I wanted it to originally before even realizing that someone had posted an answer to my question. Thank you for you reply regardless :) – 3monkeys1gorilla Feb 19 '16 at 06:20
  • Glad it came of help. – Sameer Mirji Feb 19 '16 at 06:23
0

I edited the code posted in my original question with the new code that I figured out so that it works exactly the way I wanted it too.