3 Replies - 176 Views - Last Post: 13 February 2013 - 08:29 PM Rate Topic: -----

#1 4elderscrolls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 12

How to make this into a Method?

Posted 13 February 2013 - 06:16 PM

I've been learning Java for about 6 months now (5 month class, ended a month ago) so I still have some troubles with methods.

Is there any way to in a text based game, make a method that will perform this code?

System.out.println("\nYou have "+statPts+" skill points left to place!\n");
			System.out.println("Which skills would you like to increase?");
			System.out.println("HP = "+maxHp+", press 1 to increase by 5");
			System.out.println("MP = "+maxMp+", press 2 to increase by 5");
			System.out.println("Att = "+att+", press 3 to increase by 1");
			System.out.println("Def = "+def+", press 4 to increase by 1");
			System.out.println("Acc = "+acc+", press 5 to increase by 1");
			System.out.println("Eva = "+eva+", press 6 to increase by 1");
			System.out.println("Lck = "+lck+", press 7 to increase by 1");
			int statUp=i.nextInt();
			if(statUp==1){
				maxHp=maxHp+5;
			}else if(statUp==2){
				maxMp=maxMp+5;
			}else if(statUp==3){
				att++;
			}else if(statUp==4){
				def++;
			}else if(statUp==5){
				acc++;
			}else if(statUp==6){
				eva++;
			}else if(statUp==7){
				lck++;
			}
			pts--;



I have all the variables created in other parts of the code, but would like to be able to call this whenever the player gains a level.

Is This A Good Question/Topic? 0
  • +

Replies To: How to make this into a Method?

#2 4elderscrolls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 12

Re: How to make this into a Method?

Posted 13 February 2013 - 06:37 PM

If it helps at all, this is my whole code. What I want to do is replace the while loop with a method.

import java.util.Scanner;
class java{
	public static void main(String args[]){
		Scanner i=new Scanner(System.in);
		int lvl=3;
		/*  >>NOT IMPLEMENTED YET<<
		int exp=0;
		int totExp=0;
		int expNextLvl=50;
		*/
		int maxHp=15;
		int hp=15;
		int maxMp=10;
		int mp=10;
		int att=5;
		int def=5;
		int acc=5;
		int eva=5;
		int lck=5;
		int statPts=2;
		System.out.println("Welcome! You gained 2 levels just by joining the game!");
		System.out.println("You are currently level "+lvl+"!");
		while(pts>0){
			System.out.println("\nYou have "+statPts+" skill points left to place!\n");
			System.out.println("Which skills would you like to increase?");
			System.out.println("HP = "+maxHp+", press 1 to increase by 5");
			System.out.println("MP = "+maxMp+", press 2 to increase by 5");
			System.out.println("Att = "+att+", press 3 to increase by 1");
			System.out.println("Def = "+def+", press 4 to increase by 1");
			System.out.println("Acc = "+acc+", press 5 to increase by 1");
			System.out.println("Eva = "+eva+", press 6 to increase by 1");
			System.out.println("Lck = "+lck+", press 7 to increase by 1");
			int statUp=i.nextInt();
			if(statUp==1){
				maxHp=maxHp+5;
			}else if(statUp==2){
				maxMp=maxMp+5;
			}else if(statUp==3){
				att++;
			}else if(statUp==4){
				def++;
			}else if(statUp==5){
				acc++;
			}else if(statUp==6){
				eva++;
			}else if(statUp==7){
				lck++;
			}
			pts--;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: How to make this into a Method?

Posted 13 February 2013 - 06:46 PM

I would probably use
switch(condition)
, it looks cleaner and then maybe a method call dependent on the condition that you stated. In the case of case 1 "press 1 to increase by 5", maybe make a method that would increment the hp+5?
http://docs.oracle.c...lts/switch.html

EXAMPLE:
package switchexample;
/**
 *
 * @author k3y
 */
import java.util.Scanner;
class SwitchExample {
    private static Scanner console = new Scanner(System.in);
    private static int userInput, tempVariable, valueToModify;
    private static String menu;
    public static void main(String[] args) {
        menu = "Option 1: Add value +5 \nOption 2: Subtract value -1\n";
        System.out.println(menu);
        userInput = getUserInput();
        System.out.println("Please enter value to modify");
        valueToModify = getUserInput();
        switch(userInput){
            case 1: 
                System.out.println(addValue(valueToModify));
                break;
            case 2: 
                System.out.println(subValue(valueToModify));
                break;
            default:
                System.out.println("uh...");
                break;
        }
    }
    static int getUserInput(){
        tempVariable = console.nextInt();
        return tempVariable;
    }
    static int addValue(int x){
        tempVariable = x + 5;
        return tempVariable;
    }
    static int subValue(int x){
        tempVariable = x - 1;
        return tempVariable;
    }
}


This post has been edited by k3y: 13 February 2013 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: How to make this into a Method?

Posted 13 February 2013 - 08:29 PM

After 6 months of Java you must have learnt how to use classes :) ?

I did help another guy some time ago with a similar problem
The problem was about skill distribution, but I guess you could use some of the same logic in attribute distribution
I made this example to show him how helpful it is to make good named classes and methods
The methods solve simple tasks, and the responsibilities are spread out into different classes
public void distributeSkillPoints(SkillTree skillTree) {
	for (int remaining = skillTree.getRemainingPoints(); remaining > 0;) {
		Skill skill = chooseSkill(skillTree);
		int points = getPointsToDistribute(skill, remaining);
		skill.increase(points);
		remaining -= points;
	}
}

public Skill chooseSkill(SkillTree skillTree) {
	//Print the available skills
	//let the user choose a skill
	//return the chosen skill
}

public int getPointsToDistribute(Skill skill, int max) {
	//Get how many points we should put into this skill
	//The returned value must not exceed max
}


This post has been edited by CasiOo: 13 February 2013 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1