12 Replies Latest reply: Nov 14, 2007 5:58 PM by 807603 RSS

    public static void vs. public static int?

    807603
      Hey guys Im trying to transfer variable between classes and it was working at first but now that Ive tinkered with it it doesnt, someone suggested I replace all the voids I have with int instead, I did and now I get the error "battle(int,int,int,int...) cannot be applied to (int,int,int,int)"

      Im not sure what Im doing wrong, if someone could point me in the riht diresction Id be extremely grateful.

      Enemy
      import java.util.Random;
      public class Enemy{
           
           public static int enhp = 0, enmp = 0, enblck = 0, enstrk = 0, a = 0, eninitiative = 0;
         public static int danger () {
                   
              
                   Random generator = new Random();
                   a = generator.nextInt(9)+1;
               System.out.println(a);
               if(a>=8){
                     enhp = 120;
                     enmp = 70; 
                     enblck = 30; 
                     enstrk = 40;
                     eninitiative = 125;      
               System.out.println("Your'e fighting the Black knight");
               System.out.println("His health is " + enhp);
               System.out.println("His magic is " + enmp);
               System.out.println("His defence is " + enblck);
               System.out.println("His strength is " + enstrk);
               System.out.println("His speed is " + eninitiative);
               Fight.battle(enhp, enmp, enblck, enstrk);
               }
               else if(a>=5&&a<=7){
                     enhp = 60;
                     enmp = 40; 
                     enblck = 1; 
                     enstrk = 2;
                     eninitiative = 75;
               
               System.out.println("Your'e fighting the Troll");
               System.out.println("His health is " + enhp);
               System.out.println("His magic is " + enmp);
               System.out.println("His defence is " + enblck);
               System.out.println("His strength is " + enstrk);
               System.out.println("His speed is " + eninitiative);
               Fight.battle(enhp, enmp, enblck, enstrk);
               }
               else if(a>=3&&a<=4){
                    enhp = 60;
                     enmp = 40; 
                     enblck = 1; 
                     enstrk = 2; 
                     eninitiative = 25;
               Fight.battle(enhp, enmp, enblck, enstrk);
       
               System.out.println("Your'e fighting the Minion");
               System.out.println("His health is " + enhp);
               System.out.println("His magic is " + enmp);
               System.out.println("His defence is " + enblck);
               System.out.println("His strength is " + enstrk);
               System.out.println("His speed is " + eninitiative);
               }
               else if(a<=2){
                     enhp = 60;
                     enmp = 40; 
                     enblck = 1; 
                     enstrk = 2;
                     eninitiative = 5;
               Fight.battle(enhp, enmp, enblck, enstrk);
               System.out.println("Your'e fighting the Wolf");
                System.out.println("His health is " + enhp);
               System.out.println("His magic is " + enmp);
               System.out.println("His defence is " + enblck);
               System.out.println("His strength is " + enstrk);
               System.out.println("His speed is " + eninitiative);
               }
               return enhp;
               return enmp;
               return enblck;
               return enstrk;
               return eninitiative;
              }
      }
      Fight
      import java.util.Scanner;
      public class Fight {
          public static void battle
               (int hp, int mp, int blck, int strk, int myspace, int initiative, int enhp, int enmp, int enblck, int enstrk, int a, int eninitiative){
               int health = hp; 
               int mana = mp;
               int defence = blck;
               int attack = strk;
               int inventory = myspace;
               Scanner input = new Scanner(System.in).useDelimiter("\n");
               System.out.println("Would you like to attack or defend?"); 
               String action = input.nextLine();
          if (action.compareToIgnoreCase("attack") == 0){  
              System.out.println(health*2);
              System.out.println(mana*2);
              System.out.println(defence*2);
              System.out.println(attack*2);
              System.out.println(inventory*2);
              return;
          }
          else if (action.compareToIgnoreCase("defend") == 0){          
               System.out.println(health/2);
              System.out.println(mana/2);
              System.out.println(defence/2);
              System.out.println(attack/2);
              System.out.println(inventory/2);
              return;
               }
      }
      ;}
               /**if(initiative>eninitiative){//Prompt for descion then appply, then enemy does the the same, loop until is someone is dead
               }
               if(initiative<eninitiative){//Enemy's turn, then prompt for action, loop someone is dead
               }*/
          
      
          
          
         
        • 1. Re: public static void vs. public static int?
          807603
          The error message couldn't be more obvious:
          Enemy.java:23: battle(int,int,int,int,int,int,int,int,int,int,int,int) in Fight cannot be applied to (int,int,int,int)
          Fight.battle(enhp, enmp, enblck, enstrk);
          How many ints should you pass to the method battle? In Java you must pass the right number.
          • 2. Re: public static void vs. public static int?
            807603
            Thats because half of the values are coming from Enemy and half are coming from profession. Is that not okay? If not what do you do to fix it?
            Profession
            //creates random a and starts choice of profession and begins game
            import java.util.Scanner;
            import java.util.Random;
            public class Profession {
                 
                static int  job () { 
                
                     Scanner input = new Scanner(System.in).useDelimiter("\n");
                     int hp = 0, mp = 0, blck = 0, strk = 0, myspace = 0, health = 0, magic = 0, defence = 0, strike = 0, inventory = 0, a = 0, initiative = 0;
                     Random generator = new Random();
                     System.out.println("Would you like to be a mage or a warrior?"); 
                          System.out.println("If you choose anything else your stupidity will be reflected.");
                     String character = input.nextLine();
                     if (character.compareToIgnoreCase("mage") == 0){         
                           hp = 60;
                           mp = 40; 
                           blck = 1; 
                           strk = 2; 
                           myspace = 4;
                           initiative = 16;
                          System.out.println("You have chosen to be a mage");
                          System.out.println("Health is " + hp);
                          System.out.println("Mana is " + mp); 
                          System.out.println("Defence is " + blck);
                          System.out.println("Power is " + strk);
                          System.out.println("Invetory space is " + myspace);
                          System.out.println("Speed is " + initiative);
                          Fight.battle(hp, mp, blck, strk, myspace);
                               
                     } else if (character.compareToIgnoreCase("warrior") == 0){          
                                hp = 40;
                                 mp = 60;
                                 blck = 2;
                                 strk = 1; 
                                myspace = 2;
                                initiative = 4;
                          System.out.println("You have chosen to be a mage");
                          System.out.println("Health is " + hp);
                          System.out.println("Mana is " + mp); 
                          System.out.println("Defence is " + blck);
                          System.out.println("Power is " + strk);
                          System.out.println("Invetory space is " + myspace);
                          System.out.println("Speed is " + initiative);
                          Fight.battle(hp, mp, blck, strk, myspace);
                          
                     } else if ( character.compareToIgnoreCase("Huntington Van Vidal") == 0){
                               hp = 100;
                               mp = 100;
                               blck = 100;
                               strk = 100;
                               myspace = 100;
                               initiative = 100;
                                health = hp;
                                magic = mp;
                           System.out.println("Health is " + hp);
                          System.out.println("Mana is " + mp); 
                          System.out.println("Defence is " + blck);
                          System.out.println("Power is " + strk);
                          System.out.println("Invetory space is " + myspace);
                          System.out.println("Speed is " + initiative);
                          Fight.battle(hp, mp, blck, strk, myspace);
                                
                     } else {
                               hp = 0;
                               mp = 0;
                               blck = 0;
                               strk = 0;
                               myspace = 0;
                               initiative = 0;
                           System.out.println("Health is " + hp);
                          System.out.println("Mana is " + mp); 
                          System.out.println("Defence is " + blck);
                          System.out.println("Power is " + strk);
                          System.out.println("Invetory space is " + myspace);
                          System.out.println("Speed is " + initiative);
                          Fight.battle(hp, mp, blck, strk, myspace);     
                     }
                     
                }
                
             
                }
                
            • 3. Re: public static void vs. public static int?
              796447
              paranoiapenguins wrote:
              Thats because half of the values are coming from Enemy and half are coming from profession. Is that not okay? If not what do you do to fix it?
              What??? A method in a class has no idea where any of its parameters are "coming from". If you have a method which takes 12 parameters, then you have
              to pass it 12 parameters (of the matching types too of course).

              As an aside, a method taking 12 parameters looks pretty messy. Sounds like you're designing a giant mess.
              • 4. Re: public static void vs. public static int?
                807603
                no I need to pass variables from two classes to the same class were they can be used together. How would I go about doing that?
                • 5. Re: public static void vs. public static int?
                  807603
                  How about creating some appropriate objects? As a starter, you should not define static methods. The only static method you really need is main.
                  • 6. Re: public static void vs. public static int?
                    807603
                    return enhp;
                             return enmp;
                             return enblck;
                             return enstrk;
                             return eninitiative;
                    Methods can only return one value. As mentioned above you need to create and pass objects as your parameters (references to those objects actually).
                    • 7. Re: public static void vs. public static int?
                      807603
                      Here are 2 suggestions

                      This code here
                      int hp, int mp, int blck, int strk, int myspace, int initiative, int enhp, int enmp, int enblck, int enstrk, int a, int eninitiative){
                           int health = hp;

                      should be

                      int health = 0;

                      don't even use the variables hp, mp, etc. that I see

                      etc

                      but each variable on one line by itself and intitalize it. Very important for readiblity and consistency.
                      • 8. Re: public static void vs. public static int?
                        807603
                        Parapenguin was advised about this several days ago and like a trooper totally ignored it!
                        • 9. Re: public static void vs. public static int?
                          807603
                          Its because Im not sure how to make an object, and I can't find someone to explain it, or a tutorial I can understand
                          • 10. Re: public static void vs. public static int?
                            807603
                            paranoiapenguins wrote:
                            Its because Im not sure how to make an object, and I can't find someone to explain it, or a tutorial I can understand
                            Er, it's only the single most important thing one does in Java or any object-oriented language. Are you sure you can't find any explanation in a book or tutorial?

                            http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
                            • 11. Re: public static void vs. public static int?
                              807603
                              paranoiapenguins wrote:
                              Its because Im not sure how to make an object, and I can't find someone to explain it, or a tutorial I can understand
                              Right! So either you are lying or stupid or you just stole code:
                              Random generator = new Random();
                              • 12. Re: public static void vs. public static int?
                                807603
                                Im entry level at java guys, there's no need to be rude, and yes Ive written every bit of my code, me and few of my friends are ahead of our class so were trying to teach ourselves so thats why I may not be up on some basic terms