Results 1 to 3 of 3
  1. #1
    anthonys56 is offline Member
    Join Date
    Apr 2017
    Posts
    1
    Rep Power
    0

    Default Need help with a program i'm trying to make.

    I just started teaching myself how to code in java almost a week ago as my first programming language, and was attempting to create sort of a game in which you start with a preset value and in order to raise that value to achieve a higher score a certain preset value is removed before a randomly generated value is added. The problem I've run into is the preset value is not being subtracted. I created two classes, one being an extension of the other to keep things a bit organised as I try to work this out. Any advise or corrections to my code would be helpful. I have only been doing this a few days so the code is a bit simple.

    import java.util.Scanner;
    public class numbergeneratorgame
    {
    static Scanner UserInput = new Scanner(System.in);

    public static void main(String[] args)
    {
    System.out.println("Welcome to the game. ");

    String stringa = UserInput.nextLine();
    while(stringa.equalsIgnoreCase("yes"))
    {
    int randomNumber = numberGenerator(0);
    System.out.println(randomNumber);
    System.out.println("Would you like another? ");
    UserInput.nextLine();

    }

    }

    public static int numberGenerator(int randomNumber)
    {
    randomNumber = (int)(Math.random()* 31);
    return randomNumber;

    }

    }


    import java.util.Scanner;
    public class numgengameP2 extends numbergeneratorgame {

    static Scanner UserInput = new Scanner(System.in);
    public static void main(String[] args)
    {
    int startingValue = 25;
    System.out.println("Your starting value is: " + startingValue);

    while(startingValue > 0)

    {
    String stringa = UserInput.nextLine();
    while(stringa.equalsIgnoreCase("yes"))
    {
    if(stringa == "yes")
    {
    startingValue -= 20;
    }

    startingValue = startingValue + numberGenerator(0);
    System.out.println(startingValue);
    System.out.println("Would you like to continue? ");
    UserInput.nextLine();

    }
    if(startingValue <= 0)
    {

    break;
    }

    }

    System.out.println("The game is over. ");
    System.out.println("Your final score is: " + startingValue);
    }

    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Need help with a program i'm trying to make.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with a program i'm trying to make.

    I suggest you wait to write a game or something of your own idea until you have gone thru more tutorials or read a book on Java and have programmed the examples that follow what you have been taught. Examples, would be classes, objects, primitives, inheritance, interfaces, etc. And specific classes to learn about would be String, ArrayList, HashMap, and the primitive wrappers (e.g. Integer for int, Double for double). It will then make your game writing easier and more enjoyable.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 19
    Last Post: 07-17-2015, 03:40 PM
  2. How to make a make a Console program?
    By Snake006 in forum New To Java
    Replies: 7
    Last Post: 11-11-2014, 12:17 PM
  3. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  4. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  5. Replies: 3
    Last Post: 11-26-2010, 03:08 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
  •