gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:09:42 26 4
gpt4 key购买 nike

这是我当前代码的基本版本(请注意,我是新人,所以你即将看到的愚蠢行为会让你生气 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/

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