java - Changing variables using user input -


i have write code class. these requirements:

one class should controller main method.

the other class should:

  • do action
  • pass in variables
  • use @ least 1 if statement
  • have private methods
  • have getters , setters.

i have done of this. ran problem want user type 'go forward' , stamina decreased 5 , have message appear in sonsole saying, "you lose 5 stamina." , i'd user able type 'rest' , stamina increase 5 (capping @ 100, , console say, "stamina increased #."

i don't know how increase/decrease stamina though. whenever try something, error.

if there'es have don't need, tell me , why should remove it. if there that's public , doesn't need be, tell me why shouldn't be. teacher says later on, there 5 point deduction every time public doesn't need be.

here's code:

controller class:

import java.io.*; import java.util.scanner;  public class controller {   public static void main(string[] args){      system.out.println("type name here:");     scanner scan = new scanner(system.in);     string input = scan.nextline();              player playerone = new player(input);           playerone.setstrength(78);           playerone.sethealth(99);           playerone.setstamina(67);           playerone.printplayer();            system.out.println("type 'go forward' move forward");           scanner mscan = new scanner(system.in);           //mscan = movement scan              string minput = scan.nextline();                 if(minput.equals("go forward"))                     //minput = movement input                     system.out.println("you lose 5 stamina.");                     //getting error here : 'stamina cannot resolved variable'                     system.out.println("stamina: " + stamina);                 if(minput.equals("rest"))                    //getting error here : 'stamina cannot resolved variable'                     system.out.prinln("stamina has been increased " + stamina);                      scan.close();                       } } 

and player class:

import java.util.scanner;  public class player{          public string name;          private string input;         private double health;           public double strength;          public double stamina;          //player name          public player (string input){           name = input;        }         //player health          public void sethealth(double playhp){              health = playhp;        }        //player strength        public void setstrength(double playstrn){           strength = playstrn;        }        //player stamina        public void setstamina(double playstam){           stamina = playstam;        }         public void printplayer(){           system.out.println("name  : " + name );           system.out.println("health :" + health);           system.out.println("strength :" + strength);           system.out.println("stamina :" + stamina);              }         private void reducestamina() {            int stamina;           }     public int getstamina() {          return 0;     } }            system.out.println("type 'go forward' move forward");           scanner mscan = new scanner(system.in);           //mscan = movement scan              string minput = scan.nextline();                 if(minput.equals("go forward"))                     //minput = movement input                     system.out.println("you lose 5 stamina.");                     //getting error here : 'stamina cannot resolved variable'                     system.out.println("stamina: " + stamina);                 if(minput.equals("rest"))                    //getting error here : 'stamina cannot resolved variable'                     system.out.prinln("stamina has been increased " + stamina);                      scan.close();                       } } 

to use variable in java, have declare first. example use integer, have declare it

int mynumber = 1; //declare variable named mynumber , give value 1 

you missed on how declare variable, make 2 mistake see in code 1. reusable: declared scanner before, can use again, dont have declare 1 more scanner named mscan 2. declare variable: said before, if want use variable, have declare first. in whole code, did not declare stamina variable can not use it. that's reason why got error

stamina cannot resolved variable.

if want decrease stamina of player, should current stamina, decrease , set example:

double currentstamina = player.getstamina(); currentstamina = currentstamina - 5; // (or currentstamina-=5) decrease current stamina 5 player.setstamina(currentstamina); // set current stamina player 

next continue code. property of object should not declared public (your fields 'name', 'strength', 'stamina'). should private , can get/set value of them through get/set methods. use stamina variable not referenced (i said did not declare before). did mean stamina field of class player? if want use field of object, have use this: instead of

system.out.println("current stamina " + stamina); 

change to

system.out.println("current stamina " + player.stamina); 

but dont call that, should call:

system.out.println("current stamina " + player.getstamina()); // don't directly access field of object, please call get/set instead 

next: if , else if open , close brankets in code if statements. if dont have open , close brankets after statement, execute 1 next line action previous condition. , should learn use else keyword.

after whole things, code should be:

string minput = scan.nextline(); double currentstamina = player.getstamina(); if(minput.equals("go forward")) {      currentstamina -= 5;      system.out.println("you lose 5 stamina.");      system.out.println("stamina: " + currentstamina); } else if(minput.equals("rest"))    {      currentstamina += 5;      system.out.prinln("stamina has been increased " + currentstamina ); } player.setstamina(currentstamina); // update stamina value player scan.close();  

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -