如何使用基于用户输入的for循环从数组中打印字符串

时间:2016-12-06 13:57:07

标签: 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。我需要一个循环来打印数组的第一个值,然后根据用户输入只打印第二个和第三个。可能听起来模糊但我基本上想要循环打印第一个选项,如果用户输入否则则打印第二个选项等。 任何帮助表示赞赏! 谢谢,奥森。

3 个答案:

答案 0 :(得分:1)

你可以这样写:

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);
    }
}

答案 1 :(得分:0)

使用for循环遍历数组。

for(int i = 0; i < video.length; i++) {
     ....code
}

您可以使用 i 打印数字和相应的视频。

答案 2 :(得分:0)

选项1 :循环完整任务,每次询问用户是否要选择视频:

public class clsPractice {

    public static void main(String[] args) {
        String answer;
        String date;
        String time;

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

        for(int id=0; id<video.length; id++) {
            System.out.println("Option id:" + video[id]);
            System.out.println("Would you like to use option1?");
            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();
                break; // stop looping
            }
        }
    }
}

循环将停在break指令处。如果用户未输入yes,则循环块不执行任何操作并提示下一个视频元素。

选项2 :显示完整列表,然后要求用户输入选项编号。

public class clsPractice {

    public static void main(String[] args) {
        int answer = -1;
        String date;
        String time;

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

        for(int id=0; id<video.length; id++) {
            System.out.println("Option id:" + video[id]);
        }

        System.out.println("Which option would you like to use?");
        answer = option.nextInt();

        if(answer>0 && answer<video.length) {
            System.out.println("Video selected:"+answer);
            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();
        }
    }
}

这使得使用起来更快,因为在找到第N个项目之前你不必拒绝N-1个项目。

在这两种情况下,都不会处理不正确的用户输入。我让你做这个部分。