- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定这个问题或类似的问题以前是否有人问过,但我搜索了又搜索,但一无所获。我刚在学校开始编程,但是老师给我们的项目,我可以很快完成,而且几乎有点太简单了,所以我开始看 Oracle 的 java 教程。经过大量的实验,我决定基于 MGSV TPP 制作一款基于文本的潜行游戏。无论如何,背景故事已经足够了。我的问题是我的代码中的开关没有执行。没什么。我一遍又一遍地检查我的代码,但我不知道出了什么问题。这是我第一次使用开关。以下是带有开关的代码片段:
public void shoot() {
Random rand = new Random();
String farshot = this.shoot[rand.nextInt(this.shoot.length)];
if(reload == 1) {
if(supressor > 1) {
if(dbme<=75) {
System.out.println("Great Job! He's out cold!");
if(dbme == Guard1)
Guard1 = 0;
else if(dbme == Guard2)
Guard2 = 0;
else if(dbme == Guard3)
Guard4 = 0;
else if(dbme == Guard4)
Guard4 = 0;
else if(dbme == Guard5)
Guard5 = 0;
else if(dbme == Guard6)
Guard6 = 0;
else if(dbme == Guard7)
Guard7 = 0;
else if(dbme == Guard8)
Guard8 = 0;
dbme = 0;
reload = 0;
supressor--;
}
} else { System.out.println("You're suppressor had worn out! You alerted the guards! But Great shot, you knocked him out!");
if(dbme == Guard1)
Guard1 = 0;
else if(dbme == Guard2)
Guard2 = 0;
else if(dbme == Guard3)
Guard4 = 0;
else if(dbme == Guard4)
Guard4 = 0;
else if(dbme == Guard5)
Guard5 = 0;
else if(dbme == Guard6)
Guard6 = 0;
else if(dbme == Guard7)
Guard7 = 0;
else if(dbme == Guard8)
Guard8 = 0;
dbme = 0;
reload = 0;
supressor--;
alert = 1;
actions();
}
if(dbme > 75)
System.out.println("He's too faraway. but...");
switch (farshot) {
case "headshot":
System.out.println("Wow! You're a great shot! He's out cold! He's now unmarked");
if(dbme == Guard1)
Guard1 = 0;
else if(dbme == Guard2)
Guard2 = 0;
else if(dbme == Guard3)
Guard4 = 0;
else if(dbme == Guard4)
Guard4 = 0;
else if(dbme == Guard5)
Guard5 = 0;
else if(dbme == Guard6)
Guard6 = 0;
else if(dbme == Guard7)
Guard7 = 0;
else if(dbme == Guard8)
Guard8 = 0;
dbme = 0;
break;
case "miss":
if(supressor != 0)
System.out.println("YOU MISSED! But Your shot was suppressed, so you're safe!");
else {
System.out.println("YOU MISSED! You've alerted everyone of your presence!!!!");
reload = 0;
combat();
}
break;
case "alert":
System.out.println("It wasn't a headshot, but he doesn't know anything yet. He's alerted, so becareful!");
break;
}
dbme = 0;
actions();
reload = 0;
} else { System.out.println("Reload your gun first!");
markedAction();
}
}
我不确定您是否还需要更多内容,但这是完整的代码:
package metalgear;
import java.util.Scanner;
import java.util.Random;
public class MetalGear {
//Arrays
String[] objects = {"Food, Water, Ammo, Suppressor, Mags"};
String[] shoot = {"miss, headshot, alert"};
//Player
int health = 150;
int ammo = 10;
int food = 5; //5 packets
int water = 2; //2 liters
int damage = 20;
int xp;
int reload;
int supressor = 10;
int mags = 1;
int dbme;
String recon = "";
//Enemy
int distanceFromEnemyGuard = 200;
int returnDamage;
int foodDrop = 30;
int waterDrop = 20;
int ammoDrop = 50;
int emags = 15;
int esupp = 10;
int alert = 0;
int Guard1 = -1;
int Guard2 = -1;
int Guard3 = -1;
int Guard4 = -1;
int Guard5 = -1;
int Guard6 = -1;
int Guard7 = -1;
int Guard8 = -1;
int locations = 0;
public void enter() {
java.util.Scanner keyboard = new java.util.Scanner(System.in);
keyboard.nextLine();
}
public static void main(String[] args) {
MetalGear a=new MetalGear();
a.intro();
a.Page1();
}
public void intro()
{
java.util.Scanner input = new java.util.Scanner(System.in);
String x;
System.out.println("Welcome to a Text-Based Mission of MGSV-The Phantom Pain");
System.out.print("Enter your name... ");
x = input.next();
System.out.println("");
System.out.print("Hello " + x + ". " + "I'm Ocelot, and I'll be breifing you on your mission today. [Press Enter]");
enter();
System.out.print("Your job is to succesfully infiltrate the Soviet outpost located ");
enter();
System.out.print("at the village of Di Wialo Kalai in Afghanistan.");
enter();
System.out.print("Currently, you have 150 health, you do 20 damage,");
enter();
System.out.print("Have 10 bullets in your gun, and 5 food packets,");
enter();
System.out.print("2 liters of water, and 1 magazine(10 bullets) in your ");
enter();
System.out.println("Backpack. Use your supplies wisely.");
System.out.println("");
}
public void Page1() {
System.out.println("The chopper has dropped you at the best recon point at the edge of Di Wialo Kalai");
actions();
}
//Action Methods
public void actions() {
Scanner in = new Scanner(System.in);
String y;
System.out.println("");
System.out.println("Now, You... (Type help for a list of commands)");
y = in.next();
if(y.equalsIgnoreCase("help"))
help();
else if (y.equalsIgnoreCase("Recon"))
recon();
else if (y.equalsIgnoreCase("Reload"))
reload();
else if (y.equalsIgnoreCase("Backpack"))
backpack();
else if (y.equalsIgnoreCase("iDroid"))
iDroid();
else if (y.equalsIgnoreCase("Mark"))
marking();
else {
System.out.println("You can't do that!");
System.out.println("");
actions();
}
}
public void backpack() {
Scanner in = new Scanner(System.in);
String x;
boolean n = true;
while(n) {
System.out.println("You have " + this.food + " packets");
System.out.println("You have " + this.water + " liters of water");
System.out.println("You have " + this.mags + " magazines");
System.out.println("You can 'eat', 'drink', or 'load mag', or exit");
x = in.next();
if(x.equalsIgnoreCase("eat")) {
if(this.food >= 1) {
this.food--;
this.health += 30;
System.out.println("");
System.out.println("You now have " + health + " health");}
else {
System.out.println(""); System.out.println("You don't have enough food"); }
}
else if(x.equalsIgnoreCase("drink")) {
this.damage +=5;
this.water--;
System.out.println("");
System.out.println("You will now do " + damage + " damage"); }
else if(x.equalsIgnoreCase("load mag")) {
this.ammo = 10;
this.mags--;
System.out.println(""); System.out.println("Your now has full ammo(20 shots)");
System.out.println("");
}
else if(x.equalsIgnoreCase("exit"))
actions();
else System.out.println("YOu can't do that!"); System.out.println("");
backpack(); }
}
public void iDroid() {
Scanner in = new Scanner(System.in);
System.out.println("You have " + health + " health left");
System.out.println("You still have " + supressor + " suppressed shots left.");
System.out.println("You can currently do " + damage + " damage");
System.out.println("And your gun's magazine currently has " + ammo + " ammo left" );
if(!"".equals(recon))
System.out.println(recon);
else
System.out.println("You haven't obtained any intel files yet");
System.out.println("");
if((Guard1 != 1) && (Guard2 != 1) && (Guard3 != 1) && (Guard4 != 1) && (Guard5 != 1) && (Guard6 != 1) && (Guard7 != 1) && (Guard8 != 1)) {
if(Guard1 == 0) System.out.println("Guard 1 was eliminated"); else System.out.println("Guard 1 is " + Guard1 + " meters away.");
if(Guard2 == 0) System.out.println("Guard 2 was eliminated"); else System.out.println("Guard 2 is " + Guard2 + " meters away.");
if(Guard3 == 0) System.out.println("Guard 3 was eliminated"); else System.out.println("Guard 3 is " + Guard3 + " meters away.");
if(Guard4 == 0) System.out.println("Guard 4 was eliminated"); else System.out.println("Guard 4 is " + Guard4 + " meters away.");
if(Guard5 == 0) System.out.println("Guard 5 was eliminated"); else System.out.println("Guard 5 is " + Guard5 + " meters away.");
if(Guard6 == 0) System.out.println("Guard 6 was eliminated"); else System.out.println("Guard 6 is " + Guard6 + " meters away.");
if(Guard7 == 0) System.out.println("(Guard 7 was eliminated"); else System.out.println("Guard 7 is " + Guard7 + " meters away.");
if(Guard8 == 0) System.out.println("Guard 8 was eliminated"); else System.out.println("Guard 8 is " + Guard8 + " meters away."); }
else System.out.println("You haven't done any recon yet!");
System.out.println("");
if(locations != 0)
marking();
else actions();
}
public void reload() {
if (this.ammo>1) {
if(this.reload==0) {
this.reload++;
this.ammo--;
System.out.println("Locked and Loaded!");
System.out.println("You now have " + ammo + " ammo left");
System.out.println("");
}
else if(this.reload==1) {
System.out.println("You're gun is already loaded!");
System.out.println(""); }
}
else { System.out.println("You don't have any ammo!");
System.out.println(""); }
if(locations == 2)
markedAction();
else actions();
}
public void help() {
System.out.println("Recon-Spots the number of enemies and each of their distances from you");
System.out.println("Reload- Relaod your weapon. YOu need to do so after EACH shot.");
System.out.println("Backpack- Look, equip, or use your supplies");
System.out.println("iDroid- Tells you your health, your reconed enemies, and intel you have obtained.");
System.out.println("Mark- Marks a Guard, allowing for interaction with that Guard");
System.out.println("");
actions();
}
public void recon() {
Scanner in = new Scanner(System.in);
Random rand = new Random();
if((Guard1 == -1) && (Guard2 == -1) && (Guard3 == -1) && (Guard4 == -1) && (Guard5 == -1) && (Guard6 == -1) && (Guard7 == -1) && (Guard8 == -1)) {
Guard1 = rand.nextInt(this.distanceFromEnemyGuard);
Guard2 = rand.nextInt(this.distanceFromEnemyGuard);
Guard3 = rand.nextInt(this.distanceFromEnemyGuard);
Guard4 = rand.nextInt(this.distanceFromEnemyGuard);
Guard5 = rand.nextInt(this.distanceFromEnemyGuard);
Guard6 = rand.nextInt(this.distanceFromEnemyGuard);
Guard7 = rand.nextInt(this.distanceFromEnemyGuard);
Guard8 = rand.nextInt(this.distanceFromEnemyGuard);
System.out.println("Guard 1 is " + Guard1 + " meters away.");
System.out.println("Guard 2 is " + Guard2 + " meters away.");
System.out.println("Guard 3 is " + Guard3 + " meters away.");
System.out.println("Guard 4 is " + Guard4 + " meters away.");
System.out.println("Guard 5 is " + Guard5 + " meters away.");
System.out.println("Guard 6 is " + Guard6 + " meters away.");
System.out.println("Guard 7 is " + Guard7 + " meters away.");
System.out.println("Guard 8 is " + Guard8 + " meters away.");
actions();
}
else System.out.println("You already reconed the guards! Check your iDroid for their locations!");
actions();
}
public void marking() {
locations = 1;
Scanner in = new Scanner(System.in);
String abs;
if ((Guard1 != -1) && (Guard2 != -1) && (Guard3 != -1) && (Guard4 != -1) && (Guard5 != -1) && (Guard6 != -1) && (Guard7 != -1) && (Guard8 != -1)) {
System.out.println("Which Guard would you like to mark?");
abs = in.next();
if(abs.equalsIgnoreCase("1"))
dbme = Guard1;
else if(abs.equalsIgnoreCase("2"))
dbme = Guard2;
else if(abs.equalsIgnoreCase("3"))
dbme = Guard2;
else if(abs.equalsIgnoreCase("4"))
dbme = Guard4;
else if(abs.equalsIgnoreCase("5"))
dbme = Guard5;
else if(abs.equalsIgnoreCase("6"))
dbme = Guard6;
else if(abs.equalsIgnoreCase("7"))
dbme = Guard7;
else if(abs.equalsIgnoreCase("8"))
dbme = Guard8;
else if(abs.equalsIgnoreCase("idroid")) {
iDroid();
}
else if(abs.equalsIgnoreCase("exit")) {
System.out.println("No one was marked");
locations = 0;
actions();
}
else { System.out.println("That's not a Guard!");
marking();
}
}
else { System.out.println("Do some Recon first! YOu can't mark guards without knowing their locations!");
locations = 0;
actions(); }
System.out.println("");
if(dbme != 0)
markedAction();
}
//----------------------------------------------------------------------------------------------------------
//Marked ACtions
public void markedAction() {
locations = 2;
Scanner in = new Scanner(System.in);
System.out.println("The Guard has been marked. (Type 'help' for another list of commands)");
String x;
x = in.next();
if(x.equalsIgnoreCase("shoot"))
shoot();
else if(x.equalsIgnoreCase("CQC"))
CQC();
else if(x.equalsIgnoreCase("sneak"))
sneak();
else if(x.equalsIgnoreCase("unmark")) {
System.out.println("The Guard has been unmarked");
dbme = 0;
System.out.println("Returning to Actions");
actions();
}
else if(x.equalsIgnoreCase("reload"))
reload();
else if(x.equalsIgnoreCase("help"))
help2();
else { System.out.println("You can't do that"); markedAction();
}
locations = 0;
}
public void shoot() {
Random rand = new Random();
String farshot = this.shoot[rand.nextInt(this.shoot.length)];
if(reload == 1) {
if(supressor > 1) {
if(dbme<=75) {
System.out.println("Great Job! He's out cold!");
if(dbme == Guard1)
Guard1 = 0;
else if(dbme == Guard2)
Guard2 = 0;
else if(dbme == Guard3)
Guard4 = 0;
else if(dbme == Guard4)
Guard4 = 0;
else if(dbme == Guard5)
Guard5 = 0;
else if(dbme == Guard6)
Guard6 = 0;
else if(dbme == Guard7)
Guard7 = 0;
else if(dbme == Guard8)
Guard8 = 0;
dbme = 0;
reload = 0;
supressor--;
}
} else { System.out.println("You're suppressor had worn out! You alerted the guards! But Great shot, you knocked him out!");
if(dbme == Guard1)
Guard1 = 0;
else if(dbme == Guard2)
Guard2 = 0;
else if(dbme == Guard3)
Guard4 = 0;
else if(dbme == Guard4)
Guard4 = 0;
else if(dbme == Guard5)
Guard5 = 0;
else if(dbme == Guard6)
Guard6 = 0;
else if(dbme == Guard7)
Guard7 = 0;
else if(dbme == Guard8)
Guard8 = 0;
dbme = 0;
reload = 0;
supressor--;
alert = 1;
actions();
}
if(dbme > 75)
System.out.println("He's too faraway. but...");
switch (farshot) {
case "headshot":
System.out.println("Wow! You're a great shot! He's out cold! He's now unmarked");
if(dbme == Guard1)
Guard1 = 0;
else if(dbme == Guard2)
Guard2 = 0;
else if(dbme == Guard3)
Guard4 = 0;
else if(dbme == Guard4)
Guard4 = 0;
else if(dbme == Guard5)
Guard5 = 0;
else if(dbme == Guard6)
Guard6 = 0;
else if(dbme == Guard7)
Guard7 = 0;
else if(dbme == Guard8)
Guard8 = 0;
dbme = 0;
break;
case "miss":
if(supressor != 0)
System.out.println("YOU MISSED! But Your shot was suppressed, so you're safe!");
else {
System.out.println("YOU MISSED! You've alerted everyone of your presence!!!!");
reload = 0;
combat();
}
break;
case "alert":
System.out.println("It wasn't a headshot, but he doesn't know anything yet. He's alerted, so becareful!");
break;
}
dbme = 0;
actions();
reload = 0;
} else { System.out.println("Reload your gun first!");
markedAction();
}
}
public void CQC() {
}
public void sneak() {
}
public void combat() {
}
public void help2() {
}
}
感谢您的所有回复!
最佳答案
在代码顶部,您声明字符串数组,但每个数组仅包含一个字符串:
String[] objects = {"Food, Water, Ammo, Suppressor, Mags"};
String[] shoot = {"miss, headshot, alert"};
因此,当您初始化 farshot
变量时
String farshot = this.shoot[rand.nextInt(this.shoot.length)];
它将始终包含值miss、headshot、alert
。这与您正在测试的任何案例都不匹配。
我假设您想要执行以下操作:
String[] objects = {"Food", "Water", "Ammo", "Suppressor", "Mags"};
String[] shoot = {"miss", "headshot", "alert"};
不知道它是否能解决您的所有问题,但这是一个开始。
关于java - 开关不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33470930/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!