java中输出一个数组的数字_用户在数组中输入10个数字如何编写代码来查找该数字并输出它在数组中的位置?...

Java中的数组很新,但我给出的问题是:

“在您的main方法中,提示用户输入十个数字并将它们存储在一个数组中 . 编写一个名为find的方法,返回数组中特定数字第一次出现的位置 . 如果该数字不在列表中,你的方法应该返回-1 . 从main,提示用户输入一个数字,调用find方法,并从main显示结果给用户 . 如果方法返回-1,告诉用户该数字不在不要在find方法中与用户交互 . 方法的签名必须是:public static int find(int [] arr,int thingToFind)“

输出示例:

Enter ten numbers: 18 14 82 17 2 14 6 2 18 4

What number would you like me to find? 2

2 first occurs in the 5th place in the list.

Enter ten numbers: 4 19 0 41 ­2 ­7 7 14 41 100

What number would you like me to find? 99

99 is not in the list.

我知道我需要一个循环来进入find方法,但不确定哪一个,我正在考虑for循环但不知道如何实现它 . 这是我现在的代码,是的,我知道我错过了很多,而这只是它的开始 . 不知道从哪里开始 .

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter 10 numbers ");

int n = in.nextInt();

int arr[] = new int[n];

}

public static int find(int[] arr, int thingToFind){

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值