- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Stackoverflow(以及编码)的新手,我真的希望你能在这里帮助我。我正在 BlueJ 上制作一个基于文本的蝴蝶效应游戏。我首先有我的主要“RPG”代码,然后是我的“Level1ZombieClass”。我的问题是我的用户类变量不断出现初始化错误(因为我需要将它放在我的僵尸类中才能进行“if”语句/战斗工作,而且我不知道如何修复它。在第一个中我想添加我的僵尸类的一组代码,这样我就没有可以轻松地用几行替换的大量代码。同样,问题是用户类变量是用户定义的东西,而不是我定义的东西在代码中定义。Just a screenshot here。非常感谢你们,非常感谢你们的帮助。
import java.util.Random;
import java.util.Scanner;
public class RPG
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
String name;
int playerClass;
int wincount=0;
int userhealth=100+(wincount)*3;
int comphealth=100;
int hc;
int userChoice=1;
int killcount;
int regularAttackD;
int regularAttackHC;
int powerAttackD;
int powerAttackHC;
int shield;
int quickAttackHC;
int quickAttackD;
int compChoice=1;
int playerchoice;
Random a = new Random ();
Random b = new Random ();
Random c = new Random ();
Random d = new Random ();
Random e = new Random ();
Random f = new Random ();
Random g = new Random ();
Random h = new Random ();
System.out.println ("Welcome to the zombie apocalypse...\nIn this text-driven game you will need to make decisions based on game prompts.\nYour decisions will determine your own fate.");
System.out.println ("What would you like to be called?");
name = in.next();
System.out.println (name + ",in this game you may need to fight enemies in a turn-based combat style.\nYou will now enter a number for the class you want.\n(1)Scout: Increased hit chance for all attacks.\n(2)Brute: Increased damage for all attacks\n(3)Medic:Increased health during combat.");
playerClass = in.nextInt();
switch (playerClass)
{
case 1:
System.out.println ("You have selected to be a scout! Hit chance increased!");
break;
case 2:
System.out.println ("You have selected to be a brute! Power increased!");
powerAttackD=b.nextInt(15)+28;
regularAttackD=d.nextInt(8)+18;
quickAttackD=f.nextInt (4)+4;
break;
case 3:
System.out.println ("You have selected to be a medic! Health increased!");
userhealth=115;
break;
}
System.out.println ("You now find yourself in an deserted town center. Pharmacy to your left (Looks to be completely ravaged), restaurant to your right, town hall is a couple hundred feet ahead of you.\nWhere do you want to go?\nPharmacy:1\nRestaurant:2\nTown Hall:3");
playerchoice=in.nextInt();
switch (playerchoice)
{
case 1:
System.out.println ("Nothing of use seems to be apparent when you walk in the door. There is a dead body slouched against the far wall to your right.\nYou make your way down the 5th and middle aisle. Still nothing of use. You get to the end of the store and hit a glass jar of a shelf.\nStill silence in the store. You walk to the entrance of the store empty-handed, and the presumed dead body grabs a hold of you!");
//BEGINNING OF COMBAT
Level1ZombieClass Level1ZombieClassObject = new Level1ZombieClass ();
Level1ZombieClassObject.ZombieLevel1Class();
}
} }
然后我的僵尸类:
//回合制战斗,用户输入他们想要的攻击方式。
import java.util.Random;
import java.util.Scanner;
public class Level1ZombieClass
{
public void ZombieLevel1Class()
{
Scanner in = new Scanner(System.in);
int userhealth=100;
int comphealth=100;
int playerClass;
int hc;
int userChoice=1;
int critdamage;
int regularAttackD;
int regularAttackHC;
int powerAttackD;
int powerAttackHC;
int shield;
int quickAttackHC;
int quickAttackD;
int compChoice=1;
int wincount=0;
boolean winCondition = true;
Random a = new Random ();
Random b = new Random ();
Random c = new Random ();
Random d = new Random ();
Random e = new Random ();
Random f = new Random ();
Random g = new Random ();
Random h = new Random ();
do {
hc=a.nextInt(99)+1;
powerAttackD=b.nextInt(15)+25;
regularAttackD=d.nextInt(8)+15;
quickAttackD=f.nextInt (4)+3;
shield=g.nextInt(15)+10;
System.out.println ("What move would you like to use?");
System.out.println ("1:Regular Attack");
System.out.println ("2:Power Attack: Higher damage at a lower hit chance.");
System.out.println ("3:Quick attacks: 3 weaker attacks at a higher hit chance.");
System.out.println ("4:Shield: 100% chance to work and you do not attack, but the damage you receive is reduced for the next incoming attack.");
userChoice=in.nextInt();
if (userChoice==1){
if (playerClass==2){
powerAttackD=b.nextInt(15)+28;
regularAttackD=d.nextInt(8)+18;
quickAttackD=f.nextInt (4)+4;
}
if (compChoice==4){
regularAttackD=regularAttackD-shield;
System.out.println ("Level 1 zombie used a shield! Damage reduced by " + shield + ".");
}
if (playerClass==1){
if (hc>=14){
if(regularAttackD<0){
regularAttackD=0;
}
System.out.println ("Attack successful for " + regularAttackD + " damage.");
comphealth=comphealth-regularAttackD;
}else{
System.out.println ("Attack failed!");}
}else{
if (hc>=18){
if (regularAttackD<0){
regularAttackD=0;
}
System.out.println ("Attack successful for " + regularAttackD + " damage.");
comphealth=comphealth-regularAttackD;
}else{
System.out.println ("Attack failed.");
}
}
if (comphealth <=0) {
System.out.println ("Level 1 zombie health is critically low! You win!");
wincount++;
winCondition = false;
break;
}
System.out.println ("Level 1 zombie health now at " + comphealth + ".");
}
if (userChoice==2){
if (compChoice==4){
powerAttackD=powerAttackD-shield;
System.out.println ("Level 1 zombie used a shield! Damage reduced by " + shield + ".");
}
if (playerClass==1){
if (hc>=26){
if(powerAttackD<0){
regularAttackD=0;
}
System.out.println ("Attack successful for " + powerAttackD + " damage.");
comphealth=comphealth-powerAttackD;
}else{
System.out.println ("Attack failed!");}
}else{
if (hc>=30){
if (powerAttackD<0){
powerAttackD=0;
}
System.out.println ("Attack successful for " + powerAttackD + " damage.");
comphealth=comphealth-powerAttackD;
}else{
System.out.println ("Attack failed.");
}
}
if (comphealth <=0) {
System.out.println ("Level 1 zombie health is critically low! You win!");
wincount++;
winCondition = false;
break;
}
System.out.println ("Level 1 zombie health is now at " + comphealth + ".");
}
if (userChoice==3)
{
quickAttackD=quickAttackD*3;
if (compChoice==4){
quickAttackD=quickAttackD-shield;
System.out.println ("Level 1 zombie used a shield! Damage reduced by " + shield + ".");
}
if (playerClass==1){
if (hc>=14){
if(quickAttackD<0){
quickAttackD=0;
}
System.out.println ("Attack successful for " + quickAttackD + " damage.");
comphealth=comphealth-quickAttackD;
}else{
System.out.println ("Attack failed!");}
}else{
if (hc>=18){
if (quickAttackD<0){
quickAttackD=0;
}
System.out.println ("Attack successful for " + quickAttackD + " damage.");
comphealth=comphealth-quickAttackD;
}else{
System.out.println ("Attack failed.");
}
}
if (comphealth <=0) {
System.out.println ("Level 1 zombie health is critically low! You win!");
wincount++;
winCondition = false;
break;
}
System.out.println ("Level 1 zombie health is now at " + comphealth + ".");
}
//BEGINNING OF COMPUTER'S TURN
compChoice=h.nextInt(3)+1; //the computer's choice of attack.Placed after user turn to prevent a first turn mess up
hc=a.nextInt(99)+1;
powerAttackD=b.nextInt(15)+20;
regularAttackD=d.nextInt(8)+10;
quickAttackD=f.nextInt(4)+1;
shield=g.nextInt(15)+6;
if (userhealth<15)
{
compChoice=h.nextInt(2)+1;
}
if (compChoice==1){
if (userChoice==4){
regularAttackD=regularAttackD-shield;
System.out.println ("You have used a shield! Damage reduced by " + shield + ".");
}
if (hc>=20){
if (regularAttackD<0){
regularAttackD=0;
}
System.out.println ("Level 1 zombie's attack successful for " + regularAttackD + " damage.");
userhealth=userhealth-regularAttackD;
}else{
System.out.println ("Level 1 zombie's attack failed.");
}
if (userhealth <=0) {
System.out.println ("You have died.");
winCondition = false;
break;
}
System.out.println ("Your health now at " + userhealth + ".");
}
if (compChoice==2){
if (userChoice==4){
powerAttackD=powerAttackD-shield;
System.out.println ("You have used a shield! Damage reduced by " + shield + ".");
}
if (hc>=40){
if (powerAttackD<0){
powerAttackD=0;
}
System.out.println ("Level 1 zombie's power attack hits successfully for " + powerAttackD + " damage!");
userhealth=userhealth-powerAttackD;
}else{
System.out.println ("Oppenent's attack failed!");
}
if (userhealth <=0) {
System.out.println ("You have died.");
winCondition = false;
break;
}
System.out.println ("Your health is now at " + userhealth + ".");
}
if (compChoice==3)
{
quickAttackD=quickAttackD*3;
if (userChoice==4){
quickAttackD=quickAttackD-shield;
System.out.println ("You have used a shield! Damage reduced by " + shield + ".");
}
if (hc>=10){
if (quickAttackD<0){
quickAttackD=0;
}
System.out.println ("Level 1 zombie's quick attacks hit for a total of " + quickAttackD + " damage!");
userhealth=userhealth-quickAttackD;
}else{
System.out.println ("Level 1 zombie's attack missed!");
}
if (userhealth <=0) {
System.out.println ("You have died.");
winCondition = false;
break;
}
System.out.println ("Your health is now at " + userhealth + ".");
}
} while (winCondition); //don't define winCondition, IT IS ALREADY TRUE
}}
最佳答案
由于您有一个类,因此您应该向其添加一个构造函数,该构造函数包含用户输入的所有参数,并在创建游戏实例时传递这些参数。
例如,如果您需要将 playerClass
传递给 Level1ZombieClass,请在 Level1ZombieClass 中添加:
public Level1ZombieClass (int playerClass, int userhealth, <type> otherParam ...){
this.playerClass = playerClass;
this.userhealth = userhealth;
this.otherParam = otherParam;
...
}
并在创建关卡实例时添加需要传递的变量
Level1ZombieClass Level1ZombieClassObject = new Level1ZombieClass (playerClass,userhealth,otherParam,...);
您必须(并且应该)将这些变量放在类的方法(函数)之外。你的代码应该是这样的
public class Level1ZombieClass
{
int playerClass;
int userhealth;
<type> otherParam;
...
public Level1ZombieClass (int playerClass){
this.playerClass = playerClass;
this.userhealth = userhealth;
this.otherParam = otherParam;
...
}
public void ZombieLevel1Class()
{
Scanner in = new Scanner(System.in);
//Variables that are not user-dependent
int comphealth=100;
int hc;
int userChoice=1;
int critdamage;
//rest of code
关于java - 变量可能尚未初始化,使用 2 个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34867266/
这很可能是我的语法错误,因为我对在 C++ 中使用多个文件和结构(特别是将结构传递给函数)还很陌生。这是三个文件: 主要.cpp: #include #include #include #inc
我有 TypeScript NestJS 项目。 我需要验证传入的 DTO 到我的 API。它可以被描述为“创建项目”,其中我们有建筑类型(房屋、公寓、花园),并根据该类型我们需要定义: 房屋:楼层包
是否可以从可用于泛型参数的可能类型集中排除特定类型?如果是如何。 例如 Foo() : where T != bool 将意味着除了类型 bool 之外的任何类型。 编辑 为什么? 以下代码是我尝试强
我的 WebGL 体积光线转换应用程序即将完成。但是我发现了一个问题。我必须通过 2D 纹理模拟 3D 纹理。这不是问题。我正在用小切片创建一个巨大的纹理。巨大纹理的尺寸约为 4096x4096 像素
我正在处理的网页上显示了一个返回顶部按钮。当您向下滚动时,有时单击它时,它会跳到顶部,然后跳回您在页面上的位置,然后像预期的那样平滑滚动到顶部。请记住,它并不总是这样做。这只是一个滞后或故障问题还是我
我对此还很陌生,所以请耐心等待。 我有一个类,它具有三个属性:几个整数和一个用户定义对象的集合。 public class Response { public int num1 { get;
我正在制作一款平台游戏,让玩家每 30 毫秒跳跃一次,并向上添加少量的力。我想我应该使用多线程,因为我之前已经做过一些,而且看起来很简单。无论如何,我尝试了这个: public void jump()
是否可以从可能的类型集中排除特定类型,这些类型可以在泛型参数中使用?如果是这样的话。 例如 Foo() : where T != bool 表示除 bool 类型之外的任何类型。 编辑 为什么? 以下
我正在尝试在单个查询中实现内部和外部联接,我不确定我的做法是正确还是错误,因为我不太擅长查询。 就这样吧。 我有以下表格。 hrs_residentials hrs_residential_utili
关于 my website ,有一段代码可以向页面添加几个元素。这段代码不是我可以编辑的东西,而且我对它放置这些元素的位置不满意,因为它弄乱了我的一些布局。所以我想出了一个小的 jQuery 来将它们
一位客户希望我创建一个数据集,如下所示。我不知道这是否可能或合乎逻辑。 我有表parent: id name ------- ------- 1 parent1 2
这可能吗?google 好像没有这方面的资料.. 这样,如果用户在另一个网站上播放视频或歌曲,我的音量就会自动减小 最佳答案 不,这是不可能的。 如果可能的话,它必须是特定于浏览器的,但我不认为这种情
所以我正在尝试制作响应式页面。问题是为什么它归结为移动数据需要位于列表中。 我会用一些示例代码来解释 所以这可能是桌面上的输出 option1
当您将鼠标悬停在a 元素 上时,是否可以删除url? 这就是我的意思: 最佳答案 一种选择是使用一些 JavaScript。 删除 href=来自 的属性标签,取而代之的是 onclick=...
我已经考虑了几个小时,但我无法取得太大进展。它是这样的: You have an array of size n and q queries. Each query is of the form (l
我一直在尝试编写一个脚本来强化 android。我没有成功! 我正在通过模拟器运行一个 AVD,并且已经用我加载的 android shell 和 bash shell 试过了。正如您将在下面看到的那
Private Sub Workbook_Open() Dim WBname As String WBname = ThisWorkbook.name If Not InStr(WBname, "te
Spark 2.0.0-预览版 我们有一个应用程序使用了相当大的广播变量。我们在大型 EC2 实例上运行它,因此部署处于客户端模式。广播变量是一个巨大的 Map[String, Array[Strin
我正在尝试从此link中提取摘要。但是,我无法仅提取摘要的内容。到目前为止,这是我完成的工作: url <- "http://www.scielo.br/scielo.php?script=sci_a
我的主页中有一个iframe。 iframe页面中有一个modalpopup。因此,当显示modalpopup时,modalpopup的父级是iframe主体和主页父级主体。因此,覆盖层仅覆盖ifra
我是一名优秀的程序员,十分优秀!