我的java程序已编译,但赢得了';跑不动
我的java程序可以编译,但在尝试运行它时不会返回任何内容。我正在做一个作业,在作业中我使用了多种方法,我不确定我是否对这些方法做了一些错误的事情,这些错误可能会影响程序的运行我的java程序已编译,但赢得了';跑不动,java,compiler-errors,Java,Compiler Errors,我的java程序可以编译,但在尝试运行它时不会返回任何内容。我正在做一个作业,在作业中我使用了多种方法,我不确定我是否对这些方法做了一些错误的事情,这些错误可能会影响程序的运行 import java.util.Scanner; import java.util.Random; public class CombatCalculatorExpansion1{ public static void main(String[] args){ int monstersHe
import java.util.Scanner;
import java.util.Random;
public class CombatCalculatorExpansion1{
public static void main(String[] args){
int monstersHealth = 100;
int monsterAttackpower = 0;
String monstersName;
int xp;
int yourHealth = 0;
int yourAttackpower = 0;
int magicPower = 0;
int magicPoints = 0;
int statPoints = 20;
}
public static void createHero(){
int yourHealth = 0;
int yourAttackpower = 0;
int magicPower = 0;
int magicPoints = 0;
int statPoints = 20;
boolean loopControl = true;
while (loopControl){
System.out.println("***************************");
System.out.println("Health: " + yourHealth);
System.out.println("Attack: " + yourAttackpower);
System.out.println("Magic: " + magicPower);
System.out.println("***************************");
System.out.println("5.) +10 Health");
System.out.println("6.) +1 Attack");
System.out.println("7.) +3 Magic");
System.out.println("You have 20 points to spend: " + statPoints);
System.out.println("***************************");
int choice;
Scanner inputReader = new Scanner(System.in);
choice = inputReader.nextInt();
if(choice == 5){
yourHealth = yourHealth + 10;
statPoints = statPoints - 1;
} else if(choice == 6){
yourAttackpower = yourAttackpower + 1;
statPoints = statPoints - 1;
} else if(choice == 7){
magicPower = magicPower + 3;
statPoints = statPoints - 1;
} else {
System.out.println("I don't understand that command.");
} if(statPoints <= 0){
loopControl = false;
System.out.println("You don't have any stat points left to spend.");
}
}
}
public static void runCombat(){
int monstersHealth = 100;
int monsterAttackpower = 0;
String monstersName;
int xp;
int yourHealth = 0;
int yourAttackpower = 0;
int magicPower = 0;
int magicPoints = 0;
int statPoints = 20;
boolean loopControl = true;
while (loopControl){
System.out.println("Combat Options");
System.out.println("1.) Sword Attack");
System.out.println("2.) Cast Spell");
System.out.println("3.) Charge Mana");
System.out.println("4.) Run Away");
System.out.println("What Action do you want to perform?");
int choice;
Scanner inputReader = new Scanner(System.in);
choice = inputReader.nextInt();
if(choice == 1){
monstersHealth = monstersHealth - yourAttackpower;
System.out.println("You strike the goblin with your sword for 12 damage!");
System.out.println(monstersHealth + " health remaining");
} else if (choice == 2){
if(magicPower >= 3){
System.out.println("You cast the weaken spell on the monster.");
monstersHealth = monstersHealth / 2;
} else
System.out.println("You don't have enough mana.");
System.out.println(monstersHealth + " health remaining");
} else if (choice == 3){
magicPower = magicPower + 1;
System.out.println("You focus and charge your magic power.");
} else if (choice == 4){
loopControl = false;
System.out.println("You run away!");
} else if (choice >= 8){
System.out.println("I don't understand that command.");
}
if(monstersHealth <= 0){
loopControl = false;
System.out.println("You defeated the goblin!");
}
}
}
public static void createMonster(){
int monstersHealth = 100;
int monsterAttackpower = 0;
String monstersName;
int xp;
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(2);
if (randomNumber == 0){
monstersName = "Goblin";
monstersHealth = 75 + randomGenerator.nextInt(24);
monsterAttackpower = 8 + randomGenerator.nextInt(4);
xp = 1;
}
else if (randomNumber == 1){
monstersName = "Orc";
monstersHealth = 100 + randomGenerator.nextInt(24);
monsterAttackpower = 12 + randomGenerator.nextInt(4);
xp = 3;
}
else if (randomNumber == 2){
monstersName = "Troll";
monstersHealth = 150 + randomGenerator.nextInt(49);
monsterAttackpower = 15 + randomGenerator.nextInt(4);
xp = 5;
}
}
}
import java.util.Scanner;
导入java.util.Random;
公共类作战计算扩展1{
公共静态void main(字符串[]args){
int monstersHealth=100;
攻击力=0;
弦怪兽;
int-xp;
int yourheath=0;
int yourAttackpower=0;
int magicPower=0;
int magicPoints=0;
int statPoints=20;
}
公共静态void createHero(){
int yourheath=0;
int yourAttackpower=0;
int magicPower=0;
int magicPoints=0;
int statPoints=20;
布尔循环控制=真;
while(循环控制){
System.out.println(“*******************************”);
System.out.println(“健康:+yourHealth”);
System.out.println(“攻击:+yourAttackpower”);
System.out.println(“Magic:+magicPower”);
System.out.println(“*******************************”);
系统输出打印项次(“5”)+10健康度);
System.out.println(“6.)+1攻击”);
System.out.println(“7.)+3魔术”);
System.out.println(“你有20点要花:“+statPoints”);
System.out.println(“*******************************”);
智力选择;
扫描仪输入读取器=新扫描仪(System.in);
choice=inputReader.nextInt();
如果(选项==5){
yourHealth=yourHealth+10;
统计点=统计点-1;
}else if(选项==6){
yourAttackpower=yourAttackpower+1;
统计点=统计点-1;
}else if(选项==7){
magicPower=magicPower+3;
统计点=统计点-1;
}否则{
System.out.println(“我不明白那个命令。”);
}如果(statPoints=3){
System.out.println(“你对怪物施放了削弱法术。”);
monstersHealth=monstersHealth/2;
}否则
System.out.println(“你没有足够的法力。”);
System.out.println(monstersHealth+“剩余生命值”);
}else if(选项==3){
magicPower=magicPower+1;
System.out.println(“你聚焦并充电你的魔力。”);
}else if(选项==4){
loopControl=false;
System.out.println(“你跑了!”);
}否则如果(选项>=8){
System.out.println(“我不明白那个命令。”);
}
如果(monstersHealth如我所见,主方法中没有方法调用,请尝试在主方法中调用静态方法,如下所示
public static void main(String[] args){
int monstersHealth = 100;
int monsterAttackpower = 0;
String monstersName;
int xp;
int yourHealth = 0;
int yourAttackpower = 0;
int magicPower = 0;
int magicPoints = 0;
int statPoints = 20;
createHero();
createMonster();
runCombat();
}
您没有在main函数中调用任何方法,因此它只是创建您在main中声明的变量,并返回,而不提供任何输出
您需要在main函数中创建流
public static void main(String[] args){
int monstersHealth = 100;
int monsterAttackpower = 0;
String monstersName;
int xp;
int yourHealth = 0;
int yourAttackpower = 0;
int magicPower = 0;
int magicPoints = 0;
int statPoints = 20;
createHero();
runCombat();
}
你没有调用任何方法,java不应该以这种方式使用!!
对于您的情况,在main方法中初始化变量是没有用的,您可以在main方法中调用函数(根据我的说法,如果您在构造函数中调用函数,而不是在main方法中调用函数,并将变量用作类的实例变量,那么您可以在不同的方法中使用这些函数)
JavaScript不是Java.Java!==javascriptin其主函数不调用任何方法当它不运行时意味着什么。有错误消息吗?有调试尝试吗?
public CombatCalculatorExpansion1{
createHero();
createMonster();
runCombat();
}