创建包含多个值的扫描程序

时间:2013-09-12 19:49:45

标签: java

我正在做一个有教师限制的程序。我们只允许使用String,NumberFormat和Scanner类。我的程序要求将汽车信息插入计算器中,以后将与其他汽车进行比较。我的问题不是代码本身,而是如何在不使用数组函数的情况下创建数组。 “if”声明中的信息520.2 =总里程(英里),70 =车辆速度,15 =填充油箱的分钟数,3.59美元是每加仑的价格。我只想了解如何在扫描仪中保存多个值,或者如果您以不同的方式执行此操作。

int i;
    String make, model;
    double cityMPG, hwyMPG, tanksize, tankdistance = 0, fillups = 0;
    float totalcost = 0; 
    int time = 0;

    do {
        System.out.println("Please enter your cars make"); //asking for cars make
        make  = keyboard.next();

        System.out.println("Enter your cars model"); // asking for cars model
        model = keyboard.next();

        System.out.println("Enter your gas tank size"); // asking for gas tank size
        tanksize = keyboard.nextDouble();

        System.out.println("Enter your city MPG"); // asking for City MPG
        cityMPG = keyboard.nextDouble();

        System.out.println("Enter your highway MPG"); // asking for Highway MPG
        hwyMPG = keyboard.nextDouble();

        System.out.println("if you want to add additional cars press (1)" );
         i = keyboard.nextInt();
        if (i == 1) {
            tankdistance = 520.2 / hwyMPG;
            fillups = (int) ((tankdistance / tanksize));
            time = (int) ((fillups * 15) + (( 520.2 / 70 ) * 60));
            totalcost = (float) ((fillups * tanksize) * 3.59);

1 个答案:

答案 0 :(得分:0)

假设你有一些汽车对象,你需要将这些汽车保持在某种类型的有序数据结构中。数组或列表都可以使用。在这个数据结构中使用它们之后,您可以遍历这些项目,并对速度和使用成本进行各种比较。

伪码:

instantiate each car, and store in some data structure called cars

create empty car references for cheap, expensive, fastest, slowest

for each car in cars
  if car faster than fastest
    set fastest = car
  if car slower than slowest
    set slowest = car
  if car cheaper than cheap
    set cheap = car
  if car more expensive that expensive
    set expensive = car
end for loop

现在你的最快车被分配到最快,你最便宜的车被分配到廉价等等