如果有人能帮我解决这个问题,我将不胜感激。
在第三种方法中,我被要求在方法enterSequenceNames和enterSequenceLength
中显示用户键入的内容我挣扎,因为这些是两种不同的方法,而且它们也是数组?拜托,我该怎么做呢。到目前为止,这是我的代码。
import java.util.Scanner;
public class P12Ex5
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size;
System.out.print("Please enter the number of sequences in the film: ");
size = sc.nextInt();
int option;
int [] numberOfSequences;
//String [] name;
//int [] length;
numberOfSequences = new int [size];
//name = new String [size];
//length = new int [size];
do
{
System.out.println();
System.out.println();
System.out.println();
System.out.println("*************** MENU ***************");
System.out.println("1. Enter sequene names");
System.out.println("2. Enter the length of each sequence in the film(in minutes)");
System.out.println("3. Display all sequence details (names and length");
System.out.println("4. Display the details of longest sequence");
System.out.println("5. Display the name of the shortest sequence");
System.out.println("6. Calculate and display the average sequence length");
System.out.println("7. Exit system");
System.out.println();
System.out.print("Please enter an option: ");
option = sc.nextInt();
System.out.println();
System.out.println();
System.out.println();
switch(option)
{
case 1:
enterSequenceNames(numberOfSequences);
break;
case 2:
enterSequenceLength(numberOfSequences);
break;
//case 3:
//displayFilmDetails(numberOfSequences, name, length);
}
}while(option != 0);
}
private static void enterSequenceNames(int [] numberOfSequencesIn)
{
Scanner sc = new Scanner(System.in);
int i;
String name;
for(i=0; i<numberOfSequencesIn.length; i++)
{
System.out.print("Enter name of sequence "+(i+1)+": ");
name = sc.next();
}
}
private static void enterSequenceLength(int [] numberOfSequencesIn)
{
Scanner sc = new Scanner(System.in);
int i;
int length;
for(i=0; i<numberOfSequencesIn.length; i++)
{
System.out.print("Enter the length of sequence "+(i+1)+": ");
length = sc.nextInt();
}
}
private static void displayFilmDetails(int [] numberOfSequencesIn, String [] nameIn, int [] lengthIn)
{
System.out.println();
for( int i = 0; i<numberOfSequencesIn.length; i++)
{
// System.out.println("Name: "+nameIn[i]+"Length: "+lengthIn[i]);
// System.out.print(nameIn[i], lengthIn[i]);
}
}
}
答案 0 :(得分:0)
在方法之外定义数组,这样您就可以在整个类中访问它。将其设置为static
也可以让您在整个程序中访问它。