gpt4 book ai didi

java - 我如何主要使用其他代码?

转载 作者:行者123 更新时间:2023-12-01 16:21:15 25 4
gpt4 key购买 nike

import java.util.Scanner;

class Character {
public String name;
public int hp;
public int atk;
}//public class Character

class Wizard extends Character {
public void main(){
name = "Wizard";
hp = 15;
atk = 5;
}
}//public class hero extends character

class Warrior extends Character {
public void main(){
name = "Warrior";
hp = 20;
atk = 3;
}
}//public class hero extends character

class Monster extends Character {
public void main() {
name = "Monster";
hp = 5;
atk = 1;
}
}//class Monster extends Character


public class Main{
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int hero;
hero = sc.nextInt();

if(hero==1)
{
human = new Warrior();

} else if(hero==2){

human = new Wizard();}
}

在这种情况下,java main找不到Warrior和Wizard以及怪物。我知道只能覆盖一次。但我希望主代码可以加载战士、巫师和怪物。我该如何修复它?有可能吗?感谢您的帮助。

最佳答案

你必须定义人类。在你的情况下可能是“角色”。更改后,您可以运行您的代码。

...
public class Main{
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int hero;
hero = sc.nextInt();

Character human; //add this line

if(hero==1)
{
human = new Warrior();

} else if(hero==2){

human = new Wizard();}
}

关于java - 我如何主要使用其他代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com