日期:2014-05-20  浏览次数:20737 次

菜鸟问个弱弱的问题 这程序哪里出错了?
import java.io.*;
public class OrderSearch {
static int data,size;
static int array [] = {2,5,9,8,12,14,32,56,72,81};
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br;
String str;
size = array.length;
for(int i=0;i<array.length;i++){
System.out.print(array[i]);
}System.out.print("\n");
System.out.print("输入你要查找的数据:");
try {
br = new BufferedReader(new InputStreamReader(System.in));

str = br.readLine();
data = Integer.parseInt(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OrderSearch b = new OrderSearch();
int index = b.OrSearch(data);
System.out.print("查找的结果如下:"+"\n");
if(index == -1){
System.out.print("数据集合或序列中没有找到该数据:"+data+"\n");
}else{
System.out.print("在数据集合或序列中第"+index+"个位置:"+ data+ "\n");
}
int OrSearch(int key){
for(int i=0;i<size-1;i++){
if(array[i]==key)
return i+1;
}
return -1;
}
 
}

}


------解决方案--------------------
把int OrSearch(int key) {
for (int i = 0; i < size - 1; i++) {
if (array[i] == key)
return i + 1;
}
return -1;
}
移到倒数第2个括号外面,你写在main方法里面了。。。

Java code

import java.io.*;

public class OrderSearch {
    static int data, size;
    static int array[] = { 2, 5, 9, 8, 12, 14, 32, 56, 72, 81 };

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BufferedReader br;
        String str;
        size = array.length;
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.print("\n");
        System.out.print("输入你要查找的数据:");
        try {
            br = new BufferedReader(new InputStreamReader(System.in));

            str = br.readLine();
            data = Integer.parseInt(str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        OrderSearch b = new OrderSearch();
        int index = b.OrSearch(data);
        System.out.print("查找的结果如下:" + "\n");
        if (index == -1) {
            System.out.print("数据集合或序列中没有找到该数据:" + data + "\n");
        } else {
            System.out.print("在数据集合或序列中第" + index + "个位置:" + data + "\n");
        }

    }
    int OrSearch(int key) {
        for (int i = 0; i < size - 1; i++) {
            if (array[i] == key)
                return i + 1;
        }
        return -1;
    }
}

------解决方案--------------------
int OrSearch(int key)是个方法吧?方法不能定义在方法里面,你怎么把这个方法定义在main方法里了