public String getValue(int n)
{
if (n % ham == 0 || n % spam==0)
{
if(n % ham == 0 && n % spam == 0)
{
return "hamspam";
}
else if(n % ham == 0 && n % spam != 0)
{
return "ham";
}
else if(n % ham != 0 && n % spam==0)
{
return "spam";
}
}
else
{
return Integer.toString(n);
}
}
Logically, we may conclude that if n is a multiple of ham or of spam, then either n is a multiple of both, or of exactly one of the two. We can logically conclude that there is no way that inside the outer if, at least one of the 3 conditions inside will be true and a return will be executed.
But the Java compiler is not that smart. It just sees no return past the last else if in the block, and concludes that there is an execution path that doesn't have a return, so it gives the compilation error.
Logically, if the first 2 conditions are false, given the outer if, the third condition must be true.
Replace
else if(n % ham != 0 && n % spam==0)
with
else
It is logically equivalent, and the compiler will also be satisfied that every execution path has a return statement.
public String getValue(int n)
{
if (n % ham == 0 || n % spam==0)
{
if(n % ham == 0 && n % spam == 0)
{
return "hamspam";
}
else if(n % ham == 0 && n % spam != 0)
{
return "ham";
}
else if(n % ham != 0 && n % spam==0)
{
return "spam";
}
// this return is missing
return "something";
}
else
{
return Integer.toString(n);
}
}
In your first if statement, you need an else clause or a return value.
To elaborate, what if:
if (n % ham == 0 || n % spam==0) //is true
if(n % ham == 0 && n % spam == 0) // is false
{
return "hamspam";
}
else if(n % ham == 0 && n % spam != 0) //is false
{
return "ham";
}
else if(n % ham != 0 && n % spam==0)//is false
{
return "spam";
}
Related
I was doing a simple fizzbuzz problem in Java and encountered a bug that I cannot find for the life of me. The "fizz" is not added to the result unless the number is also divisible by 5.
actual: ["1","2","3","4","buzz","6","7","8","9","buzz","11","12","13","14","fizzbuzz"]
expected: ["1","2","fizz","4","buzz","fizz","7","8","fizz","buzz","11","fizz","13","14","fizzbuzz"]
public List<String> fizzBuzz(int n) {
List<String> result = new ArrayList<>();
for (int i = 1; i <= n; i++) {
String word = "";
if (i % 3 == 0) {
word += "fizz";
} if (i % 5 == 0) {
word += "buzz";
} else {
word = String.valueOf(i);
}
result.add(word);
}
return result;
}
You have two if block instead of one.
if (i % 3 == 0) {
word += "fizz";
} if (i % 5 == 0) {
word += "buzz";
} else {
word = String.valueOf(i);
}
In the above block first condition is if the i is divisible by 3 and immediately it will check if the i is divisible by 5 else it will execute the else part.
In case of i being divisible 3 and not divisible by 5 you will always get the i value.
for your requirement you could do as follows
if (i % 3 == 0 && i % 5 == 0) {
word = "fizzbuzz";
} else if (i % 3 == 0) {
word = "fizz";
} else if (i % 5 == 0) {
word = "buzz";
} else {
word = String.valueOf(i);
}
This way first you are check if the value is both divisible by 3 and 5 if not then divisible by 3 if not then divisible by 5 and finally else part.
public static List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<>(n);
for (int i = 1; i <= n; i++) {
boolean mulThree = i % 3 == 0;
boolean mulFive = i % 5 == 0;
if (mulThree && mulFive)
res.add("FizzBuzz");
else if (mulThree)
res.add("Fizz");
else if (mulFive)
res.add("Buzz");
else
res.add(String.valueOf(i));
}
return res;
}
My do-while loop is supposed to print out numbers from 1 - 106 (including 1 and 106)
whats supposed to happen:
multiples of 3 are supposed to print out Big,
multiples of 5 are supposed to print out Mean,
multiples of 7 are supposed to print out Bugs,
multiples of 3 and 5 are supposed to print out BigMean,
multiples of 3 and 7 are supposed to print out BigBugs,
multiples of 5 and 7 are supposed to print out MeanBugs,
multiples of 3, 5 and 7 are supposed to print out BigMeanBugs.
What actually happens:
my do-while loop only runs the first "if" statement. Any help?
My code:
public static void main(String [] args){
int i = 0;
do{
++i;
if(i %3 == 0){
System.out.print("Big, ");
if(i %5 ==0){
System.out.print("Mean, ");
}
if(i %7 ==0){
System.out.print("Bugs, ");
}
if((i %3 == 0) && (i %5 == 0)){
System.out.print("BigMean, ");
}
if((i %3 == 0) && (i %7 == 0)){
System.out.print("BigBugs, ");
}
}else if((i %5 == 0) && (i %7 == 0)){
System.out.print("MeanBugs, ");
}else if((i %3 == 0) && (i %5 == 0) && (i %7 ==0)){
System.out.print("BigMeanBugs, ");
}else{
System.out.print(i + " ");
}
}while(i<=106);
}//closing main
As I could see your problem is probably that your if-statements are inside of first if-statement, so maybe you want this:
public class BigMeanBugsDoWhileLoop
{
public static void main(String [] args)
{
int i = 0;
do
{
++i;
boolean bug = false;
String result = "";
if(i %3 == 0)
{
result += "Big";
bug = true;
}
if(i %5 ==0)
{
result += "Mean";
bug = true;
}
if(i %7 ==0)
{
result += "Bugs";
bug = true;
}
if(!bug)
{
System.out.println(i + "");
}
else
{
System.out.println(result);
}
}while(i<106);
}//closing main
} // closing class
You don't need other if-statements because you don't use break so other expressions will be evaluated before next iteration.
First, if something is divisible by 3 and 7 it is divisible by 21.
Second, if you reverse the order, you will catch divisible by 3 and 5 before divisible by 3 or 5 separately (assuming that is what you want). If you do it the other way you may print the values prematurely or miss them altogether before checking the other possible factors. This behavior is also dependent on if vs if/else if statements.
I have limited the printout to 25 to avoid a long list. Modify as you see fit.
public class BigMeanBugsDoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
String bugType = "";
if (i % 105 == 0) {
bugType = "BigMeanBugs"; // 3 5 7
} else if (i % 35 == 0) {
bugType = "MeanBugs"; // 5 7
} else if (i % 21 == 0) {
bugType = "BigBugs"; // 3 7
} else if (i % 15 == 0) {
bugType = "BigMean"; // 3 5
} else if (i % 7 == 0) {
bugType = "Bugs"; // 7
} else if (i % 5 == 0) {
bugType = "Mean"; // 5
} else if (i % 3 == 0) {
bugType = "Big"; // 3
}
System.out.println(bugType.isEmpty() ? i : bugType);
i++;
} while (i <= 25);
}// closing main
} // closing class
Prints this.
1
2
Big
4
Mean
Big
Bugs
8
Big
Mean
11
Big
13
Bugs
BigMean
16
17
Big
19
Mean
BigBugs
22
23
Big
Mean
You have to increment the value of i in the end of loop
public static void main(String [] args){
int i = 1;
do{
if(i %3 == 0){
System.out.print("Big, ");
if(i %5 ==0){
System.out.print("Mean, ");
}
if(i %7 ==0){
System.out.print("Bugs, ");
}
if((i %3 == 0) && (i %5 == 0)){
System.out.print("BigMean, ");
}
if((i %3 == 0) && (i %7 == 0)){
System.out.print("BigBugs, ");
}
}else if((i %5 == 0) && (i %7 == 0)){
System.out.print("MeanBugs, ");
}else if((i %3 == 0) && (i %5 == 0) && (i %7 ==0)){
System.out.print("BigMeanBugs, ");
}else{
System.out.print(i + " ");
}
i++;
}while(i<=106);
}//closing main
} // closing class
My problem is with output of my code. When I enter 20, the output must be weird, but I am getting not weird. Same with the value 18.
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 2 && n >= 5){
ans="Not weird";
} else if(n <= 6 && n >= 20){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}
the output must be weird,but i am getting not weird
Because, if(n%2 == 1) return false and fall to else block where
if(n <= 2 && n >= 5) is `false`
and
else if(n <= 6 && n >= 20) is also `false`
So, again falls to else block. You probably change
if(n <= 2 && n >= 5)
to
if(n >= 2 && n <= 5)
and
else if(n <= 6 && n >= 20)
to
else if(n >= 6 && n <= 20)
Otherwise, they will never be true and always falls to else.
In your program last else is being executed. Change && (logical AND) to || (logical OR) which will check if number is less than something OR higher than something, instead of checking if something is less or equal 5 AND higher or equal to 20 in the same time as it doesn't have a possibility to evaluate in any case.
I have come up with two solutions and also i see a flaw:
1. if(n%2 == 1) this code can be altered to if(n%2 == 0)
2. The flaw is **(n <= 2 && n >= 5)** . No number can be <2 and >5 at the same time. Try changing that to (n <= 2 || n >= 5) and same goes for (n <= 6 && n >= 20)
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 6 || n >= 20){
ans="Not weird";
} else if(n <= 2 || n >= 5){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}
I cant get "FizzBuzz". No matter what the input, the "FizzBuzz" code isn't running. What did I do wrong?
public String[] fizzBuzz(int start, int end) {
int diff = end-start;
String[] array = new String[diff];
for (int i = 0; i < diff; i++) {
if (start%3 == 0 && start%5 == 0) array[i] = "FizzBuzz";
if (start%3 == 0 || start%5 == 0) {
if (start%3 == 0) array[i] = "Fizz";
if (start%5 == 0) array[i] = "Buzz";
}
else {
array[i] = String.valueOf(start);
}
start++;
}
return array;
}
Logic in your if statements is a bit busted, using your code as the starting point, you'd have to do something like this.
if (start%3 == 0 && start%5 == 0) {
array[i] = "FizzBuzz";
}
else if (start%3 == 0 || start%5 == 0) {
if (start%3 == 0) array[i] = "Fizz";
if (start%5 == 0) array[i] = "Buzz";
}
else {
array[i] = String.valueOf(start);
}
String s = "" + i;
if ((i % 3) == 0) {
s += " Fizz";
}
if ((i % 5) == 0) {
s+= " Buzz";
}
System.out.println(s);
This code snippet placed in a loop will print Fizz, Buzz and Fizz Buzz on i divisible by 3, 5 and 15 respectively.
you should try this.
class FizzBuzz{
public static void main(String args[]){
int n = 100;
for(int i=0;i<=n;i++){
if((i % 3) == 0 && (i % 5) != 0){
System.out.println("Fizz");
}
else if((i % 5) == 0 && (i % 3) != 0){
System.out.println("Buzz");
}else if((i % 3) == 0 && (i % 5) == 0){
System.out.println("FizzBuzz");
}else{
System.out.println(""+i);
}
}
}
}
I cannot figure out how to make it so it only calls check4 once... this was for a homework assignment last semester and I got 5 points off for calling it multiple times but I would not like to know how to do it (the professor never said how).
I tried moving the check4 to after the if block but it really needs to go in between the last else if and the else which is not possible. The ONLY way the number should print is if all of the steps do not print out a word instead.
public class CheeseCakeFactory_ajh187 {
public static void main(String[] args) {
int counter = 0;
int printNumber = 0; //number that will get changed to one of the terms
while (counter != 15 & printNumber < 210) { //as long as the counter is not 15 and print number is lesss than 210 it will keep looping.
printNumber++;
if (printNumber % 3 ==0 && printNumber % 5 == 0 && printNumber % 7 == 0) {
System.out.print("cheesecakefactory");
}
else if (printNumber % 3 == 0 && printNumber % 5 == 0){
System.out.print("cheesecake");
check4(printNumber);
}
else if (printNumber % 3 == 0 && printNumber % 7 == 0){
System.out.print("cheesefactory");
check4(printNumber);
}
else if (printNumber % 5 == 0 && printNumber % 7 == 0){
System.out.print("factorycake");
check4(printNumber);
}
else if (printNumber % 3 == 0){
System.out.print("cheese");
check4(printNumber);
}
else if (printNumber % 5 ==0){
System.out.print("cake");
check4(printNumber);
}
else if (printNumber % 7 ==0){
System.out.print("factory");
check4(printNumber);
}
else { //if the number is not divisible by any of the other numbers we still have to check for the 4
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
else {
System.out.print(printNumber); //if its not divisible by 4, we just print the number
}
}
System.out.print(" ");
counter++;
if (counter == 15) { //once the counter is 15 we need to put the new items on a new line
System.out.print("\n");
counter = 0; //resets the counter so that we can accomplish this every 15 passes.
}
}
}
public static void check4(int printNumber) {
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
}
}
First, I would update check4 to return a boolean (which you can save). Something like,
public static boolean check4(int printNumber) {
return String.valueOf(printNumber).contains("4");
}
Then you can also save your tests into boolean variables. Something like
boolean mod3 = printNumber % 3 == 0;
boolean mod5 = printNumber % 5 == 0;
boolean mod7 = printNumber % 7 == 0;
if (mod3 || mod5 || mod7) {
if (mod3 && mod5 && mod7) {
System.out.print("cheesecakefactory");
} else {
boolean isCheck4 = check4(printNumber); // <-- call it once
if (mod3 && mod5) {
System.out.print("cheesecake");
} else if (mod3 && mod7) {
System.out.print("cheesefactory");
} else if (mod5 && mod7) {
System.out.print("factorycake");
} else if (mod3) {
System.out.print("cheese");
} else if (mod5) {
System.out.print("cake");
} else if (mod7) {
System.out.print("factory");
} else {
if (!isCheck4) { // <-- it doesn't have a 4, print it.
System.out.print(printNumber);
}
}
if (isCheck4) {
System.out.print("hoho"); // <-- it does have a 4.
}
}
}
I hope I got your problem right so:
you create a global boolean variable named say isMethodCalled witch is false, then in the check4 method you make it true, and simple check if the isMethodCalled is false before calling the method.
boolean isMethodCalled = false;
if(!isMethodCalled) {
check4() // do wathever you need to do to call check4()
}
public static void check4(int printNumber) {
if (Integer.toString(printNumber).contains("4")){
System.out.print("hoho");
}
isMethodCalled = true;
}
Simply use a flag, set it to true initially.
Then, wherever you dont want the check4 to run, set it to false.
after the if-else, check if the flag istrue. if it is, execute 'check4(printNumber)'
public class CheeseCakeFactory_ajh187 {
public static void main(String[] args) {
int counter = 0;
int printNumber = 0; //number that will get changed to one of the terms
int flag=true;
while (counter != 15 & printNumber < 210) { //as long as the counter is not 15 and print number is lesss than 210 it will keep looping.
printNumber++;
if (printNumber % 3 ==0 && printNumber % 5 == 0 && printNumber % 7 == 0) {
System.out.print("cheesecakefactory");
flag=false;
}
else if (printNumber % 3 == 0 && printNumber % 5 == 0){
System.out.print("cheesecake");
}
else if (printNumber % 3 == 0 && printNumber % 7 == 0){
System.out.print("cheesefactory");
}
else if (printNumber % 5 == 0 && printNumber % 7 == 0){
System.out.print("factorycake");
}
else if (printNumber % 3 == 0){
System.out.print("cheese");
}
else if (printNumber % 5 ==0){
System.out.print("cake");
}
else if (printNumber % 7 ==0){
System.out.print("factory");
}
else { //if the number is not divisible by any of the other numbers we still have to check for the 4
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
else {
System.out.print(printNumber); //if its not divisible by 4, we just print the number
}
flag=false;
}
if(flag)
check4(printNumber);
System.out.print(" ");
counter++;
if (counter == 15) { //once the counter is 15 we need to put the new items on a new line
System.out.print("\n");
counter = 0; //resets the counter so that we can accomplish this every 15 passes.
}
}
}
public static void check4(int printNumber) {
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
}
}