Pergunta

import java.util.Random;
import java.util. * ;

public class shooter {
    int myHP = 100;
    int enHP = 100;
    int myBAND = 5;
    int enBAND = 5;
    int turn = 10;

    public void main(String args[]) {
        setup();
        if (turn % 2 == 0) {
            myTurn();
        } else {
            enTurn();
        }
    }
    public void setup() {

    }
    public void myTurn() {
        System.out.println("You have " + myHP + " HP remaining and the enemy has " + enHP + " HP    remaining and you have " + myBAND + " bandages remaining");
        System.out.println("Your turn. What would you like to do?");
        System.out.println("You can aim for the head(1), the arms(2), the torso(3), or the legs(4). 5 for bandage.");
        Scanner scanner = new Scanner(System. in );
        int i = scanner.nextInt();
        turn++;

        if (i == 1) { //Head
            Random diceRoller = new Random();
            int roll = diceRoller.nextInt(6) + 1;
            if (roll == 6) {
                System.out.println("Shot at head, hit head.");
                enHP -= 70;
                if (enHP < 1) {
                    System.out.println("You Win!");
                } else {
                    System.out.println("Missed.");
                }
                //set turn to enemy 1?}
            } else if (i == 2) { //Arms
                Random diceRoller2 = new Random();
                int roll2 = diceRoller2.nextInt(6) + 1;
                if (roll2 == 6 || roll2 == 5) {
                    System.out.println("Shot at arms, hit arms.");
                    enHP -= 40;
                    if (enHP < 1) {
                        System.out.println("You Win!");
                    } else {
                        System.out.println("Missed.");
                    }
                    //set turn to enemy 1?}
                } else if (i == 3) { //torso
                    Random diceRoller3 = new Random();
                    int roll3 = diceRoller3.nextInt(6) + 1;
                    if (roll3 == 6 || roll3 == 5 || roll3 == 4) {
                        System.out.println("Shot at torso, hit torso.");
                    }
                    //set turn to enemy 1?}
                } else if (i == 4) { //Legs
                    Random diceRoller4 = new Random();
                    int roll4 = diceRoller4.nextInt(6) + 1;
                    if (roll4 == 6 || roll4 == 5) {
                        System.out.println("Shot at legs, hit legs.");
                    } else {
                        System.out.println("Missed.");
                    }
                } else if (i == 5) { // bandages here
                    System.out.println("You use a bandage.");
                    myBAND--;
                }
            }
        }
    }



    public void enTurn() //Enemy turn loop
    {
        System.out.println("Enemy's turn.");
    }
}

That's my code, and compiles fine. But at the beginning of when i try to run it it returns me this:

java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
Foi útil?

Solução

The method

public void main(String args[]) 

should be static

public static void main(String args[]) 

Also see Confusing Error Message when main Method not static

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top