gpt4 book ai didi

java - 将字符串变量传递给 Java 方法不起作用,但硬编码字符串起作用?

转载 作者:行者123 更新时间:2023-12-02 05:11:04 25 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。我试图将一些表示用户不喜欢的字符串变量传递给预定义的 Java 方法,该方法通过将这些不喜欢的内容与作为字符串数组存储在 Recipe 对象数组中的关键成分进行比较来工作。

当我硬编码不喜欢的内容(例如“Beef”)时,该方法工作正常,但是当我使用 user1.getDislikes(0) 将不喜欢的内容分配给实例字符串变量 kw1 时,该方法无法正确执行 - 它返回将“牛肉”作为关键字的食谱,而本不应该这样做。

我知道字符串正在被正确传递和分配,因为我使用 Toast 在返回有效结果时显示 kw1。

我尝试在很多地方添加 toString() ,因为 IntelliJ 之前对它很挑剔,尽管声称它是多余的,但它在这里不起作用。

这是我遇到困难的部分:

if ((SetRecipes.recipes[index].searchkeywords2(kw1, kw2, kw3))) //Not working unless words (e.g. "Beef") are hardcoded for some reason. kw1 variable being assigned correctly, as shown by Toast.
{
temp[validRecipe] = index;

validRecipe++;
} //if

完整的代码可以在下面找到。非常感谢任何帮助!

public class SuggestResult extends Activity
{

String kw1, kw2, kw3;

static TextView [] recipeText = new TextView[8];

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.suggest_results);
User user1 = (User)getIntent().getSerializableExtra("user1");

kw1 = user1.getDislikes(0).toString();
kw2 = user1.getDislikes(1).toString();
kw3 = user1.getDislikes(2).toString();

/*
kw1 = "null";
kw2 = "null";
kw3 = "null";
*/

recipeText[0] = (TextView)findViewById(R.id.recipeSuggestText1);
recipeText[1] = (TextView)findViewById(R.id.recipeSuggestText2);
recipeText[2] = (TextView)findViewById(R.id.recipeSuggestText3);
recipeText[3] = (TextView)findViewById(R.id.recipeSuggestText4);
recipeText[4] = (TextView)findViewById(R.id.recipeSuggestText5);
recipeText[5] = (TextView)findViewById(R.id.recipeSuggestText6);

final int MAXRECIPES = 7;
final int MAXTEXTFIELDS = 6;
int[] temp = new int[MAXRECIPES];
int validRecipe = 0;

SetRecipes.setArray();

for (int index = 0; index < MAXRECIPES; index++)
{


if ((SetRecipes.recipes[index].searchkeywords2(kw1, kw2, kw3))) //Not working unless words (e.g. "Beef") are hardcoded for some reason. kw1 variable being assigned correctly, as shown by Toast.
{
temp[validRecipe] = index;

validRecipe++;
} //if
}

if (validRecipe == 0)
{
Context context = getApplicationContext();
CharSequence text = "No valid recipes found!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}

for (int index3 = 0; (index3 < validRecipe) && (index3 < MAXTEXTFIELDS); index3++)
{
recipeText[index3].setText((SetRecipes.recipes[temp[index3]].getName()).toString());

}


Context context = getApplicationContext();
CharSequence text2 = kw1;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text2, duration);
toast.show();


}

}

搜索关键字2方法:

public boolean searchkeywords2(String choice1,String choice2, String choice3)
{
int ingredientsPresent = 0;


for (int index = 0; index < keywords.length; index++)
{
if ((keywords[index] == choice1) || (keywords[index] == choice2) || (keywords[index] == choice3))
{
ingredientsPresent++;
}
}
if (ingredientsPresent == 0)
{
return true;
} else
{
return false;
}


}

最佳答案

关键字[索引] == choice1 ...

这就是问题所在。使用 .equals() 函数比较字符串,而不是 ==

关键字[index].equals(choice1)

关于java - 将字符串变量传递给 Java 方法不起作用,但硬编码字符串起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338614/

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