2015-12-05 76 views
0

如果有人能幫助我解決這個問題,我將不勝感激。顯示不同方法的陣列

在第三個方法,我問要顯示的內容是在方法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

你可以將它們設置爲全局數組而不是顯示/調用你的reuire mrthod –

回答

0

定義數組以外的方法,這樣你可以在整個類中訪問它。將其設置爲static也可讓您在整個程序中訪問它。