One-dimensional array of Java arrays traverse the search element

Source: Internet
Author: User

http://www.verejava.com/?id=16992664685428

/*题目: 从scores中查找给定的值value 在数组scores中的索引位置思路:    1. 遍历数组scores中的值, 如果有值与 给定的value相等  打印出当前索引    否则打印-1 没有找到*/import java.util.Scanner;public class ArraySearch{    public static void main(String[] args)    {        //一维数组的定义和初始化        int[] scores={90,70,50,80,60,85};        System.out.println("请输入要查找的值value:");        Scanner in=new Scanner(System.in);        int value=in.nextInt();        //. 遍历数组scores中的值, 如果有值与 给定的value相等  打印出当前索引        //否则打印-1 没有找到        boolean isSearch=false;        for(int i=0;i<scores.length;i++)        {            if(scores[i]==value)            {                isSearch=true;                System.out.println("找到值:"+value+" 在数组scores中的索引为: "+i);                break;            }        }        if(!isSearch)        {            System.out.println("在数组scores中没有找到 值 : "+value);        }    }}

http://www.verejava.com/?id=16992664685428

One-dimensional array of Java arrays traverse the search element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.