-2

Could someone please help me to make this loop? I want the user to have the option to continue Y/N at the end of the program to rerun or terminate it. Preferably a do-while loop?

CODE (Java):

public static void main(String[] args) {
  
   int size = 10; //size of array
   Scanner scan = new Scanner(System.in); //create scanner
  
   while (true)
   {
       System.out.println("Enter values: ");
       int[] myArray = new int[size]; //declare array 
      
       for(int i = 0; i < size; i++)
       {
           int userInput = scan.nextInt(); //receive user input
           myArray[i] = userInput;
       }
      
      
      
       System.out.print("You entered these values: "); //print all values entered by user
       
       for (int i=0; i<myArray.length; i++)
       {  
           System.out.print(" " + myArray[i]);
       }
      
       System.out.println("");
      
       IndexOfLargestBatista Object = new IndexOfLargestBatista(); //object of IndexOfLargest class
      
       System.out.println("Index of Largest value: " + Object.findIndex(myArray)); //pass array as parameter and get index number in return
      
       
      
   }
takendarkk
  • 3,347
  • 8
  • 25
  • 37
sunnyb
  • 9

1 Answers1

0

You can do something like:

    public static void main(String[] args) {
        int size = 10; //size of array
        String nextInput;
        Scanner scan = new Scanner(System.in); //create scanner
        do {
            System.out.println("Enter values: ");
            int[] myArray = new int[size]; //declare array

            for (int i = 0; i < size; i++) {
                int userInput = scan.nextInt(); //receive user input
                myArray[i] = userInput;
            }


            System.out.print("You entered these values: "); //print all values entered by user

            for (int i = 0; i < myArray.length; i++) {
                System.out.print(" " + myArray[i]);
            }

            System.out.println("");

            IndexOfLargestBatista largeIndex = new IndexOfLargestBatista(); //object of IndexOfLargest class

            System.out.println("Index of Largest value: " + largeIndex.findIndex(myArray)); //pass array as parameter and get index number in return

            System.out.println("Do you want to continue?: Y/N ");
            nextInput = scan.nextLine();

        } while (nextInput.equalsIgnoreCase("Y"));
    }