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