Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用for循环返回要在Java中查找的整数位置的索引号?_Java_Arrays_Numbers - Fatal编程技术网

如何使用for循环返回要在Java中查找的整数位置的索引号?

如何使用for循环返回要在Java中查找的整数位置的索引号?,java,arrays,numbers,Java,Arrays,Numbers,如何使用for循环返回要查找的整数位置的索引号?例如,在上面的数组中,如果要“查找”的数字是9,则程序应以用户友好的短语返回索引号“5” import javax.swing.JOptionPane; public class ComparingStrings { public static void main( String[] args ) { int [] array = {-9, 2, 3, 4, 7, 9, 10, 23, 45, 67}; in

如何使用for循环返回要查找的整数位置的索引号?例如,在上面的数组中,如果要“查找”的数字是9,则程序应以用户友好的短语返回索引号“5”

import javax.swing.JOptionPane;
public class ComparingStrings
{
   public static void main( String[] args )
   {
       int [] array = {-9, 2, 3, 4, 7, 9, 10, 23, 45, 67};
       int index = -1;

       for(int i = 0; i < array.length; i++)
       {
            System.out.print(array[i] + " ");            
       }
       System.out.print( "\n" );
       for(int i = array.length - 1; i > 0; i--)
       {
           System.out.print(array[i] + " ");          
       }
       System.out.print( "\n" );

       String statement = JOptionPane.showInputDialog( "Type the integer which you want to find the location of" );
       int interger = Integer.parseInt(statement);
       System.out.println( "Integer:" + interger );
       for(int i = array.length - 1; i >= 0; i--)
       {
           if( statement .equals (array[i]))
           {
               index = i;
               System.out.println( "Index of Integer is: " + i);
           }
           else
           {
               System.out.println( "Index of Interger is: -1" );
           }
       }
    }//end method main
}//end class ComparingStrings
import javax.swing.JOptionPane;
公共类比较字符串
{
公共静态void main(字符串[]args)
{
int[]数组={-9,2,3,4,7,9,10,23,45,67};
int指数=-1;
for(int i=0;i0;i--)
{
System.out.print(数组[i]+“”);
}
系统输出打印(“\n”);
String语句=JOptionPane.showInputDialog(“键入要查找其位置的整数”);
int-Integer=Integer.parseInt(语句);
System.out.println(“整数:“+Integer”);
对于(int i=array.length-1;i>=0;i--)
{
if(语句.equals(数组[i]))
{
指数=i;
System.out.println(“整数的索引为:“+i”);
}
其他的
{
System.out.println(“积分指数为:-1”);
}
}
}//结束方法主
}//端类比较字符串

您需要使用len-1初始化数组,即
int i=array.length-1

   for(int i = array.length-1; i >=0; i--)
   {
        if(array[i]==noToFind)
        {
              break;//come out of loop when u find the exact match
        }
   }
   System.out.println("We found no at index = " + i);

为什么会有
i--
for的第二个
循环将进入负索引,这对于数组来说总是超出范围。错误在这一行
for(int i=0;i
。它应该是
for(int i=0;i
您需要将一个输入与数组的元素进行比较,然后打印
i
本身(如果它们相等)。我看不到有人试图执行您在这段代码中描述的操作。你说你想找到一个数字并打印它的索引,但你只是打印了两次数组条目(第二个循环中的一个错误变成了负索引)。这对第一个数组有效,但第二个数组必须以相反的顺序遍历。它必须是一个特定的输入整数。还有,你的意思是i>0吗?是的,应该是i>=0。什么意思?