我在解决如何使用for循环遍历数组时遇到问题。我的数组应检查用户是否输入了正方形或圆形的正确形状,如果找不到则返回UNKNOWN
。我在让其他部分工作时遇到了问题。
这就是我到目前为止所拥有的。我的for循环也必须从0迭代到验证数组中的最后一个元素。
//finds the shape based on user input
public static String findMyShape(String command)
{
//needs to have a for loop
//should return unknown if no values found
var myStringArray = {"circle", "square"};
var arrayLength = myStringArray.length;
for(var i=0; i<arrayLength; i++)
{
return(myStringArray[i]);
}
else
{
}
return "UNKNOWN";
}
答案 0 :(得分:1)
//finds the shape based on user input
public static String findMyShape(String command)
{
//needs to have a for loop
//should return unknown if no values found
var myStringArray = {"circle", "square"};
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++){
if (command == myStringArray[i])
return command;
return "UNKNOWN";
}