首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让我的Java 'if‘语句工作?

如何让我的Java 'if‘语句工作?
EN

Stack Overflow用户
提问于 2018-10-22 01:09:15
回答 1查看 66关注 0票数 1

我是学习Java的新手,目前我正在开发一个程序,它可以让我根据我分配给我们的简单统计数据和一个随机数来与计算机对抗,这个数字就像掷骰子一样。我认识到我的代码可能还有许多其他问题,但我试图解决的主要问题是第84行的“语法错误,删除这些令牌”和第77行的“语法错误,插入"}”以完成语句“。

我看不出有什么问题。我做错了什么?这两个问题都列在我的代码底部的注释中,紧挨着它们对应的行。

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-22 01:15:36

您的程序流程中有一些问题,但当前的问题是您试图在while循环中使用else。这是不可能的,也不是必要的。

while循环将继续,直到满足定义的条件。一旦发生这种情况,while循环就会结束,并执行下一行代码。

因此,从while循环的结尾处删除else {。然后,您可以只评估结果。

以下是更正后的代码,以及一些注释,以显示删除内容的位置:

代码语言:javascript
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52917837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档