gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:43:55 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的程序,当输入指定的数字时输出歌曲的歌词。问题是我的第二种情况不会输出,而只输出第一种情况。当我尝试输出第二种情况时,它仍然输出第一种情况。不管我是否做了两个 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)

关于Java:为什么我的 while 语句不适用于我的案例 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com