0

I have a problem in my behavior method. Because my while condition is not reading the value of 0 if i put it in the first place.

public class Buzzers { private int num; private int total; public Buzzers() { } public Buzzers(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return this.num; } //Method public int Behavior(){ 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; } }

15th Sep 2021, 7:15 AM
Redo
8 odpowiedzi
+ 2
You should not read the input as INT, if you want to process a leading "0". I would prefer a String for that.
15th Sep 2021, 8:26 AM
Coding Cat
Coding Cat - avatar
+ 1
Coding Cat [sleeps on Java] okey i will try it tnx , nothing's wrong with my Behavior? Hmm
15th Sep 2021, 8:32 AM
Redo
+ 1
Redo you're welcome
15th Sep 2021, 8:35 AM
Coding Cat
Coding Cat - avatar
+ 1
Redo here a first version with input as String for leading zeros. Next I will do this with a more simple comparisation. https://code.sololearn.com/cbk1PP6Be218/?ref=app
16th Sep 2021, 10:11 PM
Coding Cat
Coding Cat - avatar
0
I have checked by not setting num value using constructor, by default num value is 0. Check the snippet https://code.sololearn.com/c10zZX0G524J/?ref=app
15th Sep 2021, 7:32 AM
Parith (Faree)
Parith (Faree) - avatar
0
Parith (Faree) import java.util.Scanner; public class Main{ public static void main(String[] args){ int num; char choice; Scanner in = new Scanner(System.in); do { System.out.println("Enter a number:"); num = in.nextInt(); while(num <=0){ System.out.println("--Invalid number--"); System.exit(0); } Buzzers b = new Buzzers(num); System.out.println("Output is : "+b.Behavior()); System.out.println("Do you want to continue?(Yes/No)"); choice = in.next().charAt(0); } while(choice =='y'||choice == 'Y'); in.close(); } }
15th Sep 2021, 7:46 AM
Redo
0
Parith (Faree) If i try to run it and inputs 012 expected output is 5 but mine is 4 hmm 🤔 can you help me public class Buzzers { private int num; private int total; public Buzzers() { } public Buzzers(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return this.num; } //Method public int Behavior(){ 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; } }
15th Sep 2021, 7:47 AM
Redo
0
Redo maybe you want to have a look at this one. It looks better: https://code.sololearn.com/c82Sx28d87hZ/?ref=app
17th Sep 2021, 6:25 AM
Coding Cat
Coding Cat - avatar