如何在Java中显示数组的某个元素?
我正在做一个信用卡计划,告诉你什么卡应该首先付清。所有卡都是从用户处读入的。 我的问题似乎是我找不到一种方法来显示利率最高的卡 我已经建立了一个名为findHighestRate的方法,它的工作(正如您所猜测的)是查看数组并找到用户输入的最高利率。现在我相信这部分代码是正确的,但是我已经无数次尝试通过我的显示方法(在图表中显示所有用户输入)仅显示最高的代码,但每次都出现了不足如何在Java中显示数组的某个元素?,java,arrays,Java,Arrays,我正在做一个信用卡计划,告诉你什么卡应该首先付清。所有卡都是从用户处读入的。 我的问题似乎是我找不到一种方法来显示利率最高的卡 我已经建立了一个名为findHighestRate的方法,它的工作(正如您所猜测的)是查看数组并找到用户输入的最高利率。现在我相信这部分代码是正确的,但是我已经无数次尝试通过我的显示方法(在图表中显示所有用户输入)仅显示最高的代码,但每次都出现了不足 public static void header () { System.out.printf("%9s %2
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.
}
publicstaticvoid头()
{
System.out.printf(“%9s%22s%24s%29s”、“卡”、“利率”、“当前欠款”、“一年欠款”);
System.out.println();
系统输出打印(“==============================================”);
系统输出打印(“===================================================================================================================”);
系统输出打印(“==”);
}
公共静态无效显示(字符串卡[],双rOI[],双电流[],双欠1年[],整数i)
{
System.out.printf(“\n%11s%14.2f%25.2f%25.2f”,卡片[i],投资回报率[i],当前[i],欠下的1年[i]);
}
公共静态字符串getString(字符串s)
{
扫描仪输入=新扫描仪(系统输入);
//提示
系统输出打印(s+“:”);
return keyIn.nextLine().trim();
}
公共静态int findHighestRate(双倍利率[],int索引)
{
双最大值=0;
指数=-1;
for(int i=0;i最大值){
最大值=酯[i];
指数=i;
}
收益指数;
}
公共静态双getDouble(字符串s)
{
扫描仪标准输入=新扫描仪(System.in);
//提示用户输入所需的字符串
系统输出打印(s+“:”);
返回stdIn.nextDouble();
}
公共静态void main(字符串[]args)
{
//使阵列具有生命力。
字符串[]c=新字符串[4];
double[]利率=新的double[4];
double[]currentAmountOwed=新的double[4];
双精度[]YR1OWD=新双精度[4];
int指数=0;
简介();
System.out.println(“请输入卡名、利率和欠款”);
对于(int i=0;i您的方法
// 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);
你有一个findHighestRate
方法。你为什么不调用它?为什么它有一个index
参数?你为什么要马上修改它?因为我需要打印出最高的速率,通过调用什么都不会打印,对吗?只要索引作为参数传递,这就是数组的占位符,right?findHighestRate
返回某个值。该值是什么?它代表什么?如何访问数组的元素?如果可以访问它,不能打印它吗?数组的占位符是什么意思?如果要立即修改它,调用方提供它有什么意义?我希望它返回I最高值所在的ndex。大体上,我不确定如何显示该索引,因为如果我说“findHighestRate(rateOfInterest)”没有打印任何内容。我也尝试过使用显示方法。我不确定为什么在参数列表中有索引。正如您所看到的,也有点困惑。@TylerB不客气。不清楚您在那里要求什么,但我建议您立即停止您正在做的事情。检查数组如何工作。检查方法如何下面是正式的Java教程,请仔细阅读。