所以我正在制作一个模仿真正的自动售货机的程序,我遇到了一些麻烦。这是该计划的标准。必须,
我已经弄清楚如何将价格放在一个数组中并显示它们,但这就是我所得到的。请帮忙!我的代码可以在下面看到!
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class Vending2
{
public static void main(String[] args) throws FileNotFoundException
{
System.out.print("Enter your food selection file: "); // User inputs file
Scanner input = new Scanner(System.in); // Keyboard input from user
String filename = input.nextLine();
Scanner fs = new Scanner(new File(filename)); // Scans in the file that was inputed
String line;
double price;
int num = 0;
while(fs.hasNextLine()){
line = fs.nextLine();
price = Double.parseDouble(line.split(" ")[0]);
num++;
}
fs.close();
fs = new Scanner(new File(filename));
double[] value = new double[num]; //Array for prices
int i = 0;
while(fs.hasNextLine()){
line = fs.nextLine();
price = Double.parseDouble(line.split(" ")[0]);
value[i] = price;
i++;
}
System.out.println(Arrays.toString(value));
}
}
以下是我正在阅读的文件:
1.00 Honey roasted peanuts
1.50 Cheetos
1.50 Bugles
2.00 Synder’s Pretzels
1.00 Snickers
1.00 Twix
1.25 M n Ms
.75 Life savers
1.00 Twizzlers
1.00 Nutter Butters
1.00 Butter Fingers
1.50 King Size Kit Kats
1.25 Carrot sticks
.50 Juicy Fruit
.50 Spearmint Gum
0.50 Five gum
3.50 Pepperoni
1.75 Cheez-Its
.25 Slim Jim
1.50 Lays Barbeque Chips
答案 0 :(得分:0)
1)创建一个名为FoodItem的类,其中包含名称和价格
2)用“”分隔文件中的行并填充名称&价
3)将FoodItem添加到foodItemsList
4)遍历List并打印值。
我使用该列表假设食品名称中可能存在重复项。如果没有,请将List更改为HashMap。
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class Vending2 {
public static void main(String[] args) throws FileNotFoundException {
System.out.print("Enter your food selection file: "); // User inputs
// file
Scanner input = new Scanner(System.in); // Keyboard input from user
String filename = input.nextLine();
Scanner fs = new Scanner(new File(filename)); // Scans in the file that
// was inputed
String line;
double price;
List<FoodItem> foodItemsList = new ArrayList<FoodItem>();
while (fs.hasNextLine()) {
line = fs.nextLine();
String[] itemsWithPrice;
itemsWithPrice = line.split(" ");
if (itemsWithPrice.length > 1){
price = Double.parseDouble(itemsWithPrice[0]);
String name="";
for (int i=1; i<itemsWithPrice.length;i++){
name += itemsWithPrice[i];
}
foodItemsList.add(new FoodItem(name,price));
}
}
System.out.println("Food List");
for (int i=0; i< foodItemsList.size(); i++){
System.out.println(foodItemsList.get(i));
}
}
}
class FoodItem{
String name;
double price;
public FoodItem(String name, double price){
this.name = name;
this.price = price;
}
public String toString(){
return new StringBuffer().append("name:").append(name).append(":").append("price:").append(price).toString();
}
}
输出:
输入您的食物选择文件:v1.txt
食物清单
name:Honeyroastedpeanuts:price:1.0
name:Cheetos:price:1.5
name:Bugles:price:1.5
name:SynderÆsPretzels:price:2.0
name:Snickers:price:1.0
name:Twix:price:1.0
name:MnMs:price:1.25
name:Lifesavers:price:0.75
name:Twizzlers:price:1.0
name:NutterButters:price:1.0
name:ButterFingers:price:1.0
name:KingSizeKitKats:price:1.5
name:Carrotsticks:price:1.25
name:JuicyFruit:price:0.5
name:SpearmintGum:price:0.5
name:Fivegum:price:0.5
name:Pepperoni:price:3.5
name:Cheez-Its:price:1.75
name:SlimJim:price:0.25
name:LaysBarbequeChips:price:1.5