簡體   English   中英

Java:為什么我的While語句不適用於我的案例2?

[英]Java: Why Doesn't My While Statement Work With My Case 2?

我試圖創建一個簡單的程序,當輸入指定的數字時,該程序輸出歌曲的歌詞。 問題是我的第二種情況不會輸出,而只會輸出第一種情況。 當我嘗試輸出第二種情況時,它仍然輸出第一種情況。 無論我是否發表兩次while陳述,這仍然會發生。 有人可以幫我修復我的代碼嗎?

這里是:

import java.util.Scanner;
public class Lyrics {
public static void main (String args[]){
        Scanner input = new Scanner (System.in);
        int one, two, uone, utwo;
        one = 1;
        two = 2;
        System.out.println("Welcome to the Lyrics Finder!");
        System.out.println("Press the number beside the song to see the lyrics!");
        System.out.println("1) Thank For The Memories - Fall Out Boy");
        System.out.println("2) Take Me To Church - Hozier");
        uone = input.nextInt();
        utwo = input.nextInt();
        switch (one){
        case 1:
            //code goes here for option 1
            System.out.println("");
            System.out.println("Thank For The Memories - Fall Out Boy Lyrics");
            System.out.println("         ==          ");
            System.out.println("I'm gonna make it bend and break");
            System.out.println("Say a prayer but let the good times roll");
            System.out.println("In case God doesn't show");
            System.out.println("(Let the good times roll, let the good times roll)");
            System.out.println("And I want these words to make things right");
            System.out.println("But it's the wrongs that make the words come to life");
            System.out.println("Who does he think he is?");
            System.out.println("Who does he think he is?");
            System.out.println("Better put your fingers back to the keys");
            //continue song here
            break;
        case 2:
            //retype all code her for it to reset
            System.out.println("");
            System.out.println("Take Me To Church - Hozier");
            System.out.println("          ==             ");
            System.out.println("My lover's got humour, She's the giggle at a funeral");
            System.out.println("Knows everybody's disapproval, I should've worshipped her sooner");
            System.out.println("If the heavens ever did speak, She's the last true mouthpiece");
            System.out.println("Every Sunday's getting more bleak, A fresh poison each week");
            System.out.println("We were born sick you heard them say it");
            System.out.println("My church offers no absolutes, She tells me Worship in the bedroom");
            break;
  }
        while (uone == one){
            System.out.println("");
            System.out.println("<-=LYRICS ABOVE=->");
            System.out.println("<-=OPTIONS=->");
            System.out.println("Press the number beside the song to see the lyrics!");
            System.out.println("1) Thank For The Memories - Fall Out Boy");
            System.out.println("2) Take Me To Church - Hozier");

            uone = input.nextInt();
            utwo = input.nextInt();
            switch (one){
            case 1:
                //code goes here for option 1
                System.out.println("");
                System.out.println("Thank For The Memories - Fall Out Boy Lyrics");
                System.out.println("         ==          ");
                System.out.println("I'm gonna make it bend and break");
                System.out.println("Say a prayer but let the good times roll");
                System.out.println("In case God doesn't show");
                System.out.println("(Let the good times roll, let the good times roll)");
                System.out.println("And I want these words to make things right");
                System.out.println("But it's the wrongs that make the words come to life");
                System.out.println("Who does he think he is?");
                System.out.println("Who does he think he is?");
                System.out.println("Better put your fingers back to the keys");
                //continue song here
                break;
            case 2:
                //retype all code her for it to reset
                System.out.println("");
                System.out.println("Take Me To Church - Hozier");
                System.out.println("          ==             ");
                System.out.println("My lover's got humour, She's the giggle at a funeral");
                System.out.println("Knows everybody's disapproval, I should've worshipped her sooner");
                System.out.println("If the heavens ever did speak, She's the last true mouthpiece");
                System.out.println("Every Sunday's getting more bleak, A fresh poison each week");
                System.out.println("We were born sick you heard them say it");
                System.out.println("My church offers no absolutes, She tells me Worship in the bedroom");
                break;
        }
 }
}
}

當用戶鍵入數字一時,應輸出:

System.out.println("");
                System.out.println("Thank For The Memories - Fall Out Boy Lyrics");
                System.out.println("         ==          ");
                System.out.println("I'm gonna make it bend and break");
                System.out.println("Say a prayer but let the good times roll");
                System.out.println("In case God doesn't show");
                System.out.println("(Let the good times roll, let the good times roll)");
                System.out.println("And I want these words to make things right");
                System.out.println("But it's the wrongs that make the words come to life");
                System.out.println("Who does he think he is?");
                System.out.println("Who does he think he is?");
                System.out.println("Better put your fingers back to the keys");
                //continue song here
                break;
while (uone == one){
                System.out.println("");
                System.out.println("<-=LYRICS ABOVE=->");
                System.out.println("<-=OPTIONS=->");
                System.out.println("Press the number beside the song to see the lyrics!");
                System.out.println("1) Thank For The Memories - Fall Out Boy");
                System.out.println("2) Take Me To Church - Hozier");

用戶鍵入數字2時,應輸出:

System.out.println("");
                System.out.println("Take Me To Church - Hozier");
                System.out.println("          ==             ");
                System.out.println("My lover's got humour, She's the giggle at a funeral");
                System.out.println("Knows everybody's disapproval, I should've worshipped her sooner");
                System.out.println("If the heavens ever did speak, She's the last true mouthpiece");
                System.out.println("Every Sunday's getting more bleak, A fresh poison each week");
                System.out.println("We were born sick you heard them say it");
                System.out.println("My church offers no absolutes, She tells me Worship in the bedroom");
                break;
while (uone == one){
                System.out.println("");
                System.out.println("<-=LYRICS ABOVE=->");
                System.out.println("<-=OPTIONS=->");
                System.out.println("Press the number beside the song to see the lyrics!");
                System.out.println("1) Thank For The Memories - Fall Out Boy");
                System.out.println("2) Take Me To Church - Hozier");

請注意,我對編碼非常陌生,因此請盡力解釋。 如果沒有,我仍然可以理解,但是無論如何,請簡要說明您的解決方案。

更改

    uone = input.nextInt();
    utwo = input.nextInt();
    switch (one){

    int choice = input.nextInt();

    switch (choice)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM