Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Need to create a loop from user input java (https://www.askmehelpdesk.com/showthread.php?t=827904)

  • Sep 29, 2016, 08:17 PM
    tamcam55
    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
  • Sep 30, 2016, 07:54 AM
    CravenMorhead
    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:
    Quote:

    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.

  • All times are GMT -7. The time now is 03:35 AM.