当前位置: 代码迷 >> java >> 尝试进行修改并由用户输入以更改值
  详细解决方案

尝试进行修改并由用户输入以更改值

热度:82   发布时间:2023-07-31 11:32:45.0

我正在创建一个小型的游戏类型程序,首先,我要求用户提供他的名字,其次,我要求用户选择其中一个太空飞船,其次,我希望用户添加2个修改,以改进统计信息...这是问题所在,我想更改飞船的某些值。 问题是我不知道如何确定所选的太空船以及如何更改其值。 示例:如果我添加了高级船体修改,现在重型装甲现在是90,我希望将其增加到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);

}   
}

当前,StarShip的字段都是字符串,当您添加两个字符串时,它们会串联在一起,例如"abc"+"def" = "abcdef"无论它们说什么。 您要使用int这些是“ java数字”并按您的期望进行添加,例如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;

...

当用户选择船时,我们要将其保存到变量中,如下所示:

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;
...

假设选择了advancedHull ,并且您包含的逻辑类似于选择船只的方式

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

您可能想要像这样在StarShip声明toSting()方法

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

这样,您可以调用System.out.println(ship1.toString())来打印运送信息。

例:

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
  相关解决方案