i want my statement to re-execute if the condition if met false

CapnCoin

What I would like to happen here is to have the user input "start". if the user inputs start the program must give a random word from the array and then ask for the user to input "next", when the user inputs "next" the program gives another random word, then the program asks for "next" to be input again and so forth... you get the idea.

here is some code, I thought would produce this effect but all it does is prints "Type start to see a cool word" user input "start" and then the program returns nothing.

Any advice would be appreciated and if you could tell me why my code is doing this I would be really appreciative because that way I can learn from this. Thanks

here is the code i wrote:

   import java.util.Scanner;
import java.util.Random;
public class Words {
    public static void main(String[]args){


        Scanner scan = new Scanner(System.in);
        String words[] = {"Iterate:","Petrichor:"};
        String input = "";

        System.out.println("type *start* to see a cool word");
        input = scan.nextLine();

        while(!input.equals("start")){
        String random = words[new Random().nextInt(words.length)];
        System.out.println(random);
        System.out.println();
        System.out.println("type *next* to see another cool word");
        while(input.equals("next"));
        }
    }
}
daniel.brubeck

You would like to wrap your input reading in a loop:

import java.util.Scanner;

import java.util.Random;
public class Words {
  public static void main(String[]args){
    Scanner scan = new Scanner(System.in);
    String words[] = {"Iterate","Petrichor"};
    String input = "";

    while ( !input.equals("start") ) {
       System.out.println("type *start* to begin");
       input = scan.nextLine();
    }

    String random = (words[new Random().nextInt(words.length)]);
  }
}

Note that in your particular example the loop conditional works for your if statement so there was no need for the if statement.

Update

If you need to keep this running while the user types next you can wrap everything inside a do .. while loop so it executes at least once:

import java.util.Scanner;

import java.util.Random;
public class Words {
   public static void main(String[]args){
      Scanner scan = new Scanner(System.in);
      String words[] = {"Iterate","Petrichor"};
      String input = "";
      do {
         do {
            System.out.println("type *start* to begin");
            input = scan.nextLine();
         } while ( !input.equals("start") );

         String random = (words[new Random().nextInt(words.length)]);
         System.out.println("type *next* to repeat");
         input = scan.nextLine();
      }
   } while ( input.equals("next") );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I want to know why my 'login logic' always false condition?

From Dev

I only want to add JSON info if condition is met with AngularJS

From Dev

I want to include an If statement in my SQL script

From Dev

I want my if loop to do nothing if certain conditions are met

From Dev

My sql statement has two parts attached with 'union all'. I want the second part to exceecute only when some condition is true

From Dev

If statement not stopping after condition is met

From Dev

IF statement that changes based on met condition

From Dev

I want to assign priceFilter a true/false based on condition in a function

From Dev

What condition was false in the if statement

From Dev

Execute statement in mysql if condition

From Dev

Why is my condition never met?

From Dev

Having my else statement executed even if condition isn't met in jQuery

From Dev

I want to add a column in my Data Frame according to some condition

From Dev

Is there any statement/function in java which can execute the previous body if the condition becomes false?

From Dev

Given an IF-ELSE statement inside a for loop, can I only skip the IF when the condition is met once? python

From Dev

Skip if-statement once condition is met

From Dev

Will an If Statement Stop Checking if the first OR condition is met in PHP?

From Dev

Trigger bootstrap modal if a condition statement is not met

From Dev

If Statement condition not being recognised despite being met

From Dev

Will an If Statement Stop Checking if the first OR condition is met in PHP?

From Dev

condition statement executes when conditions are not met

From Dev

Code is executed in IF statement when condition is not met

From Dev

Java, repeat If statement if the condition was false

From Dev

Java, repeat If statement if the condition was false

From Dev

Datediff can cause i can't execute my statement in SQL?

From Dev

How can I properly execute this javascript within my php statement?

From Dev

How do I check my condition on an if statement to see if it is working correctly?

From Dev

How do I create a condition with my script using the if statement?

From Dev

In main I spawn a new NSThread, and at a later point in main when a condition is met I want to stop the thread. How?

Related Related

  1. 1

    I want to know why my 'login logic' always false condition?

  2. 2

    I only want to add JSON info if condition is met with AngularJS

  3. 3

    I want to include an If statement in my SQL script

  4. 4

    I want my if loop to do nothing if certain conditions are met

  5. 5

    My sql statement has two parts attached with 'union all'. I want the second part to exceecute only when some condition is true

  6. 6

    If statement not stopping after condition is met

  7. 7

    IF statement that changes based on met condition

  8. 8

    I want to assign priceFilter a true/false based on condition in a function

  9. 9

    What condition was false in the if statement

  10. 10

    Execute statement in mysql if condition

  11. 11

    Why is my condition never met?

  12. 12

    Having my else statement executed even if condition isn't met in jQuery

  13. 13

    I want to add a column in my Data Frame according to some condition

  14. 14

    Is there any statement/function in java which can execute the previous body if the condition becomes false?

  15. 15

    Given an IF-ELSE statement inside a for loop, can I only skip the IF when the condition is met once? python

  16. 16

    Skip if-statement once condition is met

  17. 17

    Will an If Statement Stop Checking if the first OR condition is met in PHP?

  18. 18

    Trigger bootstrap modal if a condition statement is not met

  19. 19

    If Statement condition not being recognised despite being met

  20. 20

    Will an If Statement Stop Checking if the first OR condition is met in PHP?

  21. 21

    condition statement executes when conditions are not met

  22. 22

    Code is executed in IF statement when condition is not met

  23. 23

    Java, repeat If statement if the condition was false

  24. 24

    Java, repeat If statement if the condition was false

  25. 25

    Datediff can cause i can't execute my statement in SQL?

  26. 26

    How can I properly execute this javascript within my php statement?

  27. 27

    How do I check my condition on an if statement to see if it is working correctly?

  28. 28

    How do I create a condition with my script using the if statement?

  29. 29

    In main I spawn a new NSThread, and at a later point in main when a condition is met I want to stop the thread. How?

HotTag

Archive