+ 2

Hi guys, Im new here and i want a help to my java program. I want to 0,5,6,7, will have a value of 1 and the others are 2.

while(number != 0) { int remainder = number % 10; if(remainder == 0 || remainder == 6 || remainder == 7) remainder = 1; else { remainder = 2; } total += remainder; number = number/10; } return total;

15th Sep 2021, 10:16 AM
F John
F John - avatar
18 Réponses
+ 1
F John I think so that's because when it reaches to the 0 of 012 the condition of while loop becomes true and it doesn't add 1. Use 102 for it
15th Sep 2021, 2:41 PM
Atul [Inactive]
+ 2
I've already suggested you to take input as a string. If you are restricted to take numeric input, then you can convert the number to string before you proceed with the sum. https://code.sololearn.com/c5wyojG9w0hV/?ref=app
16th Sep 2021, 6:47 AM
Ipang
+ 1
So basically you want to sum all the digits in given number, with each digit incremented by 1. Is that it? BTW, seeing you take int input for <num>, did you know that a zero prefix in integer is read in as octal number (base 8)? taking your example, 012 (octal) is 10 (decimal). You might get through this if you take input as string.
15th Sep 2021, 2:13 PM
Ipang
+ 1
Thank you . 😊 Yes but only numbers 0,5,6,7 have a value of 1 then the other numbers will have a value of 2. Then add the all digits . I dont how to start your request can you give some example or expand my program? Plsss
15th Sep 2021, 2:42 PM
F John
F John - avatar
15th Sep 2021, 2:47 PM
Atul [Inactive]
+ 1
See it will even work without using getters and setters too
15th Sep 2021, 2:48 PM
Atul [Inactive]
+ 1
Ipang THANK YOU FOR YOUR COOPERATION THIS WHAT I WANT. THANK YOU VERY MUCH 😊😍
16th Sep 2021, 11:47 AM
F John
F John - avatar
+ 1
F John Welcome. Please don't call me sir
16th Sep 2021, 4:20 PM
Atul [Inactive]
0
Can you please send the full program?
15th Sep 2021, 11:53 AM
Atul [Inactive]
0
public class Buzzer { private int total; private int num; public Buzzer(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return this.num; } public int Method(){ while(num != 0) { int remainder = num % 10; if(remainder == 0 || remainder == 5 || remainder == 6 || remainder == 7) { remainder = 1; } else { remainder = 2; } total += remainder; num = num/10; } return total; } } //This is my whole program. I hope you can help me. For school purposes. // the error here in my program is when i put the zero in the first place . Like 012 the correct output is 5 because 0=1 , 1= 2 and 2 = 2 then add them all. Its 5 but if i run it the result is 4.
15th Sep 2021, 12:05 PM
F John
F John - avatar
0
public class Buzzer { private int total; private int num; public Buzzer(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return this.num; } public int Method(){ while(num != 0) { int remainder = num % 10; if(remainder == 0 || remainder == 5 || remainder == 6 || remainder == 7) { remainder = 1; } else { remainder = 2; } total += remainder; num = num/10; } return total; } } //This is my whole program. I hope you can help me. For school purposes. // the error here in my program is when i put the zero in the first place . Like 012 the correct output is 5 because 0=1 , 1= 2 and 2 = 2 then add them all. Its 5 but if i run it the result is 4. public class Program { public static void main(String[] args) { Buzzer sc= new Bu
15th Sep 2021, 2:46 PM
Atul [Inactive]
0
Uhmm. Thank you for your effort sir . But i tried this. The only problem is when i put zero in the first place it code not read the value of 0 which is 1 .
15th Sep 2021, 2:55 PM
F John
F John - avatar
0
Umm. The output is different for 012 and 12. Are you sure that in every case 0 should remain in the first?
15th Sep 2021, 3:17 PM
Atul [Inactive]
0
Now you check the code all your doubts will be cleared
15th Sep 2021, 3:21 PM
Atul [Inactive]
0
Thank you for your cooperation sir but the output of 012 is 5 .
15th Sep 2021, 11:22 PM
F John
F John - avatar
0
Ok now you check
16th Sep 2021, 6:05 AM
Atul [Inactive]
0
F John Please check my code too
16th Sep 2021, 1:58 PM
Atul [Inactive]
0
Atul [Inactive] WOW its also good what a nice code 😊😊 THANK YOU VERY MUCH FOR HELPING ME SIR. 😊 you give idea 😊
16th Sep 2021, 2:29 PM
F John
F John - avatar