السلام عليكم ارجو منكم المساعده للضروره القصوى فضلا وليس امر

لولو العتيبي • منذ 5 سنوات

TASK 3 : Write a Java program that prompts for and reads the size n of an integer array, it prints an appropriate error message and loops as long as n is not a valid array size. If the array size is valid the program initializes an integer array of size n with n pseudo-random integers from 1 to 30.

 

Print the initialized array then prompt the user to enter a target value to search for in the array by using   linear search.  If present, your program should display its first position in the array, otherwise print a  message that the value is not in the array.

 

ارجو منكم المساعده علما انه يجب ان يكون الحل بلغه جافا و بي ابسط طريقه ممكنه     Note: To generate a random integer in the interval [1, 30] use the method call:

             (int)(Math.random( )*30) + 1

 

     random( ) returns a double value in the interval [0.0,  1.0)

كلمات دليلية: java

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (1)

yazan qwaider • منذ 5 سنوات
import java.util.*;


public class Main {

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        
        int n = in.nextInt();
        if(n > 0){
            
            int[] arr= new int[n];
            
            for(int i=0; i<n; i++){
                arr[i] = (int)(Math.random()*30) + 1;
                System.out.print(arr[i] + "  ");
            }
            
            System.out.println("\n"+"please enter the target number ?");
            
            int tar_num = in.nextInt();
            
            boolean flag =false;
            
            for(int i=0; i<n; i++){
                if(tar_num == arr[i]){
                    System.out.println("its first position in the array");
                    flag = true;
                    break;
                }
            }
            
            if(flag == false)
                 System.out.println("target number not found !!");

                    
        }
       else{
               System.out.println("size number not valid !!");


        }
        
     
    
    }

}

 

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !