write ajava program to Searching for the number

write ajava program to Searching for the number
  1. public static void main(String[] args) 
        {
            int[] nums = new int[100];
    
            for (int i = 0; i < nums.length; i++)
            {
                nums[i] = (int)((Math.random() * 100) + 1);
                System.out.print(nums[i] + " , ");
            }
         System.out.println();
         Scanner input = new Scanner(System.in);
         int num;
         System.out.println("What number would you like to search for?");
         num = input.nextInt();
         boolean found = false;        
         for (int i = 0; i < nums.length; i++) 
            {
                if (num == nums[i]) 
                {              
                   found = true;
                   break;
                }
    
                if (found)
                {
                    System.out.println("That number was found at index" + i);
                    break;
                }
                else
                {
                    System.out.println("That number was not found.");
                    break;
                }
            }       
        }
    • 1
أضف اجابة

أضف اجابة

‫تصفح