打印字符串而不是斐波那契数列
Print Strings instead of Fibonacci numbers
我有一个关于在 Java 中生成斐波那契数列的初学者问题。
在这个 java 程序中,该方法应该打印从 BeginIndex
到 LastIndex
的斐波那契数列,所以它应该打印 "Hi"
(而不是数字)如果数字是 5 的倍数,如果数字是 7 的倍数,则打印 "I am"
,如果数字是 35 的倍数,则打印 "Hi I am me"
。我不太确定该怎么做。
class FibonacciClass {
public static void main(String args[] ) throws Exception {
generatefibonacci(10, 20);
generatefibonacci(0, 1);
}
private static void generatefibonacci(int BeginIndex, int LastIndex) {
}
您要查找的是模数运算符 %。它将 return 除以其操作数的余数。因此,从 if (x % 5 == 0 && x % 7 == 0) 接收到真值将表示数字 x 是 5 和 7 的倍数。如果这种情况没有通过,您应该使用 else if 语句来分别检查 x 是否是 5 的倍数,然后再检查 x 是否是 7 的倍数,每个 if 分支调用 System.out.println("x is a multiple of y");
每次 generate fibonacci 产生了一个结果,你必须用模数 (%) 检查。
像这样:
private static generatefibonnacci(int startIndex, int endIndex){
int result = -1;
//do generating stuff
//and set "result" to generated fibonacci
//and then
if(result%5==0){
System.out.println("Hi");
} else if(result%7==0){
System.out.println("Hi I am me!");
} //and so on
所以这只是一个小例子。
玩得开心
另一种可能性:
private static void generateFibonacci(int beginIndex, int lastIndex) {
int len = lastIndex + 1;
int[] fib = new int[len];
fib[0] = 0;
fib[1] = 1;
// Building Fibonacci sequence from beginIndex through lastIndex
for (int i = 2; i < len; ++i)
fib[i] = fib[i-1] + fib[i-2];
// Printing
for (int index = beginIndex; index <= lastIndex; ++index) {
if ((fib[index] % 5 == 0) && (fib[index] % 7 == 0)) {
System.out.println("Hi I am me");
}
else if (fib[index] % 5 == 0) {
System.out.println("Hi");
}
else if (fib[index] % 7 == 0) {
System.out.println("I am");
}
else {
System.out.println(fib[index]);
}
}
}
private static void generatefibonacci(int BeginIndex, int LastIndex) {
int[] numbers = new int[LastIndex + 2]; //creates an array to put fibonacci numbers in
numbers[0] = 1; numbers[1] = 1;
for(int i = 2; i <= LastIndex; i ++){
numbers[i] = numbers[i - 1] + numbers[i - 2]; //generates the Fibonacci Sequence
}
for(int i = BeginIndex; i <= LastIndex; i ++){
if(numbers[i] % 5 == 0 && numbers[i] % 7 == 0){//if the remainder when the numbers/5 equals 0 and the remainder when the numbers/7 equals 0
System.out.println("Hello I am me");
}
else if(numbers[i] % 5 == 0){ //if the remainder when the numbers/5 equals 0
System.out.println("I am");
}
else if(numbers[i] % 7 == 0){ //if the remainder when the numbers/7 equals 0
System.out.println("Hi");
}
else{ //if none of the above three conditions are true
System.out.println(numbers[i]);
}
}
}
我有一个关于在 Java 中生成斐波那契数列的初学者问题。
在这个 java 程序中,该方法应该打印从 BeginIndex
到 LastIndex
的斐波那契数列,所以它应该打印 "Hi"
(而不是数字)如果数字是 5 的倍数,如果数字是 7 的倍数,则打印 "I am"
,如果数字是 35 的倍数,则打印 "Hi I am me"
。我不太确定该怎么做。
class FibonacciClass {
public static void main(String args[] ) throws Exception {
generatefibonacci(10, 20);
generatefibonacci(0, 1);
}
private static void generatefibonacci(int BeginIndex, int LastIndex) {
}
您要查找的是模数运算符 %。它将 return 除以其操作数的余数。因此,从 if (x % 5 == 0 && x % 7 == 0) 接收到真值将表示数字 x 是 5 和 7 的倍数。如果这种情况没有通过,您应该使用 else if 语句来分别检查 x 是否是 5 的倍数,然后再检查 x 是否是 7 的倍数,每个 if 分支调用 System.out.println("x is a multiple of y");
每次 generate fibonacci 产生了一个结果,你必须用模数 (%) 检查。
像这样:
private static generatefibonnacci(int startIndex, int endIndex){
int result = -1;
//do generating stuff
//and set "result" to generated fibonacci
//and then
if(result%5==0){
System.out.println("Hi");
} else if(result%7==0){
System.out.println("Hi I am me!");
} //and so on
所以这只是一个小例子。 玩得开心
另一种可能性:
private static void generateFibonacci(int beginIndex, int lastIndex) {
int len = lastIndex + 1;
int[] fib = new int[len];
fib[0] = 0;
fib[1] = 1;
// Building Fibonacci sequence from beginIndex through lastIndex
for (int i = 2; i < len; ++i)
fib[i] = fib[i-1] + fib[i-2];
// Printing
for (int index = beginIndex; index <= lastIndex; ++index) {
if ((fib[index] % 5 == 0) && (fib[index] % 7 == 0)) {
System.out.println("Hi I am me");
}
else if (fib[index] % 5 == 0) {
System.out.println("Hi");
}
else if (fib[index] % 7 == 0) {
System.out.println("I am");
}
else {
System.out.println(fib[index]);
}
}
}
private static void generatefibonacci(int BeginIndex, int LastIndex) {
int[] numbers = new int[LastIndex + 2]; //creates an array to put fibonacci numbers in
numbers[0] = 1; numbers[1] = 1;
for(int i = 2; i <= LastIndex; i ++){
numbers[i] = numbers[i - 1] + numbers[i - 2]; //generates the Fibonacci Sequence
}
for(int i = BeginIndex; i <= LastIndex; i ++){
if(numbers[i] % 5 == 0 && numbers[i] % 7 == 0){//if the remainder when the numbers/5 equals 0 and the remainder when the numbers/7 equals 0
System.out.println("Hello I am me");
}
else if(numbers[i] % 5 == 0){ //if the remainder when the numbers/5 equals 0
System.out.println("I am");
}
else if(numbers[i] % 7 == 0){ //if the remainder when the numbers/7 equals 0
System.out.println("Hi");
}
else{ //if none of the above three conditions are true
System.out.println(numbers[i]);
}
}
}