java - 通过其他方法发送和接收单元

原文 标签 java function methods netbeans

在一个方法游戏中,我试图从敌方方法接收大量的伤害和生命,还试图从方法用户处接收用户的生命和伤害,那么一个方法可以从其他方法接收两个以上的整数以便执行功能吗? ,因此该方法游戏具有所有信息,因此我可以执行与敌方互动的用户的算术功能

    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);
}}

最佳答案

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

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;
}
}


这样,您可以根据区域的形状类型调用相同的方法

相关文章:

function - 为什么编译器以不同方式处理闭包和本地函数?

c - C程序仅接受整数1-42进入数组,并且它们无法在函数中重复

python - 确实会返回值的方法会更改原始数据类型

java - 无法从B类访问A类的公共方法

java - Java正则表达式-解析函数名称和参数名称-引发PatternSyntaxException:未封闭字符

java - OSGi找不到激活器

java - 查询Java编程中的内聚性

php - Javascript AJAX函数调用延迟

java - 测试产生随机结果的方法

r - 使用正文,形式和环境方法修改R中的函数