2014-06-23 2 views
0

나는 먼저 어떤 카드를 지불해야하는지 알려주는 신용 카드 프로그램을 작성 중입니다. 모든 카드는 사용자로부터 읽혀집니다. 가장 큰 이자율로 카드를 표시하는 방법을 찾지 못하는 것 같습니다.Java에서 배열의 특정 요소를 어떻게 표시합니까?

나는 findHighestRate라는 메서드를 설정했으며, 사용자가 입력 한 최고 이자율을 찾고 배열을 살펴 보는 것이 그 일이라고 생각합니다. 이제 나는 코드의 일부가 정확하다고 믿지만, 내 디스플레이 방법 (차트의 모든 사용자 입력을 표시)을 통해 가장 높은 값을 표시하기 위해 무수히 많은 노력을했지만 항상 매번 짧은 시간을 보냈습니다.

public static void header() 
{ 
    System.out.printf("%9s %22s %24s %29s" , "Card" , "Rate of Interest" , "Current Amount Owed", "Amount owed in One Year"); 
    System.out.println(); 
    System.out.print("============================"); 
    System.out.print("================================================================="); 
    System.out.print("====="); 
} 

public static void display (String card [], double rOI [], double current [], double owed1yr [], int i) 
{ 

    System.out.printf("\n%11s %14.2f %25.2f %25.2f" , card[i], rOI[i], current[i], owed1yr[i]); 
} 

public static String getString (String s) 
{ 
    Scanner keyIn = new Scanner (System.in); 
    //prompt 

    System.out.print(s+": "); 
    return keyIn.nextLine().trim(); 
} 

public static int findHighestRate(double interestRate [], int index) 
{ 
    double max = 0; 
    index = -1; 
    for (int i = 0; i < interestRate.length; i++) 
     if (interestRate[i] > max){ 
      max = interestRate[i]; 
      index = i; 
    } 
    return index; 
} 

public static double getDouble(String s) 

{ 
    Scanner stdIn = new Scanner (System.in); 
    // Prompt the user for the string needed 
    System.out.print(s+": "); 
    return stdIn.nextDouble(); 

} 

public static void main (String [] args) 
{ 
    //bringing arrays to life. 
    String [] c = new String [4]; 
    double [] rateOfInterest = new double [4]; 
    double [] currentAmountOwed = new double [4]; 
    double [] yr1Owed = new double [4]; 

    int index =0; 
    intro(); 
    System.out.println("Please enter the name of the card, interest rate, and amount owed"); 
    for (int i = 0; i <c.length; i++){ 
     System.out.println(); 

     c[i] = getString ("Card"); 
     rateOfInterest [i] = getDouble("Rate of interest"); 
     currentAmountOwed [i] = getDouble ("Amount Owed"); 

    } 
    System.out.println(); 

    for (int i =0 ; i < c.length; i++){ 
     yr1Owed[i] = ((rateOfInterest[i] * currentAmountOwed[i] /100) + currentAmountOwed[i]); 
    } 

    header(); 

    for (int i = 0; i <c.length; i++) 
    { 
     display(c, rateOfInterest, currentAmountOwed,yr1Owed, i ); 

    } 
    System.out.println(); 
    System.out.println(); 

    System.out.println("Seach for the highest rate of interest.. Pay this off first"); 
    System.out.println(); 
    header(); 

    for (int i = 0; i <c.length; i++) 
    { 

     FIND HIGHEST RATE METHOD HERE 

    } 

    //highest rate part of code not included..... did not complete. 
} 

}

+0

당신은이 그 값을 출력 할 수있는 배열의 요소에 액세스 할 그 변수의 값을 사용할 수 있습니다

int index = findHighestRate(someDoubleArray); 

를 들어

'findHighestRate' 메소드. 왜 그걸 부르지 않는거야? 왜 그것은'index' 매개 변수를 가지고 있습니까? 왜 지금 수정하고 있니? –

+0

내가 가장 높은 비율을 인쇄해야하기 때문에 아무것도 인쇄하지 않을 것입니다. 매개 변수로 전달되는 인덱스의 경우 배열의 place holder가 맞습니까? –

+0

'findHighestRate'는 뭔가를 반환합니다. 그 가치는 무엇입니까? 그것이 무엇을 나타내는가? 어떻게 배열 요소에 접근합니까? 액세스 할 수 있으면 인쇄 할 수 없습니까? array_ **의 의미는 무엇입니까 **? 바로 수정하려면 발신자가 제공하는 것이 무엇입니까? –

답변

0

귀하의 방법

// I've removed the index parameter as it makes no sense 
public static int findHighestRate(double interestRate []) 

int, 반환 유형이 있습니다. 따라서 메소드가 값을 리턴하도록해야합니다. 값이 반환되면 변수에 값을 할당하거나 다른 메서드에 대한 인수로 사용할 수 있습니다. 예를 들어 당신은 다음

double highestRate = someDoubleArray[index]; 

당신은 다음

System.out.println(highestRate); 
+0

@TylerB 오신 것을 환영합니다. 당신이 무엇을 요구하고 있는지 불분명하지만, 당신이하고있는 일을 멈추고 한 번에 많이하는 것처럼 제안 할 것입니다. 배열 작업 방법을 검토하십시오. 메소드가 작동하는 방식과 호출 방식에 대해 검토하십시오. 공식 Java 자습서는 [여기] (http://docs.oracle.com/javase/tutorial/java/)에 있습니다. 주의 깊게 살펴보십시오. –

관련 문제