A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ...!!! 中级黑马   /  2015-9-18 22:11  /  538 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

需求:
     1、键盘录入数组长度
     2、键盘录入数组的每个元素
     3、打印数组
     4、键盘录入查找数据
     5、返回查找数据的索引

评分

参与人数 1黑马币 +2 收起 理由
菜鸟飞 + 2 赞一个!

查看全部评分

4 个回复

倒序浏览
public class Hm_Test {

        public static void main(String[] args){
                Array_Test at=new Array_Test();
                at.method();
                at.method1();
                System.out.println("索引为"+at.method2());
        }
}
class Array_Test{
        private int a[]=null;
        private Scanner sc=new Scanner(System.in);
        public Array_Test(){
                int n;
                System.out.println("输入数组长度");
                n=sc.nextInt();
                a=new int[n];
        }
        public void method(){
                for(int i=0;i<a.length;i++){
                        System.out.println("输入第"+(i+1)+"个数");
                        a[i]=sc.nextInt();
                }
        }
        public void method1(){
                System.out.print("遍历数组[");
                for(int i=0;i<a.length;i++){
                        if(i!=a.length-1)
                                System.out.print(a[i]+",");
                        else
                        System.out.print(a[i]);
                }
                System.out.print("]");
        }
        public int method2(){
                int i;
                System.out.println("输入你要查找的数:");
                i=sc.nextInt();
                for(int j=0;j<a.length;j++)
                        if(i==a[j])
                                return j;
                return -1;
        }
}
回复 使用道具 举报 1 0
楼上是大神呀
回复 使用道具 举报
2楼很给力啊
回复 使用道具 举报
2楼正解
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马