e. john e. john - 1 month ago 4
Java Question

Creating a loop from Java input

I need to create a loop from user input. For example they have to enter how many times they want to shuffle the cards. And then it will run the loop of the cards being drawn as many times as the user input states. I will apply my entire code.

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class stringVariables {

private static boolean isValid;

public static void main (String[]args) throws NumberFormatException, IOException {

//user inputs their name in this section






Scanner user_input = new Scanner (System.in);

String first_name;
System.out.print("Enter Your First Name: ");
first_name = user_input.next ();

String last_name;
System.out.print("Enter Your Last Name: ");
last_name = user_input.next ();

String full_name;
full_name = first_name + " " + last_name;

System.out.println( full_name + " Is Now Playing");

//this is the shuffle portion as well as something to see if a number is not inputed
boolean testing = false;
String pos = "";
while(true)
{
testing = false;
Scanner sc = new Scanner(System.in);
System.out.println("How many times do you want the numbers shuffled: ");
pos = sc.next();
for(int i=0; i<pos.length();i++)
{
if(!Character.isDigit(pos.charAt(i)))
testing = true;
}
if(testing == true)
{
System.out.print("Enter only numbers.. ");
continue;
}

else
{
int key = Integer.parseInt(pos);


break;








// here is going to be the loop for shuffles









// we are now going to generate their random number and add a delay after completing their name fields

delay(2000);
System.out.println(" You will be given a hand of 3 random numbers between 7-13");

delay(2000);
System.out.println(" Then, the computer will add the random numbers and if it is equal to 31, you win.");









/* end of explanation of the game, next i will create a new screen
with the user's name and numbers */


delay(4000);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");






System.out.println("User playing: " + full_name);

System.out.println("Your lucky numbers are...");



// random number generator
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random rn = new Random();

int ch=1;
while(ch==1){


// get two random numbers between 7 and 13
Random r = new Random();
int num1 =7 + (int)(Math.random()*(7));
int num2 = 7 + (int)(Math.random()*(7));
int num3 = 7 + (int)(Math.random()*(7));



System.out.println(num1 + " + " + num2 + " + " + num3+ " = " + (num1 + num2 + num3 ));

int i = 0 ;
{
System.out.println( num1 + num2 + num3 );
i++ ;
}


if(num1 + num2 + num3 == 31){
System.out.println("Congratulations !! You are the Lucky Winner !!!!");
}

else
System.out.println("Better Luck Next Time");











//the play again menu. this blocks any input besides 1 or 0


Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Want To Play Again ? ANY # = YES, ANY LETTER = NO");
String input = sc.next();
int intInputValue = 0;
try {

intInputValue = Integer.parseInt(input);
break;
} catch (NumberFormatException ne) {
System.out.println("Input is not a number, type 1 to continue, or any letter to quit");

ch=Integer.parseInt(br.readLine());
}

}}

}


//delay field

public static void delay(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException exp) {

//delay field

}
}
}


what I need to do is loop the user input from

boolean testing = false;
String pos = "";
while(true)
{
testing = false;
Scanner sc = new Scanner(System.in);
System.out.println("How many times do you want the numbers shuffled: ");
pos = sc.next();
for(int i=0; i<pos.length();i++)
{
if(!Character.isDigit(pos.charAt(i)))
testing = true;
}
if(testing == true)
{
System.out.print("Enter only numbers.. ");
continue;
}

else
{
int key = Integer.parseInt(pos);


break;


And make it replay this loop

// random number generator
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random rn = new Random();

int ch=1;
while(ch==1){


// get two random numbers between 7 and 13
Random r = new Random();
int num1 =7 + (int)(Math.random()*(7));
int num2 = 7 + (int)(Math.random()*(7));
int num3 = 7 + (int)(Math.random()*(7));



System.out.println(num1 + " + " + num2 + " + " + num3+ " = " + (num1 + num2 + num3 ));

int i = 0 ;
{
System.out.println( num1 + num2 + num3 );
i++ ;
}


To make it loop to the desired user input after the completion of their name

Answer

I have done some altering in your code and I feel this is what you are looking for. Just copy paste the code and it should work ideally.

import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class StringVariables  {

    public static void main(String[] args) throws NumberFormatException, IOException {

        //user inputs their name in this section

        Scanner sc = new Scanner(System.in);

        String first_name;
        System.out.print("Enter Your First Name: ");
        first_name = sc.next();

        String last_name;
        System.out.print("Enter Your Last Name: ");
        last_name = sc.next();

        String full_name;
        full_name = first_name + " " + last_name;

        System.out.println(full_name + " Is Now Playing");

//this is the shuffle portion as well as something to see if a number is not inputed


        int ch = 1;
        while (ch == 1) {
            int pos = 0;
            System.out.println("How many times do you want the numbers shuffled: ");
            while (true) {
                sc = new Scanner(System.in);
                try {
                    pos = sc.nextInt();
                    break;
                } catch (InputMismatchException e) {
                    System.out.println("Use only numbers.");
                }
            }
            // we are now going to generate their random number and add a delay after completing their name fields

            delay(2000);
            System.out.println(" You will be given a hand  of 3 random numbers between 7-13");

            delay(2000);
            System.out.println(" Then, the computer will add the random numbers and  if it is equal to 31, you win.");


    /* end of explanation of the game, next i will create a new screen
       with the user's name and numbers */


            delay(4000);
            for (int i = 0; i < 100; i++) {
                System.out.println(" ");
            }

            System.out.println("User playing: " + full_name);

            System.out.println("Your lucky numbers are...");


// random number generator
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            int num1 = 0, num2 = 0, num3 = 0;
            boolean isWinner = false;
            for (int j = 0; j < pos; j++) {
                num1 = 7 + (int) (Math.random() * (7));
                num2 = 7 + (int) (Math.random() * (7));
                num3 = 7 + (int) (Math.random() * (7));
                System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3));

                if (num1 + num2 + num3 == 31) {
                    System.out.println("Congratulations !! You are the Lucky Winner !!!!");
                    isWinner = true;
                }
            }

            if(!isWinner){
                System.out.println("Better Luck Next Time");
            }




            //the play again menu. this blocks any input besides 1 or 0


            while (true) {
                System.out.println("Want To Play Again ? ANY # = YES, ANY LETTER = NO");
                String input = sc.next();
                int intInputValue = 0;
                try {

                    intInputValue = Integer.parseInt(input);
                    break;
                } catch (NumberFormatException ne) {
                    System.out.println("Input is not a number, type 1 to continue, or any letter to quit");

                    ch = Integer.parseInt(br.readLine());
                }

            }
        }

    }


//delay field

    public static void delay(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException exp) {

            //delay field

        }
    }

}

Comments