Как заставить мой Java-оператор if работать? - PullRequest
0 голосов
/ 21 октября 2018

Я очень новичок в изучении 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();
    }
}

1 Ответ

0 голосов
/ 21 октября 2018

Есть несколько проблем с потоком вашей программы, но текущая проблема заключается в том, что вы пытаетесь использовать else в цикле while.Это невозможно или необходимо.

Цикл while будет продолжаться до тех пор, пока не будет выполнено определенное условие.Как только это происходит, цикл while заканчивается и выполняется следующая строка кода.

Итак, удалите else { из закрытия цикла while.Затем вы можете просто оценить результаты.

Вот исправленный код с парой комментариев, чтобы показать ГДЕ, чтобы удалить вещи:

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();
    }
}
...