英文:
Printing out Savings the second time
问题
public class Gob {
public String position;
public int age;
public double saving;
public Gob(double d) {
saving = d;
position = "Teacher";
age = 55;
}
public void printPosition() {
System.out.println("My position is a " + position);
}
public void printSaving() {
System.out.println("My saving is " + saving);
}
public void addSaving(double e) {
saving += e;
}
public static void main(String args[]) {
Gob h = new Gob(2531.50);
h.printPosition();
h.printSaving();
h.addSaving(25000); // Adding 25,000 to Mr. Gob's saving
h.printSaving();
}
}
英文:
So I already have coded mostly everything but I need help with one part which is all the way at the bottom of the code. So I made an object with a saving of 2531.5 so i can print that out as the initial saving of Mr. Gob's but then you have to add 25,000 to Mr. Gob's savings and print it out again and it prints out the same thing as the first one which is 2531.5. The output needs to come out like this https://gyazo.com/631227169fa677fa389fd029db146ed4 . I just confused on how Im suppose to do task #6 and don't know what I'm doing wrong.
public class Gob
{
//The following are IF/IV
public String position;
public int age;
//task #1 create an Instance Field "saving" as type double
public double saving;
//your work here
public Gob(double d){
//task #2 initialize the saving to user input
saving = d;
//your work here
position = "Teacher";
age = 55;
}
//task #2 create a method printPosition() to print out position
//in the following format
// "My position is a _______"
public void printPosition(){
System.out.println("My position is a " + position);
}
//task #3 create a method to print out saving
//you need to create a whole new method
public void printSaving(){
System.out.println("My saving is "+ saving);
}
//task #4 complete the method below
//add the input amount to the saving you have
public void addSaving(double e){
e = saving;
}
public static void main(String args[]){
//Create an new object with saving = 2531.50
Gob h = new Gob(2531.50);
h.printPosition(); //this is the name of the method
//Task #5 print out the initial saving of Mr. Gob
//your work here
h.printSaving();
//Task #6 add 25,000 to Mr. Gob saving and then
//print the saving again
//your work here
h.printSaving();
}
}
答案1
得分: 2
方法更正如下:
public void addSaving(double e){
this.saving += e;
}
在第二次调用printSaving
之前,需要调用addSaving
方法,这将实际上累加到旧的存款金额中。例如:
h.addSaving(500.0);
h.printSaving();
英文:
Two things :
1 : correct the below method.
You are not adding the variable to the class' reference variable (saving)
public void addSaving(double e){
// e = saving; **should be as below**
this.saving += e;
}
2 : you need to call addSaving(--some double value--) before calling the printSaving for the second time which will actually add up to the old saving.
maybe
h.addSaving(500.0);
h.printSaving();
答案2
得分: 1
public void addSaving(double e){
e = saving;
}
在这种情况下,您将字段要修改的当前值分配给传递的参数。应该反过来,您应该将这些值相加,而不是用一个替换另一个。所以,像这样:
public void addSaving(double e){
this.saving = this.saving + e;
}
此外,在您的 main()
方法中,我没有看到您调用 addSaving()
。如果您还没有这样做,您也需要这样做。
英文:
public void addSaving(double e){
e = saving;
}
When you do this you are assigning the current value of the field you want to modify to the parameter you are passing. It should be the other way around, and you should add the values, not replace one with the other. So, like this:
public void addSaving(double e){
this.saving = this.saving + e;
}
Also in your main()
method I don't see you calling addSaving()
. You'll need to do that too if you aren't already.
答案3
得分: 1
Your code for public void addSaving(double e) won't add e to the running total of this.saving. You need to change public void addSaving(double e) to be
public void addSaving(double e) {
this.saving += e;
}
and then call h.addSaving(25000.00); after which you can print the savings amount again using your print method.
英文:
Your code for public void addSaving(double e) won't add e to the running total of this.saving. You need to change public void addSaving(double e) to be
public void addSaving(double e) {
this.saving += e;
}
and then call h.addSaving(25000.00); after which you can print the savings amount again using your print method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论