为什么不能';方法查找包含的输入数组java
方法:为什么不能';方法查找包含的输入数组java,java,Java,方法: public void findInfo(String name) { for (int index = 0; index < names.size(); index++) { if ( name.equalsIgnoreCase( (String) names.get(index)) || name.contains(names.get(index)) ) { System.out.println("Customer : ");
public void findInfo(String name) {
for (int index = 0; index < names.size(); index++) {
if ( name.equalsIgnoreCase( (String) names.get(index)) || name.contains(names.get(index)) ) {
System.out.println("Customer : ");
System.out.println(name + " " + coins.get(index) + " cents");
System.out.println("Change : ");
this.calChange(coins.get(index));
break;
}
System.out.println("Not found");
break;
}//end of for loop
}//end of findInfo
正确的输出应为:
Select a menu item(1 to 5):
1
Find record of name:
peter
Customer :
peter has 20 cents
Change :
No. of 20 Cents: 1
我怎样才能得到这个结果/ findInfo()中的for循环将仅在index=0时执行,因为if外部有中断
以下几点应该有效-
int index;
for (index = 0; index < names.size(); index++)
{
if ( name.equalsIgnoreCase( (String) names.get(index)) || name.contains(names.get(index)) )
{
System.out.println("Customer : ");
System.out.println(name + " " + coins.get(index) + " cents");
System.out.println("Change : ");
this.calChange(coins.get(index));
break;
}
}//end of for loop
if(index == names.size())
System.out.println("Not found");
}//end of findInfo
int索引;
对于(索引=0;索引
findInfo()中的for循环将仅在index=0时执行,因为if外部中断
以下几点应该有效-
int index;
for (index = 0; index < names.size(); index++)
{
if ( name.equalsIgnoreCase( (String) names.get(index)) || name.contains(names.get(index)) )
{
System.out.println("Customer : ");
System.out.println(name + " " + coins.get(index) + " cents");
System.out.println("Change : ");
this.calChange(coins.get(index));
break;
}
}//end of for loop
if(index == names.size())
System.out.println("Not found");
}//end of findInfo
int索引;
对于(索引=0;索引问题在于这句话中断代码>,将其移出循环
让我们了解一下您做错了什么,您犯了一个逻辑错误
。当您循环运行index=0
时,检查名称。如果name位于index=0
,那么它就很好了
但如果name不在index=0
,if
语句将变为false,程序将直接进入下一个语句。这部分代码告诉程序要中断
因此,for
循环只对列表中的第一个条目执行。应使用此函数定义:
public void findInfo(字符串名称){
对于(int index=0;index问题在于这句话中断代码>,将其移出循环
让我们了解一下您做错了什么,您犯了一个逻辑错误
。当您循环运行index=0
时,检查名称。如果name位于index=0
,那么它就很好了
但如果name不在index=0
,if
语句将变为false,程序将直接进入下一个语句。这部分代码告诉程序要中断
因此,for
循环只对列表中的第一个条目执行。应使用此函数定义:
public void findInfo(字符串名称){
对于(int index=0;index
删除“break”语句。这使执行在无法满足“if(name.equalsIgnoreCase((String)names.get(index))| | name.contains(names.get(index))”的时间内脱离for循环”
就这点而言,如果您在代码中把名字命名为“Carter”,它将运行,因为这是第一个元素,而不是其他元素。
下面是删除break语句后的代码。
此外,此“未找到”将在每次迭代中持续打印。所以,做点什么吧
公共void findInfo(字符串名称){
for(int index=0;index
删除“break”语句。这使执行在无法满足“if(name.equalsIgnoreCase((String)names.get(index))| | name.contains(names.get(index))”的时间内脱离for循环”
就这点而言,如果您在代码中把名字命名为“Carter”,它将运行,因为这是第一个元素,而不是其他元素。
下面是删除break语句后的代码。
此外,此“未找到”将在每次迭代中持续打印。所以,做点什么吧
公共void findInfo(字符串名称){
for(int index=0;index
Wh
int index;
for (index = 0; index < names.size(); index++)
{
if ( name.equalsIgnoreCase( (String) names.get(index)) || name.contains(names.get(index)) )
{
System.out.println("Customer : ");
System.out.println(name + " " + coins.get(index) + " cents");
System.out.println("Change : ");
this.calChange(coins.get(index));
break;
}
}//end of for loop
if(index == names.size())
System.out.println("Not found");
}//end of findInfo
for (int index = 0; index < names.size(); index++) {
if ( name.equalsIgnoreCase( (String) names.get(index)) || name.contains(names.get(index)) )
{
System.out.println("Customer : ");
System.out.println(name + " " + coins.get(index) + " cents");
System.out.println("Change : ");
this.calChange(coins.get(index));
}
System.out.println("Not found");
}//end of for loop
}//end of findInfo