java - 如何根据用户输入使用 for 循环从数组打印字符串

标签 java

这是我当前代码的基本版本(请注意,我是新人,所以你即将看到的愚蠢行为会让你生气 XD)。

package prjWebinar2;
import java.util.Scanner;

public class clsPractice {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String answer;
        String date;
        String time;

        Scanner option = new Scanner(System.in);
        String[] video = {"abc", "bce", "cde"};

        System.out.println("Option 1:" + video[0]);
        System.out.println("Would you like to use option1?");
        answer = option.nextLine();

        if(option.equals("yes")) {
            if(option.equals("yes")) {
                System.out.println("Please enter a date you would like to show the video:");
                date = option.nextLine();
                System.out.println("Please enter a time you would like to show the video:");
                time = option.nextLine();
            }
            else {
                System.out.println("Option 2:" + video[1]);
                System.out.println("Would you like to use this video?");
                answer = option.nextLine();

                if(option.equals("yes"))   {
                    System.out.println("Please enter a date you would like to show the video:");
                    date = option.nextLine();
                    System.out.println("Please enter a time you would like to show the video:");
                    time = option.nextLine();
                }
                else {
                    System.out.println("final option, option 3:" + video[2]);
                    System.out.println("Would you like to use this video?");
                    answer = option.nextLine();

                    if(option.equals("yes")) {
                        System.out.println("Please enter a date you would like to show the video:");
                        date = option.nextLine();
                        System.out.println("Please enter a time you would like to show the video:");
                        time = option.nextLine();
                    }
                    else {
                        System.out.println("We do not have an other options for videos");
                    }
                }
            }
        }
    }
}

有没有一种方法可以实现 for 循环,而不是使用 ifelse。我需要一个循环来打印数组的第一个值,然后根据用户输入仅打印第二个和第三个值。可能听起来很模糊,但我基本上希望循环打印第一个选项,如果用户输入 no 则打印第二个选项等。 任何帮助表示赞赏! 谢谢,奥森。

最佳答案

你可以这样写:

import java.util.Scanner;

public class Test {
    static final Scanner sc = new Scanner(System.in);

    private void answerYes(String video) {
        System.out.println("Please enter a date you would like to show the video:");
        String date = sc.nextLine();
        System.out.println("Please enter a time you would like to show the video:");
        String time = sc.nextLine();

        // Do something with this
        System.out.println(String.format("You have chosen date: %s and time: %s", date, time));
    }

    private void showOptions(String[] videos, String noMoreVideosMsg) {
        int currentVideoIdx = 0;
        while (currentVideoIdx < videos.length) {
            System.out.println(String.format("Option %d: %s", currentVideoIdx + 1, videos[currentVideoIdx]));
            System.out.println("Would you like to use this video? (yes / no)");
            String answer = sc.nextLine();
            if (answer.equals("yes")) {
                // Choose this option
                answerYes(videos[currentVideoIdx]);
                // This will exit the function, meaning skip all further logic
                return;
            } else if (answer.equals("no")) {
                // Go to the next option
                currentVideoIdx++;
            } else {
                // Unknown command
                System.out.println(String.format("Command '%s' is not recognized", answer));
                System.out.println(); // Just a blank line for nicer output
            }
        }

        // If no option is selected, print the default message
        System.out.println(noMoreVideosMsg);
    }


    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);
        final String[] VIDEOS = new String[] { "A", "B", "C", "D" };
        final String NO_MORE_VIDEOS_MSG = "We do not have another options for videos";

        Test test = new Test();
        test.showOptions(VIDEOS, NO_MORE_VIDEOS_MSG);
    }
}

关于java - 如何根据用户输入使用 for 循环从数组打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40997081/

相关文章:

java - 如何使用java重命名ftp服务器中的文件

java - 斐波那契和 (Java)

java - 构建后生成的目标文件不包含必须存在的所有已编译类

java - Spring Hadoop:从自己的映射器/缩减器进行日志记录

java - jsf- "Cannot find component with expression"

java - 错误 - ReceiveBuilder 中的构建方法编译错误,无法解析类型 scala.Product

java - 需要重构 eclipse 中的字段名称,这也应该更改 getters/setters

java - 引用变量的封装?

java - 未使用的 MANIFEST.MF 类路径

java - 使用struts2 mvc架构如何将Canvas图像存储到服务器