Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: input controlled loop

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default input controlled loop

    i'm currently trying to write a loop for a project where i am making a basic game of black jack.

    in this game i want to make it loop round if the player chooses to twist. i currently have.

    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    /** This program is made to simulate a game of black jack*/
    public class first {
    public static void main (String[] args ) {
    /** this bit is creating the computers score. */
    Random r = new Random();
    int compscore= 0 ;
    int card;

    for (int i = 0 ; i<15; i = i + card) {

    card = r.nextInt (12)+1;
    compscore = (compscore + card);
    System.out.println("compscore = " + compscore);
    }

    String st = JOptionPane.showInputDialog("1 to twist 2 to stick");
    int st2 = Integer.parseInt(st);
    int card2;
    card2 = r.nextInt (12)+1;
    int playerscore=card2;
    int card3;
    card3 = r.nextInt (12)+1;


    System.out.println("player score " + playerscore);
    // if a player twists this code deals with this action.
    if (st2 == 1)
    {
    System.out.println("twist");
    while (st2==1)
    playerscore= (playerscore + card3);
    st = JOptionPane.showInputDialog("1 to twist 2 to stick");

    }
    else if (st2 == 2)
    {
    System.out.println("stick");
    if (playerscore>compscore)
    {
    System.out.println("player wins");
    }
    else
    {
    System.out.println("player loses");
    }
    }
    //* this is to ensure that only the two valid inputs can be used in the program*/
    else
    {
    System.out.println("invalid command");
    }


    }
    }

    origianlly i thought that if i used the value of st2 , i could control the loop by having the player type in again if they wanted to stick, but it just ends up on a inifinite loop of doing nothing. any help would be appreciated this is frustrating and i can't find anyhting on it anywhere.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: input controlled loop

    Please edit your post and wrap the code in code tags to make it easier to read.
    BB Code List - Java Programming Forums - The Java Community

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: input controlled loop

    Stuffing all your code into the main method is a bad idea. It will just lead to lots of ugly, hard to maintain code and probably sobbing. Think about how your program can be broken down into smaller chunks and move those chunks into other methods.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: input controlled loop

    sorry this is my first time programming and i'm not familiar with a lot of the ways you can do things, so this is as far as i understand at the moment,.. what i'm looking for is a method of looping that with only loop if i give a command, in this case , twist, and go on to a different command if i type twist.

    i realise that its probly very hard to read through my program i will attempt to make them more readable and organised in future.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: input controlled loop

    method of looping that with only loop if
    That sounds like you want to use a while loop.

    go on to a different command if
    That sounds like you want to use an if statement.

    Please edit your post and wrap the code in code tags to make it easier to read.
    BB Code List - Java Programming Forums - The Java Community

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  2. Help with counter controlled while loop please!
    By rockout341 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 15th, 2011, 02:41 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM