gpt4 book ai didi

java - 从 hashmap 打印一个值到屏幕 - 一次打印多个值?

转载 作者:行者123 更新时间:2023-11-29 23:00:04 26 4
gpt4 key购买 nike

我有一个带有整数键和附加字符串的 HashMap 。用户可以选择多个值,我希望所有值都显示在屏幕上。请看下面的代码:

  Integer[] twistedThinking = {allOrNothing, blamingOthers, catastrophizing, emotionalReasoning, fortuneTelling,
labelling, magnifying, mindReading, minimising, overGeneralisation, selfBlaming, shouldStatements};

for(Integer key: twistedThinking){
key=twistedThought;
}

Map<Integer, String> twistedThoughtsMap = new HashMap<>();
twistedThoughtsMap.put(allOrNothing, "All or Nothing Thinking");
twistedThoughtsMap.put(blamingOthers, "Blaming Others");
twistedThoughtsMap.put(catastrophizing, "Catastrophizing");
twistedThoughtsMap.put(emotionalReasoning, "Emotional Reasoning");
twistedThoughtsMap.put(fortuneTelling, "Fortune Telling");
twistedThoughtsMap.put(labelling, "Labelling");
twistedThoughtsMap.put(magnifying, "Magnifying the Negative");
twistedThoughtsMap.put(mindReading, "Mind Reading");
twistedThoughtsMap.put(minimising, "Minimising the Positive");
twistedThoughtsMap.put(overGeneralisation, "Over Generalisation");
twistedThoughtsMap.put(selfBlaming, "Self-Blaming");
twistedThoughtsMap.put(shouldStatements, "Should Statements");

// Enhanced for loop searches the HashMap for any keys and sets the appropriate background.
for (Integer key : twistedThinking) {
if (twistedThought == key) {
DistortionLogDetails.setText(twistedThoughtsMap.get(key));

}

}

目前这段代码只会将列表中的最后一个值打印到屏幕上。因此,如果用户最后选择的值是“SelfBlaming”,则只会打印那个值,而忽略所有其他先前的值。有解决这个问题的简单方法吗?

编辑:

1) 只是为了澄清用户的信息来自 mysql 数据库。

2) 变量 allOrNothing、blamingOthers 等的数值为 1、2 等,直到 12。

所以从 mySQL 服务器返回的数据是这样的:

     "allOrNothing": null,
"blamingOthers": null,
"catastrophizing": null,
"emotionalReasoning": null,
"fortuneTelling": "5",
"labelling": null,
"magnifyingTheNegative": "7",
"mindReading": "8",
"minimisingThePositive": "9",
"overGeneralisation": null,
"selfBlaming": null,
"shouldStatements": null

在此示例中,用户有四个选择,因此,算命、放大负数、读心术和最小化正数都应显示在 SetText 中。

最佳答案

您可以考虑使用 Java“枚举”:

示例代码:

package com.example.cbt;

public class CBT {

public enum TwistedThinking {
allOrNothing, blamingOthers, catastrophizing, emotionalReasoning, fortuneTelling,
labelling, magnifying, mindReading, minimising, overGeneralisation, selfBlaming,
shouldStatements
};


public static void main(String[] args) {
for (TwistedThinking thought : TwistedThinking.values()) {
System.out.println("Thought(" + thought.ordinal() + ")=" + thought.name());
}
}

}

示例输出:

Thought(0)=allOrNothing
Thought(1)=blamingOthers
Thought(2)=catastrophizing
Thought(3)=emotionalReasoning
Thought(4)=fortuneTelling
Thought(5)=labelling
Thought(6)=magnifying
Thought(7)=mindReading
Thought(8)=minimising
Thought(9)=overGeneralisation
Thought(10)=selfBlaming
Thought(11)=shouldStatements

关于java - 从 hashmap 打印一个值到屏幕 - 一次打印多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com