我是堆叠溢出的新手,我正在尝试用他们的编程作业来帮助朋友。
到目前为止,我们已经有了这个
package range;
import java.util.Arrays;
import java.util.Scanner;
public class Range
{
static int[] series = new int[100];
static int seriesLength = 0;
public static void main(String[] args)
{
Scanner t = new Scanner(System.in);
boolean run = true;
while(run)
{
int option;
System.out.println("1. Loading a range of up to 100 numbers");
System.out.println("2. Showing the range of given(loaded) numbers");
System.out.println("3. Determination of the middle value of the series");
System.out.println("4. Determination of the biggest element of the series");
System.out.println("5. Determination of the smallest element of the series\n");
System.out.println("Enter the number of the option you want (1-5), or 0 to end");
option = t.nextInt();
switch(option)
{
case 1:
{
System.out.println("Please input a number from 1 -100");
seriesLength = t.nextInt();
System.out.println(seriesLength);
if((seriesLength < 1) || (seriesLength > 100))
{
System.out.println("Invalid input, series must be between 1 and 100.\nPress any key to try again.\n");
break;
}
for(int i = 0; i < seriesLength; i++)
{
series[i] = i+1;
}
break;
}
case 2:
{
System.out.println(seriesLength);
if(seriesLength == 0)
{
System.out.println("You must first load a series of numbers\n");
break;
}
showSeries(series, seriesLength);
break;
}
case 3:
{
if(seriesLength == 0)
{
System.out.println("You must first load a series of numbers\n");
break;
}
middleNum(series, seriesLength);
break;
}
case 4:
{
if(seriesLength == 0)
{
System.out.println("You must first load a series of numbers\n");
break;
}
biggestNum(series, seriesLength);
break;
}
case 5:
{
if(seriesLength == 0)
{
System.out.println("You must first load a series of numbers\n");
break;
}
smallestNum(series, seriesLength);
break;
}
case 0:
{
System.out.println("BYE, DOBRO DOBRO.");
run = false;
break;
}
default:
{
System.out.println("Invalid input");
break;
}
}
}
}
public static void showSeries(int[] input, int range)
{
for(int i = 0; i < range; i++)
{
System.out.println(input[i]);
}
}
public static void biggestNum(int[] input, int range)
{
Arrays.sort(input);
System.out.println(input[0]);
}
public static void middleNum(int[] input, int range)
{
int Middle = input.length / 2;
if ((input.length % 2) > 0)
{
System.out.println(input[Middle]);
}
else
{
System.out.println((input[Middle-1] + input[Middle]) / 2.0);
}
}
public static void smallestNum(int[] input, int range)
{
Arrays.sort(input);
for(int i = 0; i < range; i++)
{
System.out.println(input[i]);
}
}
}
任务是编写一个程序,使用菜单处理数字数组 几乎没有选择。但是每个选项的任务也需要是一个单独的方法,主要方法只需要显示菜单并根据所选的数字(选项)调用每个方法。还需要检查用户错误,例如,如果在选项1之前选择了选项2,或者选择的选项不存在等等。
我对如何继续感到困惑,因为我不是java的专家。考虑到可以使用的唯一东西是由任务
定义的,如何做到这一点答案 0 :(得分:2)
也许你想要更像这样的东西,show series方法实际上设置了数组,所以其他方法可以使用数组
static int[] series = new int[100];
static int seriesLength = 0;
...
// Get the input for range
public static void showSeries(int range)
{
seriesLength = range;
series = new int[seriesLength];
for (int i = 0; i < series.legnth; i++) {
series[i] = i;
System.out.print(series[i] + " ");
}
}
因为第一个方法需要是先调用的第一个方法,所以将设置数组。然后其他方法不应该采用任何参数,因为数组已经设置,他们可以只使用静态数组。