njw9017
njw9017

Reputation: 11

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

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

Upvotes: 1

Views: 73

Answers (2)

Aalexander
Aalexander

Reputation: 5004

Use a do-while loop to go inside the loop at least once.

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. The 'Y' is here just a dummy value you can replace it with anything you want this should just be an example.

Then you compare the typed in value in the condition of your while()

When the value you are asking for was typed in then go ahead and loop through it again.

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

Upvotes: 1

Melron
Melron

Reputation: 579

Surround your code with while(true)

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

You can break the loop by using 'break;'

Upvotes: 0

Related Questions