Java,嵌套if,if / else语句添加单词

时间:2016-03-25 21:11:16

标签: java if-statement

我正在做一项任务,我需要做很多需要嵌套if / else的事情。打印前50个斐波那契数字,但是:

  • 如果数字是3的倍数 - 打印“奶酪”
  • 如果数字是5的倍数 - 打印“Cake”
  • 如果数字是7的倍数 - 打印“工厂”
  • 如果数字是3&的倍数。 5 - 打印“CheeseCake”
  • 如果数字是3&的倍数。 7 - 打印“CheeseFactory”
  • 如果数字是5&的倍数。 7 - 打印“CakeFactory”
  • 如果数字是3&的倍数。 5& 7 - 打印“CheeseCakeFactory”
  • 如果数字是2的倍数 - 打印“Blah”

此时我正在重复这些条件,我确信有一种更清洁的方法:

package Assignment1;

public class CheeseCakeFactory_163003984 {

    public static void main(String[] args) {
        long numberOne = 0;
        long numberTwo = 1;
        long sum = 0;
        int counter = 0;

        String word1 = "Cheese";
        String word2 = "Cake";
        String word3 = "Factory";

        while (counter <= 50) {
            sum = numberOne + numberTwo;
            numberOne = numberTwo;
            numberTwo = sum;
            counter++;

            if (sum % 3 == 0) {
                System.out.print(word1 + ", ");
            } else if (sum % 5 == 0) {
                if (sum % 3 == 0) {
                    System.out.print(word1 + word2 + ", ");
                } else if (sum % 3 == 0) {
                    if (sum % 5 == 0) {
                        if (sum % 7 == 0) {
                            System.out.print(word1 + word2 + word3 + ", ");
                        }
                    }
                    if (sum % 7 == 0) {
                        System.out.print(word1 + word2 + word3 + ", ");
                    } else if (sum % 2 == 0) {
                        System.out.print("Blah, ");
                    } else {
                        System.out.print(sum);

                        if (counter % 10 == 0) {
                            System.out.print("\n");
                        } else {
                            System.out.print(", ");
                        }
                    }
                }
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

名称“CheeseFactory”,“CakeFactory”和“CheeseCakeFactory”背后有一个很好的理由。这样你就不必重复你的陈述。

我们假设您有一个数组,前50个斐波那契数字已被称为numbers

for(int i = 0; i < numbers.length; i++) {
    System.out.print(numbers[i] + ": ");
    if(numbers[i] % 3 == 0) {
        System.out.print("Cheese");
    }
    if(numbers[i] % 5 == 0) {
        System.out.print("Cake");
    }
    if(numbers[i] % 7 == 0) {
        System.out.print("Factory");
    }
    System.out.println(""); //start a new line
}

我没有说明如果它是两个的倍数将会发生什么,因为它是模棱两可的。如果它是两个和三个的倍数怎么办?

答案 1 :(得分:1)

这里的想法是你想要一次构建一个语句而不是一次构建语句。在这里,我将利用一些称为StringBuilder的东西,这将使我们能够整齐,简洁地构建我们想要的字符串。

如果我们知道可以被3整除的总和,我们将我们想要的单词添加到appender。

if (sum % 3 == 0) {
    builder.append(word1);
}

如果我们知道可以被3和5整除的总和,我们将我们想要的单词添加到appender。

if (sum % 3 == 0) {
    builder.append(word1);
}

if(sum % 5 == 0) {
    builder.append(word2);
}

在其他逻辑方面没有什么特别需要发生的事情;简单的if条件将为您提供所需的结果。如果不是,则不执行if块。

我留下其他表格(包括偶数表格,实际上还打印出数字 - 提示:如果你没有打印任何其他单词,你可能想打印这个数字)作为读者练习。 / p>

答案 2 :(得分:0)

我尝试了你的代码,它一直打印着干酪这个词。

我重构了一点以满足您的要求(我希望我理解它们:))

public static void main(String[] args) {
    long numberOne = 0;
    long numberTwo = 1;
    long sum = 0;
    int counter = 0;

    String word1 = "Cheese";
    String word2 = "Cake";
    String word3 = "Factory";
    String word4 = "Blah";


    while (counter <= 50) {
        sum = numberOne + numberTwo;
        numberOne = numberTwo;
        numberTwo = sum;
        counter++;
        StringBuilder sb = new StringBuilder();
        if (sum % 2 == 0) {
            sb.append(word4);
        } else {
            if (sum % 3 == 0) sb.append(word1);
            if (sum % 5 == 0) sb.append(word2);
            if (sum % 7 == 0) sb.append(word3);
        }
        if (sum % 2 == 0 || sum % 3 == 0 || sum % 5 == 0 || sum % 7 == 0) {
            sb.append(",");
            System.out.println(String.format("%s %s", sum, sb.toString()));
        }

    }
}

然后你可以重构它将它提取到word工厂方法等等

这将打印

2 Blah,
3 Cheese,
5 Cake,
8 Blah,
21 CheeseFactory,
34 Blah,
55 Cake,
144 Blah,
610 Blah,
987 CheeseFactory,
2584 Blah,
6765 CheeseCake,
10946 Blah,
46368 Blah,
75025 Cake,
196418 Blah,
317811 Cheese,
832040 Blah,
2178309 CheeseFactory,
3524578 Blah,
9227465 Cake,
14930352 Blah,
63245986 Blah,
102334155 CheeseCakeFactory,
267914296 Blah,
701408733 Cheese,
1134903170 Blah,
4807526976 Blah,
12586269025 Cake,
20365011074 Blah,
32951280099 Cheese,

答案 3 :(得分:-1)

您可以在同一if(sum % 3 == 0 && sum % 5 == 0) // check if this number is a multiple of 3 and 5 System.out.print("cheesecake"); 语句中检查多个条件,而不是为每个可能的条件进行嵌套。例如,要检查数字是否为3和5的倍数,您只需编写:

if

使用if语句,就像我刚给出的示例一样,您可以在不嵌套else if的情况下完成所有这些操作,只需使用if或其他if((sum % 3 == 0) && (sum % 5 == 0)) System.out.print("cheesecake"); Phoenix.Param

如果编写语句的方式令人困惑,您可以使用括号将语句组合在一起以提高可读性:

# web/models/weapon.ex
defmodule MyApp.Weapon do
  # ...

  defimpl Phoenix.Param do
    def to_param(%{name: name}) do
      to_string(name)
    end
  end
end

无论你怎么写它,结果都是一样的。