java我如何使用while循环来存储用户输入,直到计数为5
import java.util.Scanner;
public class Project {
public static void main(String[] args ){
Scanner scan = new Scanner(System.in);
//My String Array List holding all of the emotions aslong with a for loop for I can get
//All the words to print to the screen
String emotion [] = new String [10];
emotion [0] = "Happy";
emotion [1] = "Sad";
emotion [2] = "Vibing";
emotion [3] = "Trapping";
emotion [4] = "Ambationz";
emotion [5] = "Anxious";
emotion [6] = "Positive";
emotion [7] = "Scared";
emotion [8] = "Worried";
emotion [9] = "Focused";
System.out.println(" Have You ever felt an emotion and didnt know the song to put on at the moment? \n Well Just type in your feeling and this super ADVANCED Java Program will determine your song matching with your emotion");
//Used a for loop for I can print out my string Array
for(int i = 0; i < emotion.length; i++){
System.out.println(emotion[i]);
}
System.out.println(" How are you feeling please use one of the words printed above");
String n = "";
// used a while loop for it can
while(songfinder(n).equals("Not Available Try Again")){
n = scan.nextLine();
songfinder(n);
System.out.println(songfinder(n));
}
System.out.println(" Now Copy and paste the link above asdinto a Web broswer and enjoy the musica");
}
//My Method for finding the user input and matching it with their song
public static String songfinder( String w )
{
if(w.equalsIgnoreCase("Happy"))
return "https://www.youtube.com/watch?v=ZbZSe6N_BXs";
if(w.equalsIgnoreCase("Sad"))
return " https://www.youtube.com/watch?v=pgN-vvVVxMA";
if(w.equalsIgnoreCase("Vibing"))
return "https://www.youtube.com/watch?v=e2qG5uwDCW4";
if(w.equalsIgnoreCase("Trapping"))
return "https://www.youtube.com/watch?v=XuEx6lNHZjM";
if(w.equalsIgnoreCase("Ambationz"))
return "https://www.youtube.com/watch?v=cQZqPi1aHNo";
if(w.equalsIgnoreCase("Positive"))
return "https://www.youtube.com/watch?v=h4UqMyldS7Q";
if(w.equalsIgnoreCase("Scared"))
return"https://www.youtube.com/watch?v=CtdsvvMjJ0I";
if(w.equalsIgnoreCase("Worried"))
return "https://www.youtube.com/watch?v=L3HQMbQAWRc";
if(w.equalsIgnoreCase("Focused"))
return"https://www.youtube.com/watch?v=hHW1oY26kxQ";
return "Not Available Try Again";
}
}
我想做的是一个while循环,不断询问您是否想再试一次,并基本上重新启动我的整个代码
# 1 楼答案
这看起来是学习枚举类型(Enum)的绝佳时机。使用
enum
我们可以将一首歌、一种情绪联系起来,并在不重复术语和不使用各种if
阶梯的情况下获得列表。比如然后你的
main
方法可以简化为# 2 楼答案