Ask Experts Questions for FREE Help !
Ask
    tamcam55's Avatar
    tamcam55 Posts: 1, Reputation: 1
    New Member
     
    #1

    Sep 29, 2016, 08:17 PM
    Need to create a loop from user input java
    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. It is a bit messy but I need help desperately as I cannot find an answer anywhere
    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(");









    /* 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(");
    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: ");






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

    System.out.println(" + ");



    // 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+ "Crongratulatons! You are the Lucky Winner!! " + (num1 + num2 + num3 ));

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


    if(num1 + num2 + num3 == 31){
    System.out.println("Better Luck Next Time");
    }

    else
    System.out.println("Want To Play Again ? 1=YES, 0=NO");











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


    Scanner sc = new Scanner(System.in);
    while (true) {
    System.out.println("Input is not a number, continue");
    String input = sc.next();
    int intInputValue = 0;
    try {

    intInputValue = Integer.parseInt(input);
    ch=Integer.parseInt(br.readLine());
    break;
    } catch (NumberFormatException ne) {
    System.out.println("");

    }
    }}


    }
    //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 = "How many times do you want the numbers shuffled: ";
    while(true)
    {
    testing = false;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter only numbers.. ");
    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(" + ");
    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 + " = " + (num1 + num2 + num3 ));

    int I = 0 ;
    {
    System.out.println( num1 + num2 + num3 );
    I++ ;
    } to the desired amount of user input
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #2

    Sep 30, 2016, 07:54 AM
    Quote Originally Posted by tamcam55 View Post
    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. It is a bit messy but I need help desperately as I cannot find an answer anywhere
    (Snipping irrelevant code)

    //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

    }}
    A great website had the following:
    The syntax of a for loop is −

    for(initialization; Boolean_expression; update) {
    // Statements
    }

    So you need a loop varible, a test, and update.

    The typical iterative loop design is:
    for(int I=0;i<end;i++){ //STUFF}

    That should be enough for you to go on.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Issues with starting a loop to create a contact [ 0 Answers ]

Hi, I am trying to create an address book with user input through the console. I have setup a class called Contact and I am calling an array of 100 slots. I am to the point where I have user input to get a list of options such as add, delete, list, and save contacts. But I can't seem to get any...

I would like to know How to Print a square with asterisks in java with while loop onl [ 1 Answers ]

Scanner input = new Scanner(System.in) ; int squareside ; System.out.print("Enter the side: "); squareside = input.nextInt(); while ( squareside != -1 ) { int xaxis = 0 ; while ( xaxis < squareside )

What exactly are the steps to input a new user code into this alarm? [ 5 Answers ]

I am tired of this alarm --- every time I try to input a new user code I can't get the alarm panel to recognize what I've just done. What exactly are the step by step steps to add a new user to the alarm panel? I am the Master code on the alarm... so tell me exactly... push * 5 my code _ ...

What is the technical name of a loop where user enters an impossible value for the da [ 0 Answers ]

What is the technical name of a loop where user enters an impossible value for the data being read to exit the loop?

How to retrive all related data from different sheets according to the user's input? [ 10 Answers ]

Hi there, I want to connect two different excel sheet in the manner as follows: Consider I'm using sheet1 with fields like prod_id & prod_name with their data (1,2,3,4,5) and (a,b,c,d,e) respectively. Now if I enter the prod_id as 3 in sheet2, then I need all the other fields related to...


View more questions Search