CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2011
    Posts
    12

    Java Programming Code Help

    Okay guys I have been working on a code in creating a solt machine! I already made half the code but I want to encorporate arrays, if loops and for and while loops in there as well.

    Other than that I am pretty much set! The Selection and Repetition consturcts aren't the main problem though, the real problem for me is arrays, can someone possible help me?

    Code:

    // import the java.util.Random class:
    import java.io.*;
    import java.util.Random;
    public class SlotMachine
    {
    public static void main(String args[]) throws IOException{
    Random randomizer = new Random();

    String a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

    int ai=0, bi=0;


    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader= new BufferedReader(input);

    System.out.println("Please enter in the money you want to bet");
    b=reader.readLine();

    bi=Integer.parseInt(b);

    int ci = randomizer.nextInt(10);
    int di = randomizer.nextInt(10);
    int ei = randomizer.nextInt(10);
    int fi = randomizer.nextInt(10);
    int gi = randomizer.nextInt(10);
    int hi = randomizer.nextInt(10);
    int ii = randomizer.nextInt(10);
    int ji = randomizer.nextInt(10);
    int ki = randomizer.nextInt(10);
    int li = randomizer.nextInt(10);
    int mi = randomizer.nextInt(10);
    int ni = randomizer.nextInt(10);
    int oi = randomizer.nextInt(10);
    int pi = randomizer.nextInt(10);
    int qi = randomizer.nextInt(10);
    int ri = randomizer.nextInt(10);

    System.out.println(ci +"\t" + di+"\t" + ei+"\t" + fi);
    System.out.println(gi +"\t" + hi+"\t" + ii+"\t" + ji);
    System.out.println(ki +"\t" + li+"\t" + mi+"\t" + ni);
    System.out.println(oi +"\t" + pi+"\t" + qi+"\t" + ri);



    }
    }



    Keep it simple please! Thanks in advance!

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Java Programming Code Help

    My suggestions:

    1. Use code tags.
    2. Ask a specific question
    3. Don't expect this forum to teach you Java. If you don't know how to use arrays, read whatever reference you were using to learn about them. If, after that, you still don't know anything about arrays, then give up on programming, its not for you. If you did learn a bit but need clarification about what your book or tutorial said, then ask, citing specifically what part of the tutorial/book you didn't understand.

  3. #3
    Join Date
    Jan 2011
    Posts
    12

    Re: Java Programming Code Help

    Ya sry bout that and my only question is how do I change the variables so it is in arrays??

    That's it and I thought this forum was about helping and teaching people Java programming???

  4. #4
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: Java Programming Code Help

    Check out this link.

    http://www.willamette.edu/~gorr/clas...9/arrays2d.htm

    It's about using a 2D - array of int to simulate a checkers game. If that is not to your liking then google "java array example" and read read read. It's the only way to learn. And that is the point of your assignment.

    Concerning your code (which Martin O has already reminded you to use the tags so I won't):

    You have a bunch of "int" just kickin around but they aren't grouped in any fashion so systematically comparing them will be difficult (way more difficult than using arrays or a single multi-dimensional array).

    Martin O speaks the truth. Arrays are about as basic as programming gets. First you have single primitive elements (i.e. int, long, boolean, etc...), which you seem to have a grasp on, after that the next most basic form of these primitive elements is to order them in a data structure called an array. If the basic concept of an array (or 2D arrays even) is beyond comprehension then maybe programing isn't right for you (at least at this moment). Of course you will never know until you try to learn it without being explicitly told how to tackle your problem.

    So, what you are trying to do is create a multi-dimensional array of primitive type int (integers of course) and compare them in some fashion. So you need to learn how to create a 2D int array and how to "loop" through the elements and compare them for equality.

    You're getting there though so I hope you keep trying. =D

    Best of luck

  5. #5
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: Java Programming Code Help

    This forum, as I understand it, is about helping people with specific problems concerning java programming. There are already countless "learn java" tutorials on the web or in books. So if you are trying to learn java from the ground up you are better off reading a book about beginning java. There are tons of great books about learning java that will cover your specific problem in detail. I have three on my shelf right now: "Core Java, Starting Out with Java, and Programming in C (though not a java book it has the same syntax for declaring arrays)". This is for an assignment so you must have a text book. What is the title of your textbook?

  6. #6
    Join Date
    Jan 2011
    Posts
    12

    Re: Java Programming Code Help

    Thanks okay I got past all these things now I just need help on one thing and I know it is really basic but I cant make "or" work:

    Look at the bottom of the code where I try to write or!

    // import the java.util.Random class:
    import java.io.*;
    import java.util.Random;
    public class SlotMachine
    {
    public static void main(String args[]) throws IOException{
    Random randomizer = new Random();

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    String a,b,c,d,e,f,g,h,i,j,k,l,n,o,p,q,r,s,t,u,v,w,y,z;

    int ai=0, bi=0;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader= new BufferedReader(input);

    System.out.println("Please enter in the money you want to bet");
    b=reader.readLine();

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    bi=Integer.parseInt(b);

    int ci = randomizer.nextInt(10);
    int di = randomizer.nextInt(10);
    int ei = randomizer.nextInt(10);
    int fi = randomizer.nextInt(10);
    int gi = randomizer.nextInt(10);
    int hi = randomizer.nextInt(10);
    int ii = randomizer.nextInt(10);
    int ji = randomizer.nextInt(10);
    int ki = randomizer.nextInt(10);
    int li = randomizer.nextInt(10);
    int mi = randomizer.nextInt(10);
    int ni = randomizer.nextInt(10);
    int oi = randomizer.nextInt(10);
    int pi = randomizer.nextInt(10);
    int qi = randomizer.nextInt(10);
    int ri = randomizer.nextInt(10);

    System.out.println(ci +"\t" + di+"\t" + ei+"\t" + fi);
    System.out.println(gi +"\t" + hi+"\t" + ii+"\t" + ji);
    System.out.println(ki +"\t" + li+"\t" + mi+"\t" + ni);
    System.out.println(oi +"\t" + pi+"\t" + qi+"\t" + ri);

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Random money = new Random();

    int[] x = new int[10];
    for (int m=0; m<x.length; m++)
    {
    x[m] = (money.nextInt(100000));
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //|| = or
    //System.out.println(x[0]);

    if (ci=di=ei=fi | ci=gi=ki=oi | ci=hi=mi=ri | di=hi=li=pi | ei=ii=mi=qi | fi=ji=ni=ri | fi=ii=li=oi)
    {
    System.out.println(x[0]);
    }
    }

  7. #7
    Join Date
    Jan 2011
    Posts
    12

    Re: Java Programming Code Help

    anyone?

  8. #8
    Join Date
    Feb 2008
    Posts
    966

    Re: Java Programming Code Help

    You should really look up the java operators. What you are trying to do is a bitwise OR, not a "this or this or that". Plus you are using the single = sign instead of the double == sign as a check. What exactly are you trying to do with that IF statement?

  9. #9
    Join Date
    Jan 2011
    Posts
    12

    Re: Java Programming Code Help

    Code:
    // import the java.util.Random class:
    import java.io.*;
    import java.util.Random;
    public class SlotMachine  
    {
      public static void main(String args[]) throws IOException{
        Random randomizer = new Random();
     
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
        String a,b,c,d,e,f,g,h,i,j,k,l,n,o,p,q,r,s,t,u,v,w,y,z;
        
       int ai=0, bi=0; 
       
       ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
         InputStreamReader input = new InputStreamReader(System.in);
       BufferedReader reader= new BufferedReader(input);
    
       System.out.println("Please enter in the money you want to bet");
       b=reader.readLine();
       
       ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
       
       bi=Integer.parseInt(b);
       
       int ci = randomizer.nextInt(4);
       int di = randomizer.nextInt(4);
       int ei = randomizer.nextInt(4);
       int fi = randomizer.nextInt(4);
       int gi = randomizer.nextInt(4);
       int hi = randomizer.nextInt(4);
       int ii = randomizer.nextInt(4);
       int ji = randomizer.nextInt(4);
       int ki = randomizer.nextInt(4);
       int li = randomizer.nextInt(4);
       int mi = randomizer.nextInt(4);
       int ni = randomizer.nextInt(4);
       int oi = randomizer.nextInt(4);
       int pi = randomizer.nextInt(4);
       int qi = randomizer.nextInt(4);
       int ri = randomizer.nextInt(4);
       
       System.out.println(ci +"\t" + di+"\t" + ei+"\t" + fi);
       System.out.println(gi +"\t" + hi+"\t" + ii+"\t" + ji);
       System.out.println(ki +"\t" + li+"\t" + mi+"\t" + ni);
       System.out.println(oi +"\t" + pi+"\t" + qi+"\t" + ri);    
       
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
       Random money = new Random();
    
      int[] x = new int[10];
      for (int m=0; m<x.length; m++)
      {
       x[m] = (money.nextInt(100000));
      }
    
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
     
     
       if ( (ci==di)&&(ei==fi)){
        System.out.println("You won $" +x[0]);}
      
       else if ( (ci==gi)&&(ki==oi)){
        System.out.println("You won $" +x[0]);}
      
       else if ( (ci==hi)&&(mi==ni)){
        System.out.println("You won $" +x[0]);}
    
       else if ( (di==hi)&&(li==pi)){
        System.out.println("You won $" +x[0]);}
            
       else if ( (ei==ii)&&(mi==qi)){
        System.out.println("You won $" +x[0]);}
               
       else if ((fi==ji)&&(ni==ri)){
        System.out.println("You won $" +x[0]);}
    
       else if ((fi==ii)&&(li==oi)){
        System.out.println("You won $" +x[0]);}
       
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
       
       else if (((((ci==di)&&(ei==fi))&&(ci==gi))&&(ki==oi))){
        System.out.println("You won $" + x[0] + x[1]);}     
       
       else if (((((ci==di)&&(ei==fi))&&(ci==hi))&&(mi==ni))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((ci==di)&&(ei==fi))&&(di==hi))&&(li==pi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((ci==di)&&(ei==fi))&&(ei==ii))&&(mi==qi))){
        System.out.println("You won $" + x[0] + x[1]);}       
    
       else if (((((ci==di)&&(ei==fi))&&(fi==ji))&&(ni==ri))){
        System.out.println("You won $" + x[0] + x[1]);}           
    
       else if (((((ci==di)&&(ei==fi))&&(fi==ii))&&(li==oi))){
        System.out.println("You won $" + x[0] + x[1]);}      
    
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
       
       else if (((((ci==gi)&&(ki==oi))&&(ci==hi))&&(mi==ni))){
        System.out.println("You won $" + x[0] + x[1]);}     
       
       else if (((((ci==gi)&&(ki==oi))&&(di==hi))&&(li==pi))){
        System.out.println("You won $" + x[0] + x[1]);}       
    
       else if (((((ci==gi)&&(ki==oi))&&(ei==ii))&&(mi==qi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((ci==gi)&&(ki==oi))&&(fi==ji))&&(ni==ri))){
        System.out.println("You won $" + x[0] + x[1]);}         
    
       else if (((((ci==gi)&&(ki==oi))&&(fi==ii))&&(li==oi))){
        System.out.println("You won $" + x[0] + x[1]);}       
       
       
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
       
       else if (((((ci==hi)&&(mi==ni))&&(di==hi))&&(li==pi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((ci==hi)&&(mi==ni))&&(ei==ii))&&(mi==qi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((ci==hi)&&(mi==ni))&&(fi==ji))&&(ni==ri))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((ci==hi)&&(mi==ni))&&(fi==ii))&&(li==oi))){
        System.out.println("You won $" + x[0] + x[1]);}       
    
    
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    
       else if (((((di==hi)&&(li==pi))&&(ei==ii))&&(mi==qi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
       else if (((((di==hi)&&(li==pi))&&(fi==ji))&&(ni==ri))){
        System.out.println("You won $" + x[0] + x[1]);}        
    
       else if (((((di==hi)&&(li==pi))&&(fi==ii))&&(li==oi))){
        System.out.println("You won $" + x[0] + x[1]);}         
    
       
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    
       else if (((((ei==ii)&&(mi==qi))&&(fi==ji))&&(ni==ri))){
        System.out.println("You won $" + x[0] + x[1]);}          
    
       else if (((((ei==ii)&&(mi==qi))&&(fi==ii))&&(li==oi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    
       else if (((((fi==ji)&&(ni==ri))&&(fi==ii))&&(li==oi))){
        System.out.println("You won $" + x[0] + x[1]);}     
    
    //|| = or
      
      
      }
     }
    Alright so I bypassed that but now if a user outputs 2 numbers that are equal and also the following number that are equeal but not equal to the first set of numbers you still win! How do i fix it? Plz helP!

  10. #10
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: Java Programming Code Help

    What do you mean by "the user outputs"? Can you give an example of a set of data that meets the condition for your error and the result?

    think about what you are asking the program to do here.

    Code:
       if ( (ci==di)&&(ei==fi)){
        System.out.println("You won $" +x[0]);}
    If ci is equal to di AND ei is equal to fi then this statement is true.

    What you want is that ci is equal to di is equal to ei is equal to fi

    so you need to check all three values for equality with the leading value. So following your current logical evaluation methods you will need three checks:

    ci is equal to di AND ci is equal to ei AND ci is equal to fi.

  11. #11
    Join Date
    Jan 2011
    Posts
    12

    Re: Java Programming Code Help

    Oooo thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured