基本的java输入整数

时间:2015-11-11 23:51:48

标签: java arrays search linear

所以我试图运行这个程序使用线性搜索,但需要一些有才华的程序员的帮助。我需要使用数组运行该程序。问题是程序不断重复循环“Enter#integer”和“Enter find to key”。我不知道还有什么要做,所以我转向你。以下是输出的示例。谢谢

  • 输入#Int:****
  • 输入整数键来查找:
  • 键出现在索引

  • 输入整数键来查找:
  • 整数键不会出现在给定的数组中。

import java.util.Scanner;

公共类LinearSearch {

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];

        int key = -1;
        for (int i = 0; i < array.length; i++) {
                System.out.println("Enter 10 integers: ");
                array[i] = input.nextInt(); 

                System.out.println("Enter key to find: "); 
                key = input.nextInt(); 
        }
}
        public static int findIt(int[] array, int key) {

            for (int i = 0; i < array.length; i++) {
                System.out.println("The integer key " + ( key ) + " appear at index = " + array[i]);
                {
            if (key == array[i]); 
                System.out.println("The integer key " + ( key ) + " does not appear in the given array");

            return i;       
}   
}
            return -1;

}

}

2 个答案:

答案 0 :(得分:-2)

你遇到了一堆错误,我根据你的工作方式更正了程序,并添加了哪些行有问题的注释以及原因。

import java.util.Scanner;

public class LinearSearch 
{

public static void main(String[] args) 
{
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];

        System.out.println("Enter 10 integers: "); **// <-- first mistake keep the println out of the for loop or else it will print this statement 10 times.**
        for (int i = 0; i < array.length; i++) 
        {
                array[i] = input.nextInt();     
        }
        **//I am assuming you only want to enter the key once, so keep it outside the for loop to ask once and not 10 times...**
        System.out.println("Enter key to find: "); 
        int key = input.nextInt();

        **//You never called the method in main... so call it.**
        findIt(array, key);
}
        public static int findIt(int[] array, int key) 
        {

            for (int i = 0; i < array.length; i++) 
            {
                **// No semi-colon after the if statement!! Also this is how the method should look.**
                if(key == array[i])
                {
                   System.out.println("The integer key " + key + " appears at index = " + i); **//Something to note here: you want to print i instead of array[i] as it is the index (array[i] is the actual value).**
                   return i;
                }
            }
            System.out.println("The integer key " + key + " does not appear in the given array");
            return -1;       
       }   
}

如果你不理解我改变了什么,希望有帮助,评论任何事情。

答案 1 :(得分:-2)

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] array = new int[10];

    int key = -1;
    for (int i = 0; i < array.length; i++) {
            System.out.println("Enter 10 integers: ");//this will print out 10 times, you have to move this code out of loop or replace it with System.out.println("Enter" + i + "integers: ");
            array[i] = input.nextInt(); 

            System.out.println("Enter key to find: "); // you have to move out this two lines code from loop because this will run 10 times -> wrong we just enter key only once.
            key = input.nextInt(); 
    }}

下一个方法的想法:你运行数组和数组中的现有元素键。 =&GT;打印或退货。

你应该尝试自己再写并发布。我会为你复习。