2013-08-31 3 views
0

제 과제는 야구 선수가 초안을 결정하는 데 도움이되는 프로그램을 작성해야합니다. 이 프로그램은 플레이어에 대한 정보를 입력하고 배열에 저장하는 스카우트를 요청합니다. 그런 다음 배열을 확인하고 25 세 미만의 선수 목록을 표시하며 타율은 0.280 이상입니다. 목록은 연령별로 정렬해야합니다.저장된 배열의 특정 값 정렬 및 표시

아, 메뉴가 필요합니다.

제 문제는 제목 외에 출력이 나오지 않는다는 것입니다. 정렬되지 않았습니까? if 문이 작동하지 않습니까? 뭐가 문제 야!?

선수 클래스는 다음과 같습니다

public class players 
{ 
    String name; 
    String position; 
    int age; 
    double average; 
} 

여기 내 코드입니다 :

나는 이것이 곧 그래서 어떤 도움을 크게 감상 할 수있다 손 필요
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class BlueJays 
{  
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    static players[] arr; 

public static void main(String[] args) throws IOException 
{ 
    String quit = "n"; 
    while("n".equals(quit)) 
    { 
     //display menu 
     System.out.println("Toronto Blue Jays Drafting Program - Main Menu"); 
     System.out.println("1) Input Blue Jays data"); 
     System.out.println("2) Display possible draft choices");  
     System.out.println("3) Quit program"); 
     System.out.print("Please choose an option by inputting the number of your choice: "); 
     String choiceString = br.readLine(); 
     int choice = Integer.parseInt(choiceString); 

     if(choice == 1) 
     { 
      inputInfo(); 
     }else if(choice == 2) 
     { 
      sortInfo(); 
     }else if(choice == 3) 
     { 
      System.out.println("Are you sure you want to quit? (y/n) "); 
      quit = br.readLine(); 
     }else 
     { 
      System.out.println("Not a valid option."); 
     }     
    } 
} 

//method to input names of Blue Jays 
public static void inputInfo() throws IOException 
{ 
    players temp = new players(); 
    System.out.print("How many players would you like to enter? "); 
    int x = Integer.valueOf(br.readLine()).intValue(); 
    arr = new players[x];  

    //loop through players 
    for(int i = 0; i < arr.length; i++) 
    { 

     System.out.println("Enter player information."); 

     System.out.println("Input first and last name: "); 
     String name = br.readLine(); 
     temp.name = name;  

     System.out.println("Input position: "); 
     String position = br.readLine(); 
     temp.position = position; 

     System.out.println("Input batting average (e.g. .246): "); 
     String averageString = br.readLine(); 
     temp.average = Double.parseDouble(averageString); 

     System.out.println("Input age: "); 
     temp.age = Integer.parseInt(br.readLine()); 
     System.out.println(" "); 

     // Copy the software name and quantity to the global variables 
     arr[i] = temp; 
    } 
} 

//method to sort and display info 
public static void sortInfo() 
{    
    //sort by quantity 
    for(int i = 0; i < arr.length; i++) 
    { 
     for(int j = i+1; j < arr.length; j++) 
     { 
      if(arr[i].age > arr[j].age) 
      { 
       players temp = arr[j]; 
       arr[j] = arr[i]; 
       arr[i] = temp; 
      } 
     } 

    }    
    System.out.println("Draft Choices 2013");   
    //output 
    for(int i = 0; i < arr.length; i++) 
    { 
     if (arr[i].age <= 25 && arr[i].average >= 0.280) 
     { 
      System.out.println("Name: " + arr[i].name);  
      System.out.println("Age: " + arr[i].age); 
      System.out.println("Position: " + arr[i].position); 
      System.out.println("Batting average: " + arr[i].average); 
      System.out.println(" ");   
     }    

    } 
    } 
} 

! 미리 감사드립니다! 당신에게 문제를 일으키는 원인이 될 수있는 코드에서

+0

는 "제목 만"무엇을 의미합니까 for

System.out.print("How many players would you like to enter? "); int x = Integer.valueOf(br.readLine()).intValue(); arr = new players[x]; //loop through players for(int i = 0; i < arr.length; i++) { players temp = new players(); // <---- THIS System.out.println("Enter player information."); System.out.println("Input first and last name: "); String name = br.readLine(); temp.name = name; .... 

}는? 'Toronto Blue Jays Drafting Program - Main Menu' 라인 만? 그것은 아주 이상 할 것이다. – SJuan76

+0

@ SJuan76 아니요, "초안 선택 2013"라인을 의미했습니다. – Sal

답변

3

WTF는 :

players temp = new players(); 
System.out.print("How many players would you like to enter? "); 
int x = Integer.valueOf(br.readLine()).intValue(); 
arr = new players[x];  

//loop through players 
for(int i = 0; i < arr.length; i++) 
{ 

    System.out.println("Enter player information."); 

    System.out.println("Input first and last name: "); 
    String name = br.readLine(); 
    temp.name = name;  
    .... 

}

당신은 단지 players 인스턴스를 생성하고 재사용. 그래서, 당신은 20 개의 서로 다른 배열 (예 : players)을 가지고 있지 않지만 동일한 객체 ()에 20 배를 가리키는 배열을 가지고 있습니다. 이 인스턴스 데이터는 연속적으로 덮어 쓰여지고 마지막으로 입력 한 플레이어의 데이터 만 보유하며, age > 25 인 경우 필터로 인해 레코드가 인쇄되지 않습니다.

는 내부의 인스턴스 생성을 이동

+0

매력처럼 일했습니다! 당신의 도움을 주셔서 감사합니다! – Sal

관련 문제