Random Game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YASIN786
    New Member
    • Feb 2007
    • 12

    Random Game

    Hi there
    M developing an game in the form of an applet which has two buttons: Random and Clear
    Random button has to generate random numbers in a textfield labelled start.
    Generating random numbers is not a problem i have used the code below:
    *************** *************** *************** ***
    Random rand = new Random();
    int i = rand.nextInt();
    int n = 10;
    i = rand.nextInt(n+ 1);
    tfld.setText("" + i);
    *************** *************** *************** ****
    but the assignment says that to start the game the player clicks on the random button to generate a random number in the tfld textfield.
    AND when he clicks again the number should be generated in a different textfleld while the first number is retained.
    ANY IDEAS OR HELP WILL BE HIGHLY APPRECIATED
    THNKS
    Yasin
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by YASIN786
    Hi there
    M developing an game in the form of an applet which has two buttons: Random and Clear
    Random button has to generate random numbers in a textfield labelled start.
    Generating random numbers is not a problem i have used the code below:
    *************** *************** *************** ***
    Random rand = new Random();
    int i = rand.nextInt();
    int n = 10;
    i = rand.nextInt(n+ 1);
    tfld.setText("" + i);
    *************** *************** *************** ****
    but the assignment says that to start the game the player clicks on the random button to generate a random number in the tfld textfield.
    AND when he clicks again the number should be generated in a different textfleld while the first number is retained.
    ANY IDEAS OR HELP WILL BE HIGHLY APPRECIATED
    THNKS
    Yasin
    Where's the applet with the textfields then?
    Have a read at this and give it a try.

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by YASIN786
      Hi there
      M developing an game in the form of an applet which has two buttons: Random and Clear
      Random button has to generate random numbers in a textfield labelled start.
      Generating random numbers is not a problem i have used the code below:
      *************** *************** *************** ***
      Random rand = new Random();
      int i = rand.nextInt();
      int n = 10;
      i = rand.nextInt(n+ 1);
      tfld.setText("" + i);
      *************** *************** *************** ****
      but the assignment says that to start the game the player clicks on the random button to generate a random number in the tfld textfield.
      AND when he clicks again the number should be generated in a different textfleld while the first number is retained.
      ANY IDEAS OR HELP WILL BE HIGHLY APPRECIATED
      THNKS
      Yasin
      If the other textfield is already set.... i mean shown...
      You can use any flags... to determine whether the first field has already occupied by a value......

      tell me if it's to far from your point,
      sukatoa

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Originally posted by YASIN786
        Hi there
        M developing an game in the form of an applet which has two buttons: Random and Clear
        Random button has to generate random numbers in a textfield labelled start.
        Generating random numbers is not a problem i have used the code below:
        *************** *************** *************** ***
        Random rand = new Random();
        int i = rand.nextInt();
        int n = 10;
        i = rand.nextInt(n+ 1);
        tfld.setText("" + i);
        *************** *************** *************** ****
        but the assignment says that to start the game the player clicks on the random button to generate a random number in the tfld textfield.
        AND when he clicks again the number should be generated in a different textfleld while the first number is retained.
        ANY IDEAS OR HELP WILL BE HIGHLY APPRECIATED
        THNKS
        Yasin
        On first start of your game, don't create random number at all. When the user presses button,then call your code listed above to show up a number inside the text field.
        When the user clicks the button again, you first check the first text field if it is empty or not. When it is not empty, you call your code listed above again, but with the difference that you write this number inside your second text field:
        tfld2.setText(. ..)

        Comment

        Working...