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 中执行不同的数组操作。

更新于: 2022 年 12 月 27 日

2K+ 浏览量

启动你的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.