SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 79 in 26 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. public class Index {
  3.     public static void main(String[] args){
  4.         int size, number;
  5.         Scanner input = new Scanner(System.in);
  6.         System.out.println("input the size of your array");
  7.         size = input.nextInt();
  8.         System.out.println("input " + size + " numbers");
  9.         int arr[] = new int[size];
  10.         for(int i = 0; i < arr.length; i++)
  11.             arr[i]= input.nextInt();
  12.  
  13.         System.out.println("displaying your array");
  14.         for(int i = 0; i < arr.length ; i++)
  15.             System.out.print(arr[i] + " ");
  16.         System.out.println("\n");
  17.         System.out.println("choose a number from the array to determine its index");
  18.         number = input.nextInt();
  19.        
  20.         //the change starts here
  21.         bool isfound=false; //the flag if found or not
  22.         int foundwhere;     //the index if it is found
  23.  
  24.         for(int i = 0; i<arr.length; i++){
  25.             if(arr[i] == number){
  26.                
  27.                 //then change the flag
  28.                 isfound=true;
  29.                 foundwhere=i;
  30.  
  31.                 //exit the loop
  32.                 break;
  33.             }          
  34.         }
  35.        
  36.         //here test if the isfound is true; if not true  then it is not found
  37.         if (isfound==true)
  38.         {
  39.             print the number is at index "foundwhere"
  40.         }
  41.         else
  42.             print not found
  43.        
  44.  
  45.        
  46.        
  47.        
  48.     }
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top