我是java编程以及整个对象编程的新手,我已经厌倦了使用哪种类型的数组。
我需要创建一个接受NAME和PRICE用户输入的数组,并将其存储在数组中。我尝试制作两个独立的阵列并使用计数器,但它不适合我的需要。问题在于我需要能够单独平均PRICE以及反转数组并在输入特定名称时过滤数组的问题。我也试过创建一个对象数组,我不确定这是否合适。
这是我的代码:
String names[] = new String[100]; // sets array size to almost ulimited #
double prices[] = new double[100]; // sets array size to almost unlimited #
double average = 0; // initializes the array
int count = 0; // initializes the count for the arraysize
boolean flag = false;// initializes flag to false so it can be changed later
while(true){
System.out.print("Enter item " + (count + 1) + " name: "); // enter name
names[count] = in.next(); // next string becomes count index
if(names[count].equals("-1")){ break;} // if the next name entered is sentinel break
System.out.print("Enter item " + (count + 1) + " price: "); // enter price
prices[count] = in.nextDouble(); // stores price entered as array index
// if next price == -1 then the program ends
if(names[count].equalsIgnoreCase("peas")) flag = true;
average += prices[count];
count++;
}
System.out.println("The items and their prices are: ");
for(int i = 0; i < count; i++){
System.out.println(names[i] + ": " + formatter.format(prices[i]));
}
average /= count;
if(flag == true){
System.out.println("Average: " + formatter.format(average));
}
else{
System.out.println("no average output");
}
我不一定在寻找某人为我或类似的事情做这件事,我只是难过而且正在寻找我的逻辑应该考虑的方式。提前谢谢大家!
答案 0 :(得分:4)
首先,您最好创建一个具有Price
和public class Product {
String name, price;
//Getter and Setter
// You can also define some other methods here as well.
}
字段的类。
ArrayList
现在创建该类的ArrayList<Product> myProduct = new ArrayList();
。
{{1}}
并对此arraylist进行所有处理。
答案 1 :(得分:1)
我认为你的循环基本上是好的,只是为了让它起作用我做了一些事情。 - 在一个循环中输出所有内容 - 如果平均值为真,则排除&#34;&#34;平均值是正确的只是某种程度上被认为是假的
String names[] = new String[5]; // sets array size to almost unlimited #
double prices[] = new double[100]; // sets array size to almost unlimited #
double average = 0; // initializes the array
int count = 0; // initializes the count for the arraysize
boolean flag = false;// initializes flag to false so it can be changed later
while(true){
System.out.print("Enter item " + (count + 1) + " name: "); // enter name
names[count] = System.console().readLine();//in.next(); // next string becomes count index
if(names[count].equals("-1")){ break;} // if the next name entered is sentinel break
System.out.print("Enter item " + (count + 1) + " price: "); // enter price
prices[count] = Double.valueOf(System.console().readLine());//in.nextDouble(); // stores price entered as array index
// if next price == -1 then the program ends
if(names[count].equalsIgnoreCase("peas")) flag = true;
average += prices[count];
count++;
// }//deleted
System.out.println("The items and their prices are: ");
for(int i = 0; i < count; i++){
System.out.println(names[i] + ": " + prices[i]);//formatter.format()
// }//deleted
average /= count;
//if(flag == true){
System.out.println("Average: " + average);//formatter.format()
//}
else{
System.out.println("no average output");
}//added
}//added
}