从其他方法发送和接收单位

时间:2018-01-28 01:46:09

标签: java function methods netbeans

单个方法可以从其他方法接收超过2个整数,因此它可以执行一个函数吗?在方法游戏中我试图从敌人的方法中接收大量的edamage和elife,并试图接收用户的生命和伤害从方法用户,所以方法游戏具有所有信息,所以我可以执行用户与敌人交互的算术功能

    public static void game(String[] args, int elife) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int life=80;
    int damage = 8;
    int A = (elife - damage);
    String enemy ="";
    int Enemy;
    Random generator = new Random();
    Enemy = generator.nextInt(3)+1; 
        if (Enemy == 1) 
        {enemy = "Grunt";
            int hp=45;
        int shp= Integer.parseInt(br.readLine());}
        else if (Enemy == 2) 
        {enemy = "Slave";
            int hp =40; }
        else if (Enemy == 3) 
        {enemy= "Witch";
        int hp=50;}
    System.out.println("Welcome to the game!");
    user();
    System.out.println("Get Ready to face you first opponent ");
    System.out.println("You are Faced against " + enemy);
    System.out.println("Would you like to: \n [A]Attack         [B]Defend");
    String choice = br.readLine();
    if (choice.equals("A")){
        System.out.println(""+A+"");
        System.out.println("You chose to attack "+enemy+" for "+"damage!");}
    else if(choice.equals("B")){
        System.out.println("You chose to defend and recieved"+"damage.");
}}
public static void user () throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int life= 80;
    int damage = 8;
    System.out.print("Please enter your name: ");
    String name = br.readLine();
    System.out.println("WELCOME "+name); 
}


public static void Witch() throws IOException{   
    int elife = 50;
    int damage;    
    int edamage = 0;
    Random generator = new Random();
    damage = generator.nextInt(3)+1; 
        if (damage == 1) 
    edamage = 5; 
        else if (damage == 2) 
    edamage = 10; 
        else if (damage == 3) 
    edamage = 12; 
    System.out.println("Enemy life : "+elife);
    System.out.println("Enemy damage : " +edamage);
}
}
public static void Grunt(){   
    int elife = 45;
    int damage;    
    int edamage = 0;
    Random generator = new Random();
    damage = generator.nextInt(3)+1; 
        if (damage == 1) 
    edamage = 3; 
        else if (damage == 2) 
    edamage = 8; 
        else if (damage == 3) 
    edamage = 15; 
    System.out.println("Enemy life : "+elife);
    System.out.println("Enemy damage : " +edamage);
}   
public static void Slave(){  
    int elife = 40;
    int damage;    
    int edamage = 0;
    Random generator = new Random();
    damage = generator.nextInt(3)+1; 
        if (damage == 1) 
    edamage = 7; 
        else if (damage == 2) 
    edamage = 8; 
        else if (damage == 3) 
    edamage = 5; 
    System.out.println("Enemy life : "+elife);
    System.out.println("Enemy damage : " +edamage);
}}

1 个答案:

答案 0 :(得分:2)

方法重载(也称为静态多态)是一种在单个类中具有相同名称的两个(或多个)方法(函数)的方法。是的,就这么简单。

public class Shape{
//It could be a circle or rectangle or square
private String type;
//To calculate area of rectangle
public Double area(Long length, Long breadth){
return (Double) length * breadth;
}
//To calculate area of a circle
public Double area(Long radius){
return (Double) 3.14 * r * r;
}
}

这样你可以根据它所具有的形状类型为区域调用相同的方法