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 1) Calculate Sum 2) Calculate Sum of Squares 3) Calculate Sum of Cubes 0) To Exit Enter the number that corresponds to your choice: You may beautify the output to your own likings. You don’t have to implement the functionalities of the various menu choices at this stage Call this method from your main.
时间: 2023-07-03 11:10:27 浏览: 54
Sure, here's the method that meets the specifications you provided:
```
public static void DisplayMenu() {
System.out.println("Calculation Menu");
System.out.println("1) Calculate Sum");
System.out.println("2) Calculate Sum of Squares");
System.out.println("3) Calculate Sum of Cubes");
System.out.println("0) To Exit");
System.out.print("Enter the number that corresponds to your choice: ");
}
```
You can call this method from your main method like this:
```
public static void main(String[] args) {
DisplayMenu();
}
```
This will display the menu on the screen when you run the program.
相关问题
Write the following code in c #,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 1) Calculate Sum 2) Calculate Sum of Squares 3) Calculate Sum of Cubes 0) To Exit Enter the number that corresponds to your choice: You may beautify the output to your own likings. You don’t have to implement the functionalities of the various menu choices at this stage Call this method from your main.
Sure, here's the C# code that meets the specifications you provided:
```
using System;
public class Program {
public static void Main(string[] args) {
DisplayMenu();
}
public static void DisplayMenu() {
Console.WriteLine("Calculation Menu");
Console.WriteLine("1) Calculate Sum");
Console.WriteLine("2) Calculate Sum of Squares");
Console.WriteLine("3) Calculate Sum of Cubes");
Console.WriteLine("0) To Exit");
Console.Write("Enter the number that corresponds to your choice: ");
}
}
```
You can call the `DisplayMenu()` method from your `Main()` method, as shown above, to display the menu on the console when you run the program.
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.
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.
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)