- 2

help in java

can i do -- in getNumOfChanse() like getNumOfChanse()--; ? because it's gave an error

13th Oct 2022, 6:06 AM
Dee
Dee - avatar
14 Answers
+ 2
What does it mean to decrement a function? I'm not sure what do you want to do, but let's say getNumOfChanse() is a function that returns a integer and you want to decrement the value and update it, you can do it either calling another function that already gives you decremented and update it or you subtract by one and call a function to update the value. You can't increment or decrement a function because this doesn't work in values, only on variables, so I can't write 1++ but I can write a = 1; a++. I hope I could help and Keep the good coding :)
13th Oct 2022, 7:25 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
+ 1
No. The ++/-- is unary operator can be applied to a variable only. Not to any constant. getNumOfChanse() -- is works as ( getNumOfChanse() ) -- ; and the method returns a constant value : a number. You can't apply ++/-- to a constant. It's error. store return value of a method the apply decrementation and return value.
13th Oct 2022, 7:46 AM
Jayakrishna 🇮🇳
0
i did puplic int des(int a) return getNumOfChanse()--; its gava me an error also
13th Oct 2022, 7:31 AM
Dee
Dee - avatar
0
i have public void setNofC() { this.NofC=5 ; } public int getNoC() { return NofC; then i want to call getNofc() to do -- decremnt
13th Oct 2022, 7:34 AM
Dee
Dee - avatar
0
Do this instead: public int decrNoC() { this.Nofc--; return Nofc; }
13th Oct 2022, 7:46 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
0
can i do it with calling get() ? cuz i don want to use Notc when i have a methode to return it
13th Oct 2022, 7:49 AM
Dee
Dee - avatar
0
It depends what do you want the get() to do. But I would advise you to do the following: public void init() { Nofc = 5; } public int set(int x) { Nofc += x; return Nofc }
13th Oct 2022, 7:53 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
0
Now you just need to call set(-1)
13th Oct 2022, 7:56 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
0
is the there get() ? cuz i need it
13th Oct 2022, 8:00 AM
Dee
Dee - avatar
0
public int get() { return Nofc; }
13th Oct 2022, 8:00 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
0
can i use it in set(int x ) ? insted of Nofc
13th Oct 2022, 8:06 AM
Dee
Dee - avatar
0
Yeah but it will be slower... If your reeeeeeaally want to use it then: public int set(int x) { Nofc += x; return get(); } I can't see the point of doing this, since you are just making the computer to do extra steps...
13th Oct 2022, 8:12 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
0
Т
13th Oct 2022, 12:07 PM
Алексей Буров
Алексей Буров - avatar
0
I don't think so because it's going to be slower than the first one.
14th Oct 2022, 9:43 AM
Sanya Sanskriti
Sanya Sanskriti - avatar