Java 選單驅動程式執行陣列操作
Java 中的陣列被稱為非基本資料型別,它儲存固定數量的相同型別的值。它被稱為一維陣列。
在本文中,我們將瞭解如何使用 Java 選單驅動程式執行不同的陣列操作,例如檢查重複元素、以相反順序列印陣列、檢查最大元素、檢查最小元素以及查詢所有陣列元素的總和。我們將使用 switch case 實現應用程式。
向您展示一些例項 -
例項 1
Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will find duplicate elements of an array. And hence result will be Duplicate elements in given array: 2 6
例項 2
Suppose we have created an array containing 6 elements and array elements are[2,4,6,2,6,8]. Now we will print the array in reverse order. And hence result will be Original array: 2 4 6 2 6 8 Array in reverse order: 8 6 2 6 4 2
例項 3
Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the largest element in an array. And hence result will be Largest element present in given array: 8
例項 4
Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the smallest element in an array. And hence result will be. Smallest element present in given array: 2
例項 5
Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the sum of all the items of the array. And hence result will be Sum of all the elements of an array: 28
語法
為了執行陣列中的基本操作,例如查詢重複項、陣列的反轉、陣列的最大元素、陣列的最小元素、列印陣列所有元素的總和,我們使用 for 迴圈以及上面提到的每個部分的一些基本邏輯。
以下是“for 迴圈”的語法 -
for(initialization; condition; increment/decrement){//statement}
演算法
步驟 1 - 要求使用者輸入所需的元素以建立陣列。
步驟 2 - 顯示選單。
步驟 3 - 要求使用者輸入他們的選擇。
步驟 4 - 使用 switch case 轉到選擇並執行操作。
步驟 5 - 列印結果。
讓我們看看程式以更清楚地瞭解它。
示例
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); int n=sc.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the array: "); for(int i=0; i<n; i++) { array[i]=sc.nextInt(); } System.out.println("Array elements are: "); for (int i=0; i<n; i++) { System.out.println(array[i]); } mainLoop: while (true) { Scanner inn = new Scanner( System.in ); System.out.println("\n***Menu***"); System.out.println("1. Find duplicate elements of an array"); System.out.println("2. Print array in reverse order"); System.out.println("3. Print the largest element in an array"); System.out.println("4. Print the smallest element in an array"); System.out.println("5. Print the sum of all the items of the array"); System.out.println("6. Terminate the program"); System.out.println("Enter action number (1-6): "); int command; if ( inn.hasNextInt() ) { command = inn.nextInt(); inn.nextLine(); } else { System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); inn.nextLine(); continue; } switch(command) { case 1: System.out.println("Duplicate elements in given array: "); for(int i = 0; i < array.length; i++) { for(int j = i + 1; j < array.length; j++) if(array[i] == array[j]) System.out.println(array[j]); } } break; case 2: System.out.println("Original array: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); System.out.println("Array in reverse order: "); for (int i = array.length-1; i >= 0; i--) { System.out.print(array[i] + " "); } break; case 3: int max = array[0]; for (int i = 0; i < array.length; i++) { if(array[i] > max) max = array[i]; } System.out.println("Largest element present in given array: " + max); break; case 4: int min = array[0]; for (int i = 0; i < array.length; i++) { if(array[i] <min) min = array[i]; } System.out.println("Smallest element present in given array: " + min); break; case 5: int sum = 0; for (int i = 0; i < array.length; i++) { sum = sum + array[i]; } System.out.println("Sum of all the elements of an array: " + sum); break; case 6: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }
輸出
Enter the number of elements you want to store: 5 Enter the elements of the array: 4 1 5 3 2 Array elements are: 4 1 5 3 2 ***Menu*** 1. Find duplicate elements of an array 2. Print array in reverse order 3. Print the largest element in an array 4. Print the smallest element in an array 5. Print the sum of all the items of the array 6. Terminate the program Enter action number (1-6): 2 Original array: 4 1 5 3 2 Array in reverse order: 2 3 5 1 4 ***Menu*** 1. Find duplicate elements of an array 2. Print array in reverse order 3. Print the largest element in an array 4. Print the smallest element in an array 5. Print the sum of all the items of the array 6. Terminate the program Enter action number (1-6): 1 Duplicate elements in given array: ***Menu*** 1. Find duplicate elements of an array 2. Print array in reverse order 3. Print the largest element in an array 4. Print the smallest element in an array 5. Print the sum of all the items of the array 6. Terminate the program Enter action number (1-6): 5 Sum of all the elements of an array: 15 ***Menu*** 1. Find duplicate elements of an array 2. Print array in reverse order 3. Print the largest element in an array 4. Print the smallest element in an array 5. Print the sum of all the items of the array 6. Terminate the program Enter action number (1-6): 3 Largest element present in given array: 5 ***Menu*** 1. Find duplicate elements of an array 2. Print array in reverse order 3. Print the largest element in an array 4. Print the smallest element in an array 5. Print the sum of all the items of the array 6. Terminate the program Enter action number (1-6): 4 Smallest element present in given array: 1 ***Menu*** 1. Find duplicate elements of an array 2. Print array in reverse order 3. Print the largest element in an array 4. Print the smallest element in an array 5. Print the sum of all the items of the array 6. Terminate the program Enter action number (1-6): 6 Program terminated
在本文中,我們探討了如何使用選單驅動方法在 Java 中執行不同的陣列操作。
廣告