我正在制定一个信用卡计划,告诉您应该先付清哪张卡。所有卡都是从用户读入的。 我的问题似乎是我无法找到一种方法来显示利率最高的卡。
我已经设置了一个名为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 :(得分:0)
你的方法
// I've removed the index parameter as it makes no sense
public static int findHighestRate(double interestRate [])
的返回类型为int
。因此,您必须使方法返回一个值。如果它返回一个值,您可以将该值赋给变量或将其用作另一个方法的参数。
例如
int index = findHighestRate(someDoubleArray);
然后,您可以使用该变量的值来访问数组中的元素
double highestRate = someDoubleArray[index];
然后您可以打印出该值
System.out.println(highestRate);