java - How to perferm different action depending on user input -


i'm trying make simple game (even without interface). supposed type action (attack or block), i'm not having best time trying understand how it. want this: if user types attack 1 course of action, is block another, else want go asking do.

import java.util.scanner; public class game {     public static void main(string[] args){          int hp = 10; //not used yet         int enemy_hp = 10;         string attack;         string block;     int enemy_action = (int) (math.random() * 1); //not used yet     int your_block_chance1 = (int) (math.random() * 1);//not used yet     scanner userinput = new scanner (system.in);     system.out.println("it turn, attack or try block");     string action = userinput.next();      system.out.println(action);      if (action.equals(attack)){         system.out.print("you attacked enemy , ");         int enemy_block_chance1 = (int) (math.random() * 3);         if (enemy_block_chance1 == 1) {             system.out.print("he blocked it");         }         else if (enemy_block_chance1 != 1){             enemy_hp = enemy_hp - 2;             system.out.print("managed hit, hp " +enemy_hp);         }     }     else if (action.equals(block)){      }     } } 

change:

if (action.equals(attack)) 

to:

if (action.equals("attack")) 

(and same block action check)

at present, string attack has not been set equal "attack" , it's not been initialized it's therefore empty string.

note: should surround code try/catch block implement exception handling (i.e. program doesn't crash when user enters integer example).


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -