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 6 of 6

Thread: A For loop that calculates every possible outcome for 2 AND 3 chars...

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default A For loop that calculates every possible outcome for 2 AND 3 chars...

    Hi there,

    I'm having some trouble with an assignment.

    I'm having a slight problem creating a nested for loop that displays every possible combination for random input. It's supposed to be like a slot machine game. The person is to enter three characters, it checks the characters, stores them, and then is supposed to give every combo of the chars like so...

    Input = @ and #

    @@@
    @@#
    @##
    @#@
    ###
    ##@
    #@@
    #@#

    How could I create a nested for loop that does so.

    I can't even think of how to do it on paper. Apparently it requires 3 nested for loops, one for each character.

    Input can also be three chars such as @#$, Which would yield 27 possibilities instead of 8.



    Here is my code so far....

    I know how to do a nested for loop, and I know how they work... but I don't understand how you can create every possible solution, regardless of what is entered...

     import javax.swing.*;
    import java.util.*;
     
     
    public class oneArmBandit
    {
      public static void main (String [] args)
      {
         //variables
        String input = "full";
        String empty = "";
        boolean condition = true;
        Scanner keyboard;
        char signOne = '-';
        char signTwo = '-';
        char signThree = '-';
        int inputLength = 0;
        int numberOfElements = 0;
     
        //for(int i = 0; i < numberOfElements; i++)
     
       /* for (int i = 0; i < 5; i++) {
          for (int j = 0; j < 5; j++)  {
        System.out.println("i: " +i+ " j: " +j);
          }
        } */
     
     
     
     
        while (!input.equals(empty) && condition == true)
        {
          input = JOptionPane.showInputDialog("Enter the symbols on the wheels:");
          keyboard = new Scanner(input);  
     
          System.out.println(input + "\n");
          inputLength = (input.length());
     
     
          if (inputLength == 2)
          {
            signOne = (input).charAt(0);
          signTwo = (input).charAt(1);
     
          for(int i = 0; i < inputLength; i++)
     
            for(int j = 0; j < 2; j++) 
            {System.out.println(signOne);
          System.out.println(signTwo);};
     
            for (int k = 0; k < 2; k++)
            {
     
                condition = false;
            }
          }
     
     
     
     
     
     
     
          }
     
          }
      }
     
     
     
    /* 
          if (inputLength == 3)
          {
            signOne = (input).charAt(0);
           signTwo = (input).charAt(1);
          signThree = (input).charAt(2);
     
          for(int i = 0; i < inputLength; i++)
          { System.out.println(signOne);
     
            for(int j = 0; j < inputLength; j++)
            { System.out.println(signTwo);
     
             for(int k = 0; k < numberOfElements; k++)
             { System.out.println(signThree);
     
            }
          }
     
          condition = false;
          }
     
     
     
          */


    For my code, I'm just trying to get the for loop to run when the char's entered are only two... That's why the bottom portion, when char == 3, is commented out...

    I can't seem to think of a solution/what to put in the for loop to make it run all possible combinations.


    Any insight would be helpful.

    I am new to programming, so laymen's terms would be appreciated

    Cheers,

    Matt


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A For loop that calculates every possible outcome for 2 AND 3 chars...

    Input = @ and #
    I can't even think of how to do it on paper. Apparently it requires 3 nested for loops, one for each character.
    I see two characters. Now is it missing a character or do you want two loops? If you don't have a solution to the problem you should not be writing code yet. Work on steps to solve the problem first and see how far you can get. It truly is helpful in writing code.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A For loop that calculates every possible outcome for 2 AND 3 chars...

    Quote Originally Posted by jps View Post
    I see two characters. Now is it missing a character or do you want two loops? If you don't have a solution to the problem you should not be writing code yet. Work on steps to solve the problem first and see how far you can get. It truly is helpful in writing code.
    Since it is supposed to be like a slot machine, it is supposed to display 3 characters, even if 2 are inputted, such as;

    Input = @ and #

    @@@
    @@#
    @#@
    @##
    ###
    ##@
    #@@
    #@#

    ....

    and if input is # @ $

    it displays the same sort of deal but there are a lot more outcomes obviously...

    I understand the concept, using a nested FOR loop to create this output, but what is supposed to put into the...

    for (???; ???; ???)
    for (???; ???; ???)
    for(???;???;???)

    I am STUCK


  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A For loop that calculates every possible outcome for 2 AND 3 chars...

    Did you write down steps to take to solve the problem?

    What are they? What step are you stuck on?

    You want to print "one thing" three times. Where that "one thing" is a choice of an unknown number of items. So you need some way to get and save an unknown number of items. Some way to (randomly?) choose one element from the set of options. Having these steps in order will make code writing much easier.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Instead of using a for loop, try to populate a ch[3] with random symbols appearing from a list of symbols in each of the array position.

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: A For loop that calculates every possible outcome for 2 AND 3 chars...

    Very early in every beginning computer class they cover number systems, right?
    Positional number systems with base 10: Each digit can have a value of 0, 1, ... 9

    How many decimal numbers are there with three digits? (Answer 10 to the power 3, or 1000);

    How about base 2? Surely they covered that. Each binary digit can have a value of 0, 1
    How many binary numbers are there with three binary digits? (Fill in the blank here: ___________)

    They might not have covered base 3 specifically, but it is an obvious extension: Each ternary digit can have a value of 0, 1, 2
    How many numbers with base 3 are there with three ternary digits? (Fill in the blank here: ___________)

    Ho-hum, right? I wasn't in your class, but I can see it plainly: All of those eyes rolling upward. I can even read all of those thoughts: "So what? When do we get to the Good Stuff? I hope we get out of class soon. Won't that bell ever ring?"

    Well, here's an application (that is very simple to implement) using number systems like those that were covered:


    Think of your display as a number. We will display some symbols representing the digits of a number.

    If there are three positions, think of it as a three-digit number. If there are two possible symbols then it is a binary number.

    First question: How many different patterns are there? (Answer : 2 to the power 3). Surely they covered stuff like this.

    Next question: How can I use my vast knowledge of number systems to display all possible patterns?

    Well, you can have a counter that goes from 0 through 7. Just display the binary digits of each count. Or, if we wanted symbols, say '@' and '#' just let '@' represent binary 0 and let '#' represent binary 1


    I like to visualize the output before writing the program, and my program output might look like the following:
    Number of positions = 3
    Number of symbols   = 2 (@ #)
     
    Number of patterns  = 8
     
     N     Base 2   Display
    -----------------------
      0     0 0 0   (@ @ @)
      1     0 0 1   (@ @ #)
      2     0 1 0   (@ # @)
      3     0 1 1   (@ # #)
      4     1 0 0   (# @ @)
      5     1 0 1   (# @ #)
      6     1 1 0   (# # @)
      7     1 1 1   (# # #)


    After debugging, I would remove superfluous output and just show whatever the assignment requires.


    Now, what if you wanted three symbols, '@', '#', '$'

    So now think of the number that leads to the display is a number in base 3. (Each digit can have value 0, 1, 2)

    The number of different displays is equal to (3 to the power 3) = 27

    I would make a counter that goes from zero through 26 and show the base-3 digits for each value and the symbols for those digits:

    Then my program output might look like this:
    Number of positions = 3
    Number of symbols   = 3 (@ # $)
     
    Number of patterns  = 27
     
     N     Base 3   Display
    -----------------------
      0     0 0 0   (@ @ @)
      1     0 0 1   (@ @ #)
      2     0 0 2   (@ @ $)
      3     0 1 0   (@ # @)
      4     0 1 1   (@ # #)
      5     0 1 2   (@ # $)
      6     0 2 0   (@ $ @)
      7     0 2 1   (@ $ #)
      8     0 2 2   (@ $ $)
      9     1 0 0   (# @ @)
     10     1 0 1   (# @ #)
     11     1 0 2   (# @ $)
     12     1 1 0   (# # @)
     13     1 1 1   (# # #)
     14     1 1 2   (# # $)
     15     1 2 0   (# $ @)
     16     1 2 1   (# $ #)
     17     1 2 2   (# $ $)
     18     2 0 0   ($ @ @)
     19     2 0 1   ($ @ #)
     20     2 0 2   ($ @ $)
     21     2 1 0   ($ # @)
     22     2 1 1   ($ # #)
     23     2 1 2   ($ # $)
     24     2 2 0   ($ $ @)
     25     2 2 1   ($ $ #)
     26     2 2 2   ($ $ $)


    Summary:

    The thing can be done for any number of positions and any number of symbols. It will result in a finite number of deterministic operations.

    If this approach appeals to you, think of how the program will work:

    A single loop for the counter. If there are N positions and S symbols, the loop limit (number of patterns) is

    S to the power N



    For each value of the counter, derive digits in base S . For each count value you can store the Base-S digits in an array (size N) if you want to. You can use the digits to index into an array of characters (or Strings) that holds the symbols corresponding to the digits. Print out the symbols, whatever...

    Then, it's time to start writing code. Implementation is a snap.


    Cheers!

    Z
    Last edited by Zaphod_b; October 24th, 2012 at 11:22 AM.

Similar Threads

  1. [SOLVED] What in SWINGS name do i use to get this outcome?!
    By JonLane in forum AWT / Java Swing
    Replies: 2
    Last Post: February 25th, 2012, 04:55 PM
  2. Making a program that calculates how many days you lived.
    By shifat96 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2012, 12:34 AM
  3. Need help with chars/or's/if statements
    By czarcalvinsk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2012, 08:46 PM
  4. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM
  5. for loop with chars
    By dragon40226 in forum Loops & Control Statements
    Replies: 4
    Last Post: September 25th, 2011, 05:50 PM