首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:为什么我的While语句不适用于我的案例2?

Java:为什么我的While语句不适用于我的案例2?
EN

Stack Overflow用户
提问于 2015-04-25 01:32:27
回答 1查看 62关注 0票数 0

我试图创建一个简单的程序,输出一首歌的歌词时,指定的数字是输入。问题是我的第二个案例不会输出,而是只输出第一个案例。当我试图输出第二种情况时,它仍然输出第一种情况。不管我是否做了两次while语句,这种情况仍然会发生。有人能帮我修好密码吗?

下面是:

代码语言:javascript
复制
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;
        }
 }
}
}

当用户输入第一个时,应该输出以下内容:

代码语言:javascript
复制
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时,应该输出:

代码语言:javascript
复制
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");

请注意,我是非常新的编码,所以请尝试解释最好的,你可以。如果没有,我仍然可以理解,但无论如何,请简要解释一下您的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-25 01:48:21

变化

代码语言:javascript
复制
    uone = input.nextInt();
    utwo = input.nextInt();
    switch (one){

代码语言:javascript
复制
    int choice = input.nextInt();

    switch (choice)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29860002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档