简体   繁体   English

怎么把go回一个代码的开头而不结束呢?

[英]How to go back to the beginning of a code without ending it?

I want to go back to the beginning of my code in order to print multiple things and allow people to add multiple “drawings” to a picture without ending the code我想 go 回到我的代码的开头,以便打印多个内容并允许人们在不结束代码的情况下向图片添加多个“绘图”

Here's my code:这是我的代码:

package com.company;

import java.util.Scanner;

public class Art {

    public static void main(String[] args) {
        System.out.println("What would you like your scene to contain?");
        Scanner userInput = new Scanner(System.in);
        String response = userInput.nextLine();
        System.out.println("How many " + response + " would you like?");
        int number = userInput.nextInt();

        if (response.equals("trees") || response.equals("Trees")) {
            Scanner scan = new Scanner(System.in);
            System.out.println("What kind of trees would you like?");
            String variation = scan.nextLine();

            if (variation.equals("Pine") || variation.equals("pine")) {
                for (int i = 1; i <= number; i++) {
                    System.out.println("    /\\");
                    System.out.println("   /  \\");
                    System.out.println("  /    \\");
                    System.out.println(" /______\\");
                    System.out.println("    []    ");
                    System.out.println(" ");
                }
            } else if (variation.equals("oak") || variation.equals("Oak")) {
                for (int i = 1; i <= number; i++) {
                    System.out.println(" \\  ||- \\/-");
                    System.out.println(" -\\/|| -/");
                    System.out.println("   \\||-/");
                    System.out.println("   -[]    ");
                    System.out.println("    []-   ");
                }
            } else if (variation.equals("christmas") || variation.equals("Christmas")) {
                for (int i = 1; i <= number; i++) {
                    System.out.println("       o       ");
                    System.out.println("     .'.'.     ");
                    System.out.println("    .*.'.*.    ");
                    System.out.println("   *.'.*.'.*   ");
                    System.out.println(" .'*.'.*.'.*'. ");
                    System.out.println("*.''.*.'.*.''.*");
                    System.out.println("      [ ]      ");
                }
            }
        }
        System.out.println("Would you like to add more?");
        Scanner scan1 = new Scanner(System.in);
        String response1 = scan1.nextLine();
    }
} 

Use a do-while loop to go inside the loop at least once.对循环内的 go 至少使用一次 do-while 循环。

You are asking at the end if he wants to add more, you should give the User a choice like type in 'Y' for more.你在最后问他是否想添加更多,你应该给用户一个选择,比如输入“Y”以获得更多。 The 'Y' is here just a dummy value you can replace it with anything you want this should just be an example. 'Y' 在这里只是一个虚拟值,你可以用你想要的任何东西替换它,这只是一个例子。

Then you compare the typed in value in the condition of your while()然后在while()的条件下比较输入的值

When the value you are asking for was typed in then go ahead and loop through it again.当您要求的值被输入时,然后 go 提前并再次循环。

import java.util.*;
public class MyClass {
   public static void main(String[] args){
       String response1;
   do{
    System.out.println("What would you like your scene to contain?");
        Scanner userInput = new Scanner(System.in);
        String response = userInput.nextLine();
        System.out.println("How many " + response + " would you like?");
        int number = userInput.nextInt();

        if (response.equals("trees") || response.equals("Trees")) {
            Scanner scan = new Scanner(System.in);
            System.out.println("What kind of trees would you like?");
            String variation = scan.nextLine();

            if (variation.equals("Pine") || variation.equals("pine")) {
                for (int i = 1; i <= number; i++) {
                    System.out.println("    /\\");
                    System.out.println("   /  \\");
                    System.out.println("  /    \\");
                    System.out.println(" /______\\");
                    System.out.println("    []    ");
                    System.out.println(" ");
                }
            } else if (variation.equals("oak") || variation.equals("Oak")) {
                for (int i = 1; i <= number; i++) {
                    System.out.println(" \\  ||- \\/-");
                    System.out.println(" -\\/|| -/");
                    System.out.println("   \\||-/");
                    System.out.println("   -[]    ");
                    System.out.println("    []-   ");
                }
            } else if (variation.equals("christmas") || variation.equals("Christmas")) {
                for (int i = 1; i <= number; i++) {
                    System.out.println("       o       ");
                    System.out.println("     .'.'.     ");
                    System.out.println("    .*.'.*.    ");
                    System.out.println("   *.'.*.'.*   ");
                    System.out.println(" .'*.'.*.'.*'. ");
                    System.out.println("*.''.*.'.*.''.*");
                    System.out.println("      [ ]      ");
                }
            }
        }
        System.out.println("Would you like to add more?");
        Scanner scan1 = new Scanner(System.in);
        response1 = scan1.nextLine();
   }while(response1.equals("Y"));
   }
}

Surround your code with while(true)用 while(true) 包围你的代码

while (true)
    {
        // add your code here
    }

You can break the loop by using 'break;'你可以使用'break;'来打破循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM