有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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循环,不断询问您是否想再试一次,并基本上重新启动我的整个代码


共 (2) 个答案

  1. # 1 楼答案

    这看起来是学习枚举类型(Enum)的绝佳时机。使用enum我们可以将一首歌、一种情绪联系起来,并在不重复术语和不使用各种if阶梯的情况下获得列表。比如

    public enum Emotions {
        Happy("https://www.youtube.com/watch?v=ZbZSe6N_BXs"), //
        Sad("https://www.youtube.com/watch?v=pgN-vvVVxMA"), //
        Vibing("https://www.youtube.com/watch?v=e2qG5uwDCW4"), //
        Trapping("https://www.youtube.com/watch?v=XuEx6lNHZjM"), //
        Ambationz("https://www.youtube.com/watch?v=cQZqPi1aHNo"), //
        Anxious("Not Available Try Again"), //
        Positive("https://www.youtube.com/watch?v=h4UqMyldS7Q"), //
        Scared("https://www.youtube.com/watch?v=CtdsvvMjJ0I"), //
        Worried("https://www.youtube.com/watch?v=L3HQMbQAWRc"), //
        Focused("https://www.youtube.com/watch?v=hHW1oY26kxQ");
        Emotions(String songUrl) {
            this.songUrl = songUrl;
        }
    
        public static Emotions fromName(String name) {
            for (Emotions e : values()) {
                if (e.name().equals(name)) {
                    return e;
                }
            }
            return null;
        }
    
        public String getUrl() {
            return songUrl;
        }
    
        private String songUrl;
    }
    

    然后你的main方法可以简化为

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println(" Have You ever felt an emotion and didnt know the song to put on at the moment?");
        System.out.println(" Well Just type in your feeling and this super ADVANCED Java Program "
                + "will determine your song matching with your emotion");
        for (int i = 0; i < Emotions.values().length; i++) {
            System.out.println(Emotions.values()[i].name());
        }
        System.out.println("How are you feeling please use one of the words printed above");
        while (true) {
            String n = scan.nextLine();
            Emotions e = Emotions.fromName(n);
            if (e == null) {
                System.out.println("Not Available Try Again");
            } else {
                System.out.println(e.getUrl());
                System.out.println("Now Copy and paste the link above asdinto a Web broswer and enjoy the musica");
            }
            System.out.println("Do you want to try again (y/n)?");
            String again = scan.nextLine();
            if (again.equalsIgnoreCase("n")) {
                break;
            }
        }
    }
    
  2. # 2 楼答案

    Scanner scan = new Scanner(System.in);
    do {
        ...
        System.out.println("Do you want to try again (y/n)?");
    } while (scan.nextLine().equals("y"));