Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Problem attacking n buying in my little game

  1. #1
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Problem attacking n buying in my little game

    please my big bros' here i want to develop a little game but i need a litttle help here.....this is the first time i'm trying to make a game
    i made a method for attacking and a method for buying health and also getting more money while fighting so he can buy more health and still continue in the same fight but i'm having a problem when i put the
    buystuffs() method for money and health in the attacking method... when i run the attacking() method in main and a user inputs 8,the buystuff() method runs but doesnt bring out an option for the user to select
    his choice of what he wants to to ..then the application ends operation.

    and i dont know if 1 can make it possible for the attacking to execute when enter key alone is typed instead of typing an integer

    this is the code for attacking and buying stuffs below ....didnt include the get and set methods..but if need be, i will post it

    Java Code:
    //attacking
     public void attacking(int health,int attack,String oppname)
        {
           
            
            System.out.println("TYPE IN \"1\" TO ATTACK");
           
            while(input.hasNextInt() && alive)
            { //int myhealth=gethealth();
                
                 int oppattack=1+rand.nextInt(attack);
            int myattack=1+rand.nextInt(getstrength());
                
                int ft=input.nextInt();
                //to go to the market
               
                
               System.out.println("attacked!!");
               System.out.println("YOUR LIFE IS = "+gethealth());
               System.out.println(oppname+"'S LIFE IS = "+health);
               
                if(ft==8)
                {
                    buystuffs();
               
                }
               //myhealth=myhealth-oppattack;
               this.health=this.health-oppattack;
               health=health-myattack;
              
               
              if(gethealth()<1)
              {
               System.out.println("YOU ARE DEAD!!");   
              alive=false;
              }
               else if(health<1)
                {
                    System.out.println("YOU KILLED "+oppname);
                }
                  
            
            } 
        }
    
    //buy stuffs.....
    
     public void buystuffs()
        {
            System.out.println("WELCOME TO THE GREAT MARKRT!!!  WHAT DO U WANT");
            System.out.printf("%s\n%s\n%s\n","M-GET MORE MONEY","S-STRENGTH","H-HEALTH");
            String choice= input.nextLine();
           
            switch(choice)
            {
                case "m":
                case "M":
                    System.out.println("THE cashew MAKER WANT U TO GUESS HIS NUMBER(1-4) IF U FAIL,U LOOSE SOME cashew\nGUESS:");
                    int makersnumber=1+rand.nextInt(4);
                    int guess=input.nextInt();
                    if(guess==makersnumber)
                    {
                        System.out.print("YOU GUESSED RIGHT!!....your cashew increased from "+getcashew()+" to "+(getcashew()+10+makersnumber));
                        this.cashew=getcashew()+10+makersnumber;
                    }
                    else
                    {
                      System.out.println("YOU FAILED :-(. his number is "+makersnumber+"!!...your cashew droped from "+getcashew()+" to "+(getcashew()-(1+makersnumber)));
                      this.cashew=getcashew()-(1+makersnumber);
                    }
                    break;
                case "s":
                case "S":
                    na();
                    break;
                case "h":
                case "H": 
                    na();
                    break;
            }
            
           
            
            
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem attacking n buying in my little game

    My guess is that you're using a java.util.Scanner object that in your program is held by a variable called input. If so, one of the quirks of this object is that only the nextLine() method handles the end of line (EOL) token while other calls such as input.nextInt(), input.nextDouble(), input.next() don't handle this token, and this can cause problems if you call one of the non-EOL handling methods, and then call input.nextLine() with the goal of getting the actual next line.

    A suggestion:

    Any time you call input.nextInt(), follow this immediately with a call to input.nextLine() just to gobble up the EOL token. For example, I am suggesting that you change this:

    Java Code:
    int guess=input.nextInt();
    if(guess==makersnumber)
    { 
      //.... etc...
    to something like this:

    Java Code:
    int guess=input.nextInt();
    input.nextLine(); // **** add this line!
    if(guess==makersnumber)
    { 
      //.... etc...
    nonybrighto likes this.

  3. #3
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: Problem attacking n buying in my little game

    Thanks a lot a furable....u really helped me out here...i dont really understand the concept here but i tried the code you posted but it didnt work so i tried adding it before my switch statement because thats were it doesnt work from and it worked!!...

    I did something like this
    Java Code:
     String choice=input.nextLine();
    choice=input.nextLine();  
    switch(choice)
    and it worked....thanks a lot to all and mostly to you furable!!...you are the best!!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem attacking n buying in my little game

    You're welcome!

    ... but "furable"?

  5. #5
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: Problem attacking n buying in my little game

    Lol...sorry...fubarable

Similar Threads

  1. Some problem with game
    By kirayamato in forum NetBeans
    Replies: 13
    Last Post: 12-01-2010, 07:06 AM

Posting Permissions

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