使用用户输入来选择要编辑的变量

时间:2019-02-14 19:35:01

标签: java

我正在尝试让用户输入选择他们想要编辑的变量。假设我有10个变量,我希望用户能够输入“行1”,并提示他们通过另一个输入来更改该变量的值。我会发布代码,但是我没有任何相关的帖子,因为我什至不知道如何开始。

编辑:我不想有这样的东西:

if (input == "row 1") {     
    r1 = scan.nextInt();
}

因为我要处理很多变量,并且不想只对一个变量进行声明。

1 个答案:

答案 0 :(得分:2)

几分钟后完成。只是为了向您展示一种模式。

<div class="container">
  <div class="first child">
    This content can grow and be as wide as it wants
  </div>
  <div class="second child">
    This content will also be any size it wants, but I * want it to wrap at the asterisk in this sentence, which is where the first child above would naturally end. This will be its own flexbox container holding several buttons that should wrap onto new rows.
  </div>
</div>

用法基本上是:

public class TestClass {
    private final List<String> list = new ArrayList<>(16);
    private final Map<String, RowHolder> holderMap = new HashMap<>(16);

    {
        list.add("LoL 1");
        list.add("LoL 2");
        list.add("LoL 3");
        list.add("LoL 4");
        list.add("LoL 5");

        holderMap.put("Row 1", RowHolder.of(v -> { list.set(0, v); }, s -> list.get(0)));
        holderMap.put("Row 2", RowHolder.of(v -> { list.set(1, v); }, s -> list.get(1)));
        holderMap.put("Row 3", RowHolder.of(v -> { list.set(2, v); }, s -> list.get(2)));
        holderMap.put("Row 4", RowHolder.of(v -> { list.set(3, v); }, s -> list.get(3)));
        holderMap.put("Row 5", RowHolder.of(v -> { list.set(4, v); }, s -> list.get(4)));
    }

    public String getRow(final String row) {
        return holderMap.get(row).function.apply(row);
    }

    public void setRow(
            final String row,
            final String value) {
        holderMap.get(row).consumer.accept(value);
    }

    static class RowHolder {
        final Consumer<String> consumer;
        final Function<String, String> function;

        RowHolder(
                final Consumer<String> consumer,
                final Function<String, String> function) {
            this.consumer = consumer;
            this.function = function;
        }

        static RowHolder of(
                final Consumer<String> consumer,
                final Function<String, String> function) {
            return new RowHolder(consumer, function);
        }
    }
}