英文:
Parallel arrays for finding the highest element
问题
我想在“prices”数组中找到最高的元素,然后打印出“letters”数组中相应的元素。
关于我可以做什么,我需要一些建议。我尝试过输出letters[index],但由于作用域的原因,我遇到了错误。我对编程非常新,所以这真的让我困扰。
String[] letters = {"A", "B", "C", "D", "E", "F", "G"};
double[] prices = {1.00, 2.00, 50.00, 4.00, 5.00, 6.00, 7.00};
double big = prices[0];
// 使用循环查找数组中的最大值
for (int index = 0; index < prices.length; index++) {
if (prices[index] > big) {
big = prices[index];
}
}
System.out.println("具有最高价值的字母是 " + letters[index]);
英文:
I want to find the highest element in the "prices" array then print the corresponding element in the "letters" array
I need some suggestions about what I can do. I have tried outputting letters[index] but I get an error because of the scope I think. I'm very new to coding so this is really stumping me right now.
String[] letters = {"A", "B", "C", "D", "E", "F", "G"};
double[] prices = {1.00, 2.00, 50.00, 4.00, 5.00, 6.00, 7.00};
double big = prices[0];
//for loop to find the highest value in the array
for(int index = 0; index < prices.length; index++)
{
if(prices[index] > big)
{
big = prices[index];
}
}
System.out.println("The letter with the highest value is " + big);
答案1
得分: 1
你需要两个变量:一个用来跟踪当前的“最高值”,另一个用来跟踪此值的索引。
double big = prices[0];
int bigIndex = 0;
然后
if (prices[index] > big) {
big = prices[index];
bigIndex = index;
}
最后:
System.out.println("具有最高值的字母是:" + letters[bigIndex]);
英文:
You need two variables: one to keep track of the current "highest values" and one to keep track of the index of this value.
double big = prices[0];
int bigIndex = 0;
then
if (prices[index] > big) {
big = prices[index];
bigIndex = index;
}
and finally:
System.out.println("The letter with the highest value is " + letters[bigIndex]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论