gpt4 book ai didi

java - 尝试将字符串数组的索引值与 int 值进行比较

转载 作者:行者123 更新时间:2023-11-30 06:52:08 26 4
gpt4 key购买 nike

因此,我尝试将字符串数组的索引与预定值进行比较,以便它可以根据排名返回卡的类型。我知道下面的方法是将实际字符串与数字进行比较,我只是不知道如何将实际索引值与数字进行比较。

public String findFaceValue()
{
String faceValueArray[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
, "Ten", "Jack", "Queen", "King"};
for(int i = 0; i < faceValueArray.length; ++i)
{
if (faceValueArray[i] == rank)
return faceValueArray[i];
}
return "Not a valid card";

}

最佳答案

So I am trying to compare the index of the array of strings to a predetermined value so it can return the type of card it will be based on the rank

如果您只是想检查给定的索引是否可以为您获取有效的卡。如果是,则返回卡的名称(等级),否则返回“不是有效卡”。您可以执行以下操作:

public String findFaceValue(int index)
{
String faceValueArray[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};

if (index < 0 || index >= faceValueArray.length)
return "Not a valid card";
return faceValueArray[index];
}

Okay so rank is just any number between 1 and 13 so for example if it's 3 I want the program to return "Three".

要偏移索引,只需将其更改为:

if (index < 1 || index > faceValueArray.length)
return "Not a valid card";
return faceValueArray[index-1];

关于java - 尝试将字符串数组的索引值与 int 值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543663/

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