如何在字符串数组中使用整数?

huangapple go评论77阅读模式
英文:

How to use integers in string array?

问题

好的,以下是翻译好的内容:

早上好我该如何使这个代码像这样工作

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent int1 = getIntent();
    final String category = int1.getStringExtra("category");
    final int level = int1.getIntExtra("level", 0);

    if (level == 0) {
        final String[] a = new String[]{"heart", "spade"};
    }
    if (level == 1) {
        final String[] a = new String[]{"black", "blue"};
    }

    我想在我的编辑文本中使用 `a`,并在每个 `level` 中使用不同的值`a` 不起作用

    if (answer1.getText().toString().contains(a))

    如果我移除 `if (level == 0)`,`level` 的值会变成 `"heart", "spade"`。我不能使用这个因为我在两个或更多的单词中使用了 `.contains`。

    我已经尝试过 `final String[] stringvar = new String[]{Arrays.toString(a), Arrays.toString(b)};` + `.contains(stringvar[level])`,但效果不好

    所以我需要的是 `a` 在每个 `level` 中包含不同的值
    或者对于每个字符串数组只有一个值
    例如

    `final String[] a = new String[]{"asd", "qwe"} = 0`

    `final String[] b = new String[]{"dsa", "dsa"} = 1`

    谢谢如果我的问题难以理解请告诉我

请注意,代码部分仅进行了中文翻译,没有进行实际代码的执行或验证。如果您需要进一步的帮助,请随时提问。

英文:

Good day, how can I make this work like this?

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent int1 = getIntent();
final String category = int1.getStringExtra("category");
final int level = int1.getIntExtra("level", 0);
if (level == 0) {final String[] a = new String[]{"heart", "spade"};
if (level == 1) {final String[] a = new String[]{"black", "blue"};

I want to use a in my Edit text with different value in every level but a isnt working.

     if (answer1.getText().toString().contains(a))

If I remove if (level == 0) , the value of level goes to "heart", "spade". I cannot use this because I am using .contains in two or more words.

I already tried final String[] stringvar = new String[]{Arrays.toString(a), Arrays.toString(b)}; + .contains(stringvar[level]) but it's no good.

So what I need is a contains different value in every level.
or for every string array that only 1 value.
for example:

final String[] a = new String[]{"asd ", "qwe"} = 0

final String[] b = new String[]{"dsa ", "dsa} = 1

Thank you. Please tell me if my question is hard to understand.

答案1

得分: 0

所以根据我理解,您想要为每个对应的级别创建一个字符串数组。我建议您查看一下 Map 数据类型。您可以创建一个存储级别编号和要检查答案的单词数组的 Map<Integer, String[]>

一个示例是:

Map<Integer,String[]> map = new HashMap<>();
map.put(1, new String[]{"asd", "qwe"}); // 第1级
map.put(2, new String[]{"dsa", "dsa"}); // 第2级

然后在您的代码中,您可以使用 map.get(n) 来获取第 n 级的字符串数组,依此类推。

英文:

So from what I understand, You want an Array of Strings for each corresponding level. I recommend you check out the Map datatype. You create a Map<Integer, String[]> that stores your level number and array of words you want to check the answer for.

A sample is:

Map<Integer,String[]> map = new HashMap<>();
map.put(1, {"asd ", "qwe"}); // Level 1
map.put(2, {"dsa", "dsa"}); // Level 2

You can then use map.get(n) to get the String array for the nth level etc. in your code.

huangapple
  • 本文由 发表于 2020年5月5日 20:39:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定