1. Write a method with the following specifications: name: DisplayMenu arguments: none return value: none tasks: display the following menu choice on the screen Calculation Menu, developed by StudentID:xxxx B) Calculate Number E) To Exit Enter the Letter that corresponds to your choice: Please update your ID in the menu. You don’t have to implement the functionalities of the various menu choices at this stage Call this method from your main. 2. Modify your main so that the above method is call repeatedly. The program will terminate when the user enters E. Any other choice should produce an error message. Because you will not be doing any arithmetic you may accept the user response either as an int, or a char or a string. 3. Write a method with the following specifications: name: CalculateN arguments: an int array with values ranging from 0 to 10 return value: int array of 3 integers. The first element will indicate the total sum of all the values in the array, the second element will indicate the average of all the values in the array, the third element will indicate the smallest value in the array. displays: none tasks: The method will create an int array of 3 elements (call this the result). In your main you will generate an int array of 5 elements with values ranging from 0 to 10, call this method and display the resulting array’s value.
时间: 2024-01-31 12:02:06 浏览: 96
Here's the code for the first method:
```java
public static void DisplayMenu() {
System.out.println("Calculation Menu, developed by StudentID:xxxx");
System.out.println("B) Calculate Number");
System.out.println("E) To Exit");
System.out.print("Enter the Letter that corresponds to your choice: ");
}
```
And here's the updated main method that repeatedly calls the DisplayMenu method and terminates when the user enters E:
```java
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char choice = ' ';
do {
DisplayMenu();
choice = input.nextLine().charAt(0);
switch (choice) {
case 'B':
// Implement functionality for calculating a number
break;
case 'E':
System.out.println("Exiting program...");
break;
default:
System.out.println("Error: Invalid choice.");
break;
}
} while (choice != 'E');
}
```
And finally, here's the code for the CalculateN method:
```java
public static int[] CalculateN(int[] arr) {
int sum = 0;
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
if (arr[i] < min) {
min = arr[i];
}
}
int avg = sum / arr.length;
int[] result = {sum, avg, min};
return result;
}
```
To generate an int array of 5 elements with values ranging from 0 to 10 and call the CalculateN method, you can add the following code to your main method:
```java
int[] arr = {3, 7, 0, 10, 5};
int[] result = CalculateN(arr);
System.out.println("Sum: " + result[0]);
System.out.println("Average: " + result[1]);
System.out.println("Minimum: " + result[2]);
```
Note that you can change the values in the array to generate different test cases for the CalculateN method.
阅读全文