我刚学习数组。我有一个小程序要写,要求创建2个数组。一个拿着5个鲜花的名字。一个拿着那5朵花的价格。我问用户他们想要什么样的花和数量。然后,我需要创建一个定位花名称的循环,并使用该索引来查找花的成本。我正在努力研究如何在代码中编写如何使用一个数组索引来查找另一个数组索引。示例:Roses和$ .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: ");
答案 0 :(得分:1)
就像亨利上面说的那样,只需循环遍历花阵并找到它的索引。该指数与成本数组中的成本相对应。当匹配时,在成本数组中打印该索引处的元素。
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]);
}
}
答案 1 :(得分:1)
使用类似的东西
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.
}
}
答案 2 :(得分:0)
根据我的理解,花名存储在一个数组中,它们的价格存储在同一索引的另一个数组中。
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
}
答案 3 :(得分:0)
这里有一个关于我过去常常工作的快速演练。
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
}
}
所以花朵阵列和价格数组基本上是一对一的,这意味着你可以在任何一朵花和相应的价格之间画一条清晰的线。由于存在这种关系,因此在迭代数组时可以使用相同的索引。例如:三色堇 - &gt; 0.75
假设我们在循环中i = 1
...
如果我们为我们正在寻找的类型输入“pansy”,我们将input.equals(flowers[i])
,这是真的"pansy".equals("pansy")
。那么我们将跳到代码System.out.print(cost[i]);
,这句话就是System.out.print(.75);
,因为我是1。
答案 4 :(得分:0)
经过大量的工作,我有了这个。
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);
}
}
但除非我先输入,否则它不会起作用。 (我需要在那里,所以用户可以输入它以退出程序。)