Results 1 to 5 of 5

Thread: Java Project

  1. #1
    dessen is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Java Project

    Hello, I am a a beginner java student and have been told to make a small version of the deal or no deal game.

    I have got everything working properly but I cannot happen to find a way to assign an integer or string "specific" random number.. such as -

    1 - 500
    2- 1000
    I need a code so that it will assign the 500 and 1000 randomly to 1 or 2..


    I am also having trouble figuring out how to restrict a number when entered in the input dialog once.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Not really a question about JCreator, but never mind.

    You can have an array containing the values you want to assign and use Random.nextInt(max) to index into it (after checking for duplicates).

    For input validation, just check to see if it's what you want and ask for it again if it isn't. As you're offering a restricted choice, you could use any one of toggle butons, radio buttons, push buttons, combo box, list, etc. you want.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    dessen is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    it doesnt seem to be working..
    this is my code so far

    import javax.swing.*;

    class DealText
    {
    public static void main (String [] args){
    Random.nextInt(max);
    int[] aa = new int [11];

    aa[1] = 100;
    aa[2] = 500;
    aa[3] = 1000;
    aa[4] = 5000;
    aa[5] = 10000;
    aa[6] = 25000;
    aa[7] = 50000;
    aa[8] = 100000;
    aa[9] = 500000;
    aa[10] = 1000000;


    JOptionPane.showMessageDialog(null, "Welcome to the game of DEAL or NO DEAL, the briefcases are numbered from 1-10");

    String input1 = JOptionPane.showInputDialog("What case would you like to pick as your own?");
    int own = Integer.parseInt(input1);

    JOptionPane.showMessageDialog(null, "You have chosen to pick case: "+own);


    JOptionPane.showMessageDialog(null, "Now pick three cases to eliminate");


    String input3 = JOptionPane.showInputDialog("Open the first case");
    int one = Integer.parseInt(input3);
    System.out.println("You have opened the case that contains $"+aa[one]);

    String input4 = JOptionPane.showInputDialog("Open the second case");
    int two = Integer.parseInt(input4);
    System.out.println("You have opened the case that contains $"+aa[two]);

    String input5 = JOptionPane.showInputDialog("Open the third case");
    int three = Integer.parseInt(input5);
    System.out.println("You have opened the case that contains $"+aa[three]);
    int sum = 0;
    for (int p = 1; p < 10; p++){
    sum = sum + aa[p];
    }



    int avg = sum/(10 - own);
    {


    Object[] options = { "DEAL", "NO DEAL"};
    int offers = JOptionPane.showOptionDialog(null, "Bankers offer is $"+avg, "", JOptionPane.DEFAULT_OPTION,
    JOptionPane.QUESTION_MESSAGE, null, options, options[1]);

    if (offers==0){
    System.out.println("You picked DEAL, congratulations you have won $"+avg);
    }
    if (offers==1){
    System.out.println("You picked NO DEAL, you are going to continue playing.");
    }
    }

    }

    }

  4. #4
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Can you please explain what exactly you want to do (expected output) and what incorrect behavior is your program performing. Please don't just say "It does not work". This is not helping. Please help us to help you.
    Tshegofatso Manakana
    a.k.a Untouchable

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Well you clearly haven't understood a number of things.

    Arrays are indexed starting at 0, not 1.
    You have to actually set the integer max to a value - here 10.
    Random.nextInt(max) will return a random integer between 0 and max
    You have to assign this value to something or it will just get thrown away.

    Anyway, I've thought of a much better method than using Random. You can randomise your list of values by using Collections.shuffle(). I suggest you read its documentation before blindly typing it in.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Java Project
    By Smirre in forum New To Java
    Replies: 16
    Last Post: 11-17-2008, 09:37 PM
  2. Java Project
    By Smirre in forum New To Java
    Replies: 7
    Last Post: 11-06-2008, 07:13 PM
  3. how to get the project in java.....
    By thirumurugan.sethu in forum New To Java
    Replies: 2
    Last Post: 10-07-2008, 08:37 AM
  4. Help with Java project
    By ducster in forum New To Java
    Replies: 11
    Last Post: 12-03-2007, 04:08 AM
  5. Java Project Help
    By Gambit17 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 12:53 PM

Posting Permissions

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