英文:
Unable to swap out double for void as errors follow
问题
抱歉,标题可能过于模糊,但是我不太确定该如何表达。每次我将"double"替换为"void",都会出现一个错误,告诉我程序的退出值不正确。这也全部发生在CodeLab上,因此一切都必须清晰而精确。
我的任务是:
编写一个名为GasTank的类,其中包含:
一个名为amount的实例变量,类型为double,初始化为0。
一个名为addGas的方法,接受一个类型为double的参数。将amount实例变量的值增加参数的值。
一个名为useGas的方法,接受一个类型为double的参数。amount的值减去参数的值。然而,如果amount的值减少到小于0,则将amount设置为0。
一个名为isEmpty的方法,不接受参数。isEmpty返回一个布尔值:如果amount的值小于0.1,则为true,否则为false。
一个名为getGasLevel的方法,不接受参数。getGasLevel返回amount实例变量的值。
public class GasTank {
private double amount=0;
public void addGas(double addg){
amount=amount+addg;
if(amount>0.1){
amount=0;
}
}
public void useGas(double useg){
amount=amount-useg;
if(amount<0){
amount=0;
}
}
public boolean isEmpty(){
if(amount<0.1){
return true;
}
return false;
}
public double getGasLevel(){
return amount;
}
}
英文:
I apologize if the title is extremely vague however, i'm not entirely sure how else to word it. Every time I swap out double for void, I get an error in which tells me that my program's exit value is incorrect. It's all on CodeLab as well so everything has to be clear and precise.
My assignment is to:
Write a class named GasTank containing:
An Instance variable named amount of type double, initialized to 0.
A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter.
A method named useGas that accepts a parameter of type double. The value of the amount is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0.
A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise.
A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable.
public class GasTank {
private double amount=0;
public double addGas(double addg){
amount=amount+addg;
if(amount>0.1){
amount=0;
}
}
public void useGas(double useg){
amount=amount-useg;
if(amount<0){
amount=0;
}
}
public boolean isEmpty(){
if(amount<0.1){
return true;
}
return false;
}
public double getGasLevel(){
return amount;
}
}
答案1
得分: 0
让我们只调试你的代码:我们逐步进行,弄清楚应该发生什么。通过运行代码来检查你的工作。使用调试器来观察计算机的操作。如果你懒得学习如何使用调试器,那么 println 语句是一个不好的替代品,但对于初学者来说已经足够好了:
public double addGas(double addg){
amount=amount+addg;
if(amount>0.1){
amount=0;
}
}
所以,我们获取 amount
字段,按照指定的方式添加燃料..
然后如果 amount
大于 0.1,我们将其设置回到 0。
因此,正如图像所示,检查器代码添加了 78 加仑,由于这超过了 0.1,你的代码将会将 amount
设置为 78.0,然后立即将其降低为 0.0。我不知道你为什么会添加那行代码,但计算机只会执行它被告知的操作。而你告诉它执行的操作不符合任务要求。之后,该字段应该是 78.0。
我完全不明白你提到的:
无法将 double 替换为 void,因为会出现错误
但整个“如果尝试添加超过十分之一加仑,则将 amount
重置为 0”的问题是你代码中唯一的问题。
英文:
Let's just debug your code: We go through it step by step, figure out what is supposed to happen. Doublecheck your work by running the code. Use a debugger to witness what the computer is doing. If you can't be bothered to learn how to use one, well, println statements are a crappy standin and works well enough for beginning programmers:
public double addGas(double addg){
amount=amount+addg;
if(amount>0.1){
amount=0;
}
}
So, we take the amount field, add the gas to it as specified..
and then if amount is above 0.1, we set it back down to 0.
So, as the image shows, the checker code adds 78 gallons, and as that's more than 0.1, your code will set amount
to 78.0 and then IMMEDIATELY sets it back down to 0.0. I have no idea why you added that line, but computer just does what computer is told. And what you told it to do fails the assignment. the field should have been 78.0 afterwards.
I have absolutely no idea what you're on about with:
> Unable to swap out double for void as errors follow
But the whole 'I reset amount down to 0 if you try to add more than a tenth of a gallon' thing is the only problem with your code.
答案2
得分: 0
我已经为您修复了。请在注释中查看说明。
public class GasTank {
private double amount = 0;
private double v;
// 构造方法
public GasTank(double a){
v = a;
}
// 已更改为 void,我们不返回 double 值
public void addGas(double addg){
amount = amount + addg;
// 已更改您的 if 语句
if(amount > v){
amount = v;
}
}
public void useGas(double useg){
amount = amount - useg;
if(amount < 0){
amount = 0;
}
}
public boolean isEmpty(){
if(amount < 0.1){
return true;
}
// 在这里添加了 else 语句
else{
return false;
}
}
public double getGasLevel(){
return amount;
}
}
英文:
I fixed it for you. Check the notes in the comments
public class GasTank {
private double amount=0;
private double v;
//Constructor
public GasTank(double a){
v = a;
}
//Changed to void, we aren't returning a double val
public void addGas(double addg){
amount=amount+addg;
//changed your if statement
if(amount>v){
amount = v;
}
}
public void useGas(double useg){
amount=amount-useg;
if(amount<0){
amount=0;
}
}
public boolean isEmpty(){
if(amount<0.1){
return true;
}
//added else statement here
else{
return false;
}
}
public double getGasLevel(){
return amount;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论