所以我对Java很陌生,几个月前才开始为一门课程编程。我完全和完全坚持这个项目。 This is the project.
到目前为止,这是我的代码。
double mile;
double gas;
int week;
double mpg;
double totalMile;
double totalGas;
week = 0;
gas = 0;
mile = 0;
totalMile = 0;
totalGas = 0;
Scanner reply = new Scanner(System.in);
System.out.println("Please enter the amount of miles.");
mile = reply.nextDouble();
while(mile != -99)
{
week++;
System.out.println("Please enter the amount of gas (in gallons) purchased for this week.");
gas = reply.nextDouble();
mpg = mile / gas;
System.out.println("Week: "+week+"");
System.out.println("Miles: "+mile+" Vacation Miles Traveled: "+totalMile+"");
System.out.println("Gallons: "+gas+" Vacation Gallons Purchased: "+totalGas+"");
System.out.println("MPG: "+mpg+"");
System.out.println("Please enter the amount of miles.");
mile = reply.nextDouble();
}
我真正坚持的唯一部分是如何获得“假期里程/气体”值,我不知道如何在while循环中添加相同的变量。
答案 0 :(得分:0)
假设您尝试保持总计,请尝试totalmile += reply.nextDouble();
What this does是totalmile = totalmile + reply.nextDouble();
答案 1 :(得分:0)
我假设您想要记录从一开始就驾驶的所有里程?
为什么不在while循环中使用总里程或总加仑变量?
totalmile += mile
totalgas += gas
您可能还想移动
System.out.println("Please enter the amount of miles.");
mile = reply.nextDouble();
所以你的代码看起来像是:
week++;
System.out.println("Please enter the amount of gas (in gallons) purchased for this week.");
gas = reply.nextDouble();
mpg = mile / gas;
System.out.println("Please enter the amount of miles.");
mile = reply.nextDouble();
totalgas += gas
totalmiles += mile
System.out.println("Week: "+week+"");
System.out.println("Miles: "+mile+" Vacation Miles Traveled: "+totalmile+"");
System.out.println("Gallons: "+gas+" Vacation Gallons Purchased: "+totalgas+"");
System.out.println("MPG: "+mpg+"");
所以是的。 祝你的编程工作顺利!