- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 Java 学习还很陌生,目前我正在开发一个程序,该程序可以让我根据分配给我们的简单统计数据和一个充当骰子功能的随机数与计算机进行战斗。我认识到我的代码可能存在许多其他问题,但我试图解决的主要问题是第 84 行的“标记上的语法错误,删除这些标记”和第 77 行的“语法错误,插入“}”以完成语句”。
我不明白问题出在哪里。我究竟做错了什么?这两个问题都列在我的代码底部附近相应行旁边的注释中。
import java.util.Scanner;
import java.util.Random;
public class Fight {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name");
String you = keyboard.next();
int youWounds = 1;
int youTough = 4;
int youAttack = 1;
int youWS = 4;
int youAS = 3;
String Comp = "Bad Guy";
int compWounds = 1;
int compTough = 4;
int compAttack = 1;
int compWS = 4;
int compAS = 3;
System.out.println(you + ", do you want to FIGHT?!?!?");
System.out.println("Yes / No?");
String inputString = keyboard.next();
if (inputString.equalsIgnoreCase("Yes")) {
System.out.println("FIGHT!!!!");
while (youWounds > 0 && compWounds > 0) {
int youRan = new Random().nextInt(6)+1; // this is where you roll to hit
System.out.println(you + " rolls " + youRan +" to hit");
if (youRan >= 7-youWS) { // this is the logic for roll to hit
System.out.println(you +" hit");
int youRanTW = new Random().nextInt(6)+1; // this is where you check to see if your hit wounds
System.out.println(you + " rolls " + youRanTW +" to wound");
if (youRanTW > compTough) { // this is the logic for roll to wound
System.out.println(you+" wounds"+Comp);
compWounds = compWounds - 1; // this is where comp loses a wound
if (compWounds <= 0) { // this is the logic for wound loss
System.out.println(Comp+" dies!!!");
} else {
System.out.println("But, "+Comp+" fights on!");
}
} else {
System.out.println(you=" does not wound");
}
} else {
System.out.println(you +" misses");
}
int compRan = new Random().nextInt(6)+1;
System.out.println(Comp+" rolls " + compRan + " to hit");
if (compRan >= 7-compWS) { // this is the logic for roll to hit
System.out.println(Comp +" hit");
int compRanTW = new Random().nextInt(6)+1; // this is where you check to see if your hit wounds
System.out.println(Comp + " rolls " + compRanTW +" to wound");
if (compRanTW > youTough) { // this is the logic for roll to wound
System.out.println(Comp+" wounds"+you);
youWounds = youWounds - 1; // this is where you loses a wound
if (youWounds <= 0) { // this is the logic for wound loss
System.out.println(you+" dies!!!");
} else {
System.out.println("But, "+you+" fights on!");
}
} else {
System.out.println(Comp=" does not wound");
}
} else {
System.out.println(Comp +" misses");
}
} else { // this is wher I get "Syntax error, insert "}" to complete Statement". The "}" is underlined in red on my screen
if (youWounds <=0){
System.out.println(Comp+" WINS!!!!");
} else {
System.out.println(you+" WINS!!!!");
}
}
} else { // this is where i get "Syntax error on tokens, delete these tokens". it wants me to delete "} else".
System.out.println(you + " you are a looser!!!!!!!!");
}
keyboard.close();
}
}
最佳答案
您的程序流程存在一些问题,但当前的问题是您尝试在 while
循环上使用 else
。这是不可能或没有必要的。
while
循环将继续,直到满足定义的条件。一旦发生这种情况,while
循环就会结束并执行下一行代码。
因此,从 while
循环的结束中删除 else {
。然后您就可以评估结果。
这是更正后的代码,其中有几条注释来显示在哪里删除内容:
import java.util.Random;
import java.util.Scanner;
public class Fight {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name");
String you = keyboard.next();
int youWounds = 1;
int youTough = 4;
int youAttack = 1;
int youWS = 4;
int youAS = 3;
String Comp = "Bad Guy";
int compWounds = 1;
int compTough = 4;
int compAttack = 1;
int compWS = 4;
int compAS = 3;
System.out.println(you + ", do you want to FIGHT?!?!?");
System.out.println("Yes / No?");
String inputString = keyboard.next();
if (inputString.equalsIgnoreCase("Yes")) {
System.out.println("FIGHT!!!!");
while (youWounds > 0 && compWounds > 0) {
int youRan = new Random().nextInt(6) + 1; // this is where you roll to hit
System.out.println(you + " rolls " + youRan + " to hit");
if (youRan >= 7 - youWS) { // this is the logic for roll to hit
System.out.println(you + " hit");
int youRanTW = new Random().nextInt(6) + 1; // this is where you check to see if your hit wounds
System.out.println(you + " rolls " + youRanTW + " to wound");
if (youRanTW > compTough) { // this is the logic for roll to wound
System.out.println(you + " wounds" + Comp);
compWounds = compWounds - 1; // this is where comp loses a wound
if (compWounds <= 0) { // this is the logic for wound loss
System.out.println(Comp + " dies!!!");
} else {
System.out.println("But, " + Comp + " fights on!");
}
} else {
System.out.println(you = " does not wound");
}
} else {
System.out.println(you + " misses");
}
int compRan = new Random().nextInt(6) + 1;
System.out.println(Comp + " rolls " + compRan + " to hit");
if (compRan >= 7 - compWS) { // this is the logic for roll to hit
System.out.println(Comp + " hit");
int compRanTW = new Random().nextInt(6) + 1; // this is where you check to see if your hit wounds
System.out.println(Comp + " rolls " + compRanTW + " to wound");
if (compRanTW > youTough) { // this is the logic for roll to wound
System.out.println(Comp + " wounds" + you);
youWounds = youWounds - 1; // this is where you loses a wound
if (youWounds <= 0) { // this is the logic for wound loss
System.out.println(you + " dies!!!");
} else {
System.out.println("But, " + you + " fights on!");
}
} else {
System.out.println(Comp = " does not wound");
}
} else {
System.out.println(Comp + " misses");
}
} // REMOVE THE ELSE AND BRACKET
if (youWounds <= 0) {
System.out.println(Comp + " WINS!!!!");
} else {
System.out.println(you + " WINS!!!!");
}
// REMOVE THIS BRACKET
} else { // this is where i get "Syntax error on tokens, delete these tokens". it wants me to delete "} else".
System.out.println(you + " you are a looser!!!!!!!!");
}
keyboard.close();
}
}
关于java - 如何让我的 Java 'if' 语句起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917837/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!