Oleg Kniazev
Oleg Kniazev

Reputation: 25

Trying to work off and input by the user to change a value

I am creating a small game-type program, Firstly I'm asking the user to give his name, Secondly, I'm asking the user to choose one of the spaceships, Thirdly, I want the user to add 2 modification, to improve the stats... Here is the problem, I want to change some values of the spaceship. The problem is that I don't know how to determine the chosen spaceship and how to change its values. Example: The Heavy-type Armour is 90 right now​ if I add the Advanced Hull Armour modification, i want it to increase to 100

    public class Game {
    static void playerName() {
    System.out.println("What's your name?:");
    String name = Keyboard.readString();
    System.out.println("Okay,"+name+" it is!\n");   
    }


    static class StarShip {     
    String name;
    String armour;
    String attack;      
    String mobility;
    }

    public static void main (String args[]) {
    System.out.println("Hello Rookie! \n");
    playerName();

    System.out.println("Let's pick your first ship!\n");
    System.out.println("Choose one of these:");

    //insert ships here:
    StarShip ship1 = new StarShip();
    ship1.name = "Heavy-type";
    ship1.armour = "Armour=90";
    ship1.attack = "Firepower=50";
    ship1.mobility = "Mobility=40";


    StarShip ship2 = new StarShip();
    ship2.name = "Medium-type";
    ship2.armour = "Armour=60";
    ship2.attack = "Firepower=60";
    ship2.mobility = "Mobility=60";

    StarShip ship3 = new StarShip();
    ship3.name = "Light-type";
    ship3.armour = "Armour=20";
    ship3.attack = "Firepower=60";
    ship3.mobility = "Mobility=90";


    System.out.println("1  -"+ship1.name+"-      "+ship1.armour+"  "+ship1.attack+"  "+ship1.mobility);
    System.out.println("2  -"+ship2.name+"-  "+ship2.armour+"  "+ship2.attack+"  "+ship2.mobility);
    System.out.println("3  -"+ship3.name+"-      "+ship3.armour+"  "+ship3.attack+"  "+ship3.mobility);
    int chosen = Keyboard.readInt();

    switch (chosen) {
    case 1: 
        System.out.println("Heavy-type! Excellent choice! Great armour and guns! \n");
        break;
    case 2:
        System.out.println("Medium-type! An all-rounder, a mix of everything! \n");
        break;
    case 3:
        System.out.println("Light-type! Fast and mobile, but has little armour! \n");
        break;
    }


    System.out.println("Lets pimp out your ship! \n");

    String advancedHull = "1 - Advanced Hull Armour - Armour    +10";
    String betterAmmo = "2 - Better Ammo \t - Firepower +10";
    String booster = "3 - Booster \t\t - Mobility  +10";

    System.out.println(advancedHull+"\n"+betterAmmo+"\n"+booster);

}   
}

Upvotes: 0

Views: 29

Answers (1)

ktzr
ktzr

Reputation: 1645

Currently you the fields of the StarShip are all Strings, when you add two strings they are concatenated, eg "abc"+"def" = "abcdef" regardless of what they say. You want to use ints these are "java numbers" and add as you would expect eg 10 + 20 = 30

class StarShip {     
    String name;
    int armour;
    int attack;      
    int mobility;
}

...

StarShip ship1 = new StarShip();
ship1.name = "Heavy-type";
ship1.armour = 90;
ship1.attack = 50;
ship1.mobility = 40;

...

When the user selects the ship we want to save this chose into a variable, like so:

Starship chosenShip; // must be declared outside the switch statement

switch (shipChoice ) {
case 1: 
    System.out.println("Heavy-type! Excellent choice! Great armour and guns! \n");
    chosenShip = ship1;
    Break;
...

Assuming advancedHull is selected and you include logic similar to how ships are selected

if (modificationChoice = 1){
    chosenShip.armour = chosenShip.armour + 10;
} ...

You may want to declare a toSting() methods in StarShip like so

@Override
public String toString() {
    return name +" Armour=" + armour +", Firepower=" + attack +", Mobility=" + mobility;
}

This way you can call System.out.println(ship1.toString()) to print the ship info.

Example:

System.out.println(ship1.toString());
// outputs: Heavy-type Armour=90, Firepower=50, Mobility=40
ship1.armour += 10; // java shorthand for `ship1.armour + ship1.armour + 10`
System.out.println(ship1.toString());
// outputs: Heavy-type Armour=100, Firepower=50, Mobility=40

Upvotes: 1

Related Questions