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

Thread: help me plz

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy help me plz

    so im trying to make a mini game(?)
    and theres somthing i want to do but i dont kno how
    so here's my code

    Scanner scan = new Scanner(System.in);

    System.out.println("enter character name!");
    String Name = scan.nextLine();

    System.out.print("Nice name! hello ");
    System.out.print(Name);
    System.out.print("!");
    System.out.println("look over there! thats ur rival coming!");
    System.out.println("who is your rival name by the way?");

    System.out.println("enter rival name!");
    String rival = scan.nextLine();

    int rival_hp = 100;
    int Name_hp = 100;

    //story start
    System.out.println("ok ,beat him now!");
    System.out.print("system: beat ");
    System.out.print(rival);
    System.out.print(" now!");

    Scanner scan1 = new Scanner(System.in);
    System.out.println("choose the following action");
    System.out.println("1. low kick");
    System.out.println("2. jab");
    System.out.println("3. run");

    String low_kick = "20";
    String jab = "8";

    int n = scan1.nextInt();


    if( n == 1 ) { System.out.print(Name);
    System.out.print(" used low kick!!,");
    System.out.print("rival hp decreased to ");
    Integer result = Integer.valueOf(rival_hp);
    Integer result2 = Integer.valueOf(low_kick);
    int rival_hp2 = result - result2;
    System.out.print(rival_hp2); }


    if( n == 2 ) { System.out.print(Name);
    System.out.print(" used jab !!,");
    System.out.print("rival hp decreased to ");
    Integer result = Integer.valueOf(rival_hp);
    Integer result2 = Integer.valueOf(jab);
    int rival_hp2 = result - result2;
    System.out.print(rival_hp2); }



    if( n == 3 ) { System.out.print("u cant run idiot!!!");
    }


    the question is, how to make rival_hp2 variable useable for the next int i want to use out side the if{}?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: help me plz

    how to make rival_hp2 variable useable for the next int i want to use out side the if{}
    Define the variable(and give it a default value) in the scope where you want to access it. Then at inner scopes you can give it values and use those values.

    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 answer, don't ignore it, ask a question.

Similar Threads

  1. can someone plz help me...
    By benmondo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 24th, 2013, 05:56 PM
  2. I need help Plz
    By websit in forum Object Oriented Programming
    Replies: 3
    Last Post: April 11th, 2012, 12:18 PM
  3. help me up plz
    By jatinrai199 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 6th, 2011, 04:06 PM
  4. Help PLZ .. =(
    By Yazan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 5th, 2010, 12:39 PM

Tags for this Thread