-5

我正在做一个有教师限制的程序。我们只允许使用 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);
4

1 回答 1

0

Assuming you have some car object, you will need to keep these cars in an ordered data structure of some type. Either an array or a List will work. Once you have them in this data structure, you can iterate through the items, and make your various comparisons for speed and use cost.

psuedocode:

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

Now your fastest car is assigned to fastest, your cheapest is assigned to cheap, etc

于 2013-09-12T19:59:46.303 回答