在获得用户输入之前,如何暂停循环?

时间:2014-07-27 20:11:25

标签: java loops

我是Java的初学者,我正在为自己创建一个有趣的项目来了解更多关于java的信息,我计划从预设列表中随机化视频并将其显示给用户。

我无法停止循环。输入要观看的视频类型后,程序会自动重新循环,但我想让它在重新播放之前询问您是否要观看另一个视频。以下是我到目前为止的情况:

import java.util.Scanner;
import java.util.Random;

public class YoutubeGenerator {

    public static void main(String[] args) {

        int randomstring = 0;

        for ( ; ; ) {

            System.out.println("\n ---------Youtube Video Generator 0.001 BETA------------------ \n");

            System.out.println("\n ********* DISCLAIMER: WARNING - This program may direct you to violent, disturbing content, and/or vulgar language and is intended for a MATURE person only. ********* \n \n");

        Scanner scan = new Scanner (System.in); 

        System.out.println("What kind of video from the list would you like to watch? \n");

        System.out.println("Cute \n" + "Funny \n" + "WTF \n" + "Interesting \n" + "Documentary \n");

        System.out.print("I want to watch: ");

        String userString = scan.next();

    Random rand = new Random();



if(userString.equalsIgnoreCase("cute")){

    String cute1 = "https://www.youtube.com/watch?v=EdCVijVT7Wk";
    String cute2 = "http://youtu.be/-XCvPptsfhI?t=7s";
    String cute3 = "https://www.youtube.com/watch?v=-nkEPsSsH68";
    String cute4= "https://www.youtube.com/watch?v=FZ-bJFVJ2P0";
    String cute5 = "https://www.youtube.com/watch?v=argCvDpk_KQ";

    System.out.println("Here's a cute video you can watch: " +cute5) ; 
}
if(userString.equalsIgnoreCase("funny")){  

    System.out.println("Here's a funny you can watch:"); 

    String funny1 = "https://www.youtube.com/watch?v=I59MgGlh2Mg";
    String funny2 = "http://www.youtube.com/watch?v=HKMNKS-9ugY";
    String funny3 = "https://www.youtube.com/watch?v=_qKmWfED8mA";
    String funny4= "https://www.youtube.com/watch?v=QDFQYKPsVOQ";
    String funny5 = "https://www.youtube.com/watch?v=ebv51QNm2Bk";

}

if(userString.equalsIgnoreCase("wtf")){  

    System.out.println("Here's a WTF video you can watch:");  

    String wtf1 = "https://www.youtube.com/watch?v=UfKIoSv2YEg";
    String wtf2 = "https://www.youtube.com/watch?v=hcGvN0iBA5s";
    String wtf3 = "http://www.youtube.com/watch?v=vxnyqvejPjI&feature=youtu.be&t=1m37s";
    String wtf4= "https://www.youtube.com/watch?v=10NJnT6-sSE";
    String wtf5 = "https://www.youtube.com/watch?v=DQeyjgSUlrk";

    }

if(userString.equalsIgnoreCase("interesting")){  

    System.out.println("Here's an interesting video you can watch:");

    String int1 = "https://www.youtube.com/watch?v=fYwRMEomJMM";
    String int2 = "https://www.youtube.com/watch?v=1PmYItnlY5M&feature=youtu.be&t=32s";
    String int3 = "https://www.youtube.com/watch?v=HgmnIJF07kg";
    String int4= "https://www.youtube.com/watch?v=cUcoiJgEyag";
    String int5 = "https://www.youtube.com/watch?v=BePoF4PrwHs";
}
if(userString.equalsIgnoreCase("documentary")){  

    System.out.println("Here's a space video you can watch: ");  

    String doc1 = "https://www.youtube.com/watch?v=wS_WlzdOc_A";
    String doc22 = "https://www.youtube.com/watch?v=8n0SkIGARuo";
    String doc33 = "https://www.youtube.com/watch?v=6LaSD8oFBZE";
    String doc4= "https://www.youtube.com/watch?v=zvfLdg2DN18";
    String doc5 = "https://www.youtube.com/watch?v=8af0QPhJ22s&hd=1";
    }  
    }
}
}

1 个答案:

答案 0 :(得分:1)

在循环的右大括号之前插入以下代码:

System.out.println("Do you want to watch another video? Enter yes or no");
String decision = scan.next();
if (decision.equalsIgnoreCase("no"))
    break;