我正在編寫一個程序,通知用戶有一架大鋼琴並給了他們播放它的選項,但是,一旦用戶選擇了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);
}
}
}
}
爲什麼你有b,c,d變量?你可以用一個變量和switch語句做到這一點......你也可以在if子句中讀取變量b,c,d的值,這看起來不正確。你能重新檢查你在這裏發佈的代碼嗎? – kondu
我在弄清代碼後發現最終發現有人回答了我的問題。如果你看看我轉貼的內容,現在我可以更有意義地瞭解我的工作/完全按照我的意願去做 – 3monkeys1gorilla
Hi there。感謝您想提供答案。這通常是在問題之下,但是由於這個問題已經結束,您可以將它作爲原始問題的附錄添加。儘管如此,請不要替換原來的問題,因爲毫無疑問的答案對新讀者毫無意義。 – halfer