Java 菜单驱动程序执行数组操作

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程