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; } }
8 Answers
+ 2
You should not read the input as INT, if you want to process a leading "0".
I would prefer a String for that.
+ 1
Coding Cat [sleeps on Java] okey i will try it tnx , nothing's wrong with my Behavior? Hmm
+ 1
Redo you're welcome
+ 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
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
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();
}
}
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;
}
}
0
Redo maybe you want to have a look at this one. It looks better:
https://code.sololearn.com/c82Sx28d87hZ/?ref=app