Java 阵列的新功能(挣扎)

Java 阵列的新功能(挣扎),java,Java,我只是在学习数组。我有一个小程序要写,要求创建2个数组。一个有5个花的名字。一个拿着那5朵花的价格。我问用户他们想要什么样的花和数量。然后我需要创建一个循环来定位花的名称,并使用该索引来查找花的成本。我正在努力学习如何编写代码,如何使用一个数组索引来查找另一个数组索引。示例:玫瑰和0.50美元。我知道我基本上要求很多,但我只是不知道如何从一个数组调用另一个数组。任何帮助都将是惊人的。非常感谢。这是我到目前为止所拥有的 Scanner keyboard = new Scanner(System.i

我只是在学习数组。我有一个小程序要写,要求创建2个数组。一个有5个花的名字。一个拿着那5朵花的价格。我问用户他们想要什么样的花和数量。然后我需要创建一个循环来定位花的名称,并使用该索引来查找花的成本。我正在努力学习如何编写代码,如何使用一个数组索引来查找另一个数组索引。示例:玫瑰和0.50美元。我知道我基本上要求很多,但我只是不知道如何从一个数组调用另一个数组。任何帮助都将是惊人的。非常感谢。这是我到目前为止所拥有的

Scanner keyboard = new Scanner(System.in);
    String [] flowers = new String[] {"petunias", "pansy", "rose", "violet", "carnation"};
    double [] cost = new double[] {.50, .75, 1.50, .50, .80};
    for(int pq = 1; pq <= 5; pq++){
        System.out.print("Enter the name of the flower purchased: ");

就像上面亨利所说的,只需循环遍历flower数组并找到其索引。此索引将与其在成本数组中的成本相对应。当有匹配项时,打印成本数组中该索引处的元素

        String [] flowers = new String[] {"petunias", "pansy", "rose", "violet", "carnation"};
        double [] cost = new double[] {.50, .75, 1.50, .50, .80};

        System.out.print("Enter the name of the flower purchased: ");
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in))
        String input = br.readLine();

        for(int i=0;i<flower.length;i++){
            if(input.equals(flowers[i])) {
                system.out.print(cost[i]);
            }
        }

用这样的东西

String [] flowers = new String[] {"petunias", "pansy", "rose", "violet", "carnation"};
double [] cost = new double[] {.50, .75, 1.50, .50, .80};
System.out.print("Enter the name of flower purchased: ");
String key = keyboard.nextLine().trim();
for (int i = 0; i < flower.length(); i++){
String flw = flowers[i].getName();
if ( flw.startswith(key)){ // you don't have to use startswith(key) 
    // you should be able to do the rest.
}
}

据我所知,花卉名称存储在一个数组中,它们的价格存储在同一索引的另一个数组中

import java.util.*;
Scanner keyboard = new Scanner(System.in);
    String [] flowers = new String[] {"petunias", "pansy", "rose", "violet", "carnation"};
    double [] cost = new double[] {.50, .75, 1.50, .50, .80};
    System.out.print("Enter the name of the flower purchased: ");
    String temp= keyboard.nextLine();
    for(int pq = 0; pq <= flowers.length; pq++){
    if (temp==flowers[i]) //compare your input with the array of flowers
    System.out.print(flowers[i]+" costs "+cost[i]; // display name and corresponding price
        }

这里是一个快速演练,我用什么来让这个工作

    String [] flowers = new String[] {"petunias", "pansy", "rose", "violet", "carnation"}; //flower array
    double [] cost = new double[] {.50, .75, 1.50, .50, .80}; //cost array

    System.out.print("Enter the name of the flower purchased: ");
    Scanner in = new Scanner(System.in); //new scanner to read user input
    String input = in.nextLine(); //gets the line from the user

    for(int i=0;i<flowers.length;i++){ //loop through the flower array, checking if the flower's name matches the user input
        if(input.equals(flowers[i])) { //if we have a match...
            System.out.print(cost[i]); //output the price
        }
    }
所以基本上,flowers数组和prices数组是一对一的,这意味着你可以在任何一朵花和它对应的价格之间画一条清晰的线。由于存在此关系,因此在遍历数组时可以使用相同的索引。e、 g:三色堇->0.75

假设我们在循环中的i=1


如果我们为要查找的类型输入pansy,我们将有input.equalsflowers[i]这是真正的pansy.equalspansy这是真的。然后我们将跳入代码系统.out.printcost[i];在这一点上说:System.out.print.75;因为我1岁。

经过大量的工作,我有了这个

    Scanner keyboard = new Scanner(System.in);
    double total = 0;
    String flowerName = "start";
    int quantity = 0;
    System.out.println("Welcome to the Salisbury Flower Boutique");
    String [] flowerNameArray = new String[] {"petunias", "pansy", "rose", "violet",                     "carnation"};
    double [] flowerCostArray = new double[] {.50, .75, 1.50, .50, .80};
    System.out.print("Enter the name of the flower purchased : ");
    flowerName = keyboard.next();
    while (! flowerName.equals("done"));{
        System.out.print("Enter the quantity: ");
        quantity = keyboard.nextInt();
        }
    for(int index = 0; index <5; index++){
        if(flowerNameArray[index].equals(flowerName));
        total = total + (flowerCostArray[index] * quantity);
    }
    System.out.println("Enter the name of the flower purchased: ");
    flowerName = keyboard.next();
    System.out.print("Total cost of the flower sale: $" + total);
}
}


但除非我先打字,否则它不会工作。我需要把它放在那里,这样用户就可以输入它退出程序。

你太复杂了。一步一个脚印。首先,在用户输入的flowers数组中找到索引。然后,在该索引处返回cost数组中的项目。考虑到这显然是一个家庭作业,一点解释可能比代码更有用。