Java 决定使用哪种类型的数组
作为一个整体,我对java编程和对象编程都是新手,我对使用哪种类型的数组感到困惑 我需要创建一个数组,该数组接受用户输入的名称和价格,并将其存储在数组中。我试着制作两个独立的阵列,并使用计数器,但它不适合我的需要。问题在于,我需要能够分别平均价格,以及在输入特定名称时反转数组并过滤数组。我还尝试创建一个对象数组,我不确定这是否合适 这是我的密码:Java 决定使用哪种类型的数组,java,arrays,Java,Arrays,作为一个整体,我对java编程和对象编程都是新手,我对使用哪种类型的数组感到困惑 我需要创建一个数组,该数组接受用户输入的名称和价格,并将其存储在数组中。我试着制作两个独立的阵列,并使用计数器,但它不适合我的需要。问题在于,我需要能够分别平均价格,以及在输入特定名称时反转数组并过滤数组。我还尝试创建一个对象数组,我不确定这是否合适 这是我的密码: String names[] = new String[100]; // sets array size to almost ulimited #
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");
}
我不一定要找人为我做这件事或诸如此类的事,我只是被难住了,我在寻找我的逻辑应该朝哪个方向思考。提前谢谢大家 首先,最好创建一个包含Name和Price字段的类 现在创建该类的ArrayList
ArrayList<Product> myProduct = new ArrayList();
然后在此arraylist上执行所有过程。首先,最好创建一个包含Name和Price字段的类 现在创建该类的ArrayList
ArrayList<Product> myProduct = new ArrayList();
在这个arraylist上完成所有的过程。我认为你的循环基本上是好的,只是我做了一些事情使它工作。 -把它放在一个循环中 -如果平均数是真的,那么平均数是正确的,只是在某种程度上被认为是错误的
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
}
我认为你的循环基本上是好的,只是我做了一些事情使它工作。 -把它放在一个循环中 -如果平均数是真的,那么平均数是正确的,只是在某种程度上被认为是错误的
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
}
我需要能够平均价格分开。。。这是什么意思?你的代码真的有问题吗?或者你只是在寻找代码审查?如果这让人困惑,很抱歉,基本上我需要能够将价格和名称存储在单个数组中,但是能够通过一个方法平均价格。我尝试将其存储在对象数组中,但无法从对象数组中平均价格。为什么是数组?你可以使用映射,或者如果你只想使用数组,你可以使用2D数组,比如{{item1123},{item2345}}@AnilAgrawal,在我上的java课程中,我们应该对数组有一个全面的了解。老实说,我还没有学会地图。我需要能够分别平均价格。。。这是什么意思?你的代码真的有问题吗?或者你只是在寻找代码审查?如果这让人困惑,很抱歉,基本上我需要能够将价格和名称存储在单个数组中,但是能够通过一个方法平均价格。我尝试将其存储在对象数组中,但无法从对象数组中平均价格。为什么是数组?你可以使用映射,或者如果你只想使用数组,你可以使用2D数组,比如{{item1123},{item2345}}@AnilAgrawal,在我上的java课程中,我们应该对数组有一个全面的了解。老实说,我还没有学会地图。谢谢,这只是我测试课的一部分。我还没有真正使用ArrayList,但我现在将研究它们。再次感谢。谢谢,这只是我考试课上的一部分。我还没有真正使用ArrayList,但我现在将研究它们。再次感谢。谢谢你的帮助,我会试试这个。谢谢你的帮助,我会试试这个。