Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE114 Spring 2016
Lab Exercise 1 of Week 5
Generating and Determining Fibonacci Sequence
Chen-Wei Wang
Problem
The Fibonacci Sequence is an array of non-negative integer numbers
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, . . .
where starting from the third number, each number is equal to the sum of the previous two numbers.
For example, the 3rd number in the sequence is 1 (equal to 0 + 1), the 4th number is 2 (equal to 1 + 1),
the 5th number is 3 (equal to 1 + 2), 6th number is 5 (equal to 2 + 3), 7th number is 8 (equal to 3 + 5),
and so on.
Task 1
You are asked to write a fragment of Java code that generates an array of the first n numbers in the
Fibonacci sequence, where n is an integer value entered by the user. Here is an expected run of your
program:
Enter the size of the Fibonaccci sequence:
0
Error: sequence size must be larger than zero.
Would you like to continue? (Y/N)
Y
Enter the size of the Fibonaccci sequence:
1
Fibonacci Sequence of size 1:
<0>
Would you like to continue? (Y/N)
Y
Enter the size of the Fibonaccci sequence:
6
Fibonacci Sequence of size 6:
<0, 1, 1, 2, 3, 5>
Would you like to continue? (Y/N)
N
Bye!
Part of the code is finished already for you:
. . .
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the Fibonacci sequence:");
int size = input.nextInt();
1
Task 2
You are asked to write a fragment of Java code that prompts the user to keep entering integer values
until they wish to stop. When requested to stop, determine if the sequence of numbers entered so far is
a Fibonacci Sequence. Here is an expected run of your program:
Enter a positive integer value, or -1 to stop:
0
Enter a positive integer value, or -1 to stop:
1
Enter a positive integer value, or -1 to stop:
1
Enter a positive integer value, or -1 to stop:
3
Enter a positive integer value, or -1 to stop:
-1
The seuqence you entered so far <0, 1, 1, 3> is not a Fibonacci Sequence!
Would you like to try another sequence? (Y/N)
Y
Enter a positive integer value, or -1 to stop:
0
Enter a positive integer value, or -1 to stop:
1
Enter a positive integer value, or -1 to stop:
1
Enter a positive integer value, or -1 to stop:
2
Enter a positive integer value, or -1 to stop:
3
Enter a positive integer value, or -1 to stop:
-1
The seuqence you entered so far <0, 1, 1, 2, 3> is a Fibonacci Sequence!
Would you like to try another sequence? (Y/N)
N
Bye!
2