需要帮助处理java中的斐波那契序列 [英] Need help working with the fibonacci sequence in java

查看:82
本文介绍了需要帮助处理java中的斐波那契序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为我的代码获取以下输出,稍后我将提供:



示例

输入一个整数(1-46):2



Fibonacci序列中的第二个数字是:1



我必须确保足够的nd,st,th在输出中。下面是我到目前为止创建的代码:



  import  java.util中*。 

public class Somethingpart2 {

public static void main( String [] args){

Scanner kbd = new Scanner(System.in);

// 变量声明
int 数字;
long Fibnumber;
布尔接受,限制;

// Fibonacci序列的用户输入的开头
System.out.print( 输入一个整数(1-46):);
number = kbd.nextInt();

Fibnumber = Math.round(Math.pow((1 + Math.sqrt( 5 ))/ 2,number)/ Math。 sqrt( 5 ));
accepted = number> = 1 && number< = 46 ;
limit = number == Fibnumber;


如果(已接受){
{
Fibnumber = Math.round(Math.pow((1 + Math.sqrt( 5 ))/ 2,number)/ Math.sqrt(< span class =code-digit> 5 ));
if ()

}
while (限制);
}
else
System.out.println( < span class =code-string>不是有效数字。);







我是否以正确的方式解决这个问题?不知道如何处理if语句部分,但我相信它是必需的。任何建议将不胜感激。我现在只使用Java几个月了。



v / r



OneStepForward



我尝试了什么:



到目前为止,我已经在应用程序的外部工作了。我需要一些关于这一切的指导。

解决方案

这个公式当然很聪明,可以在1中获得答案:

< pre lang =java> Fibnumber = Math.round(Math.pow((1 + Math.sqrt( 5 ))/ 2,number)/ Math.sqrt ( 5 ));



但浮点上的功率,平方根和除法都在吃很多cpu时钟周期,计算序列的每个值可能更经济,因为下一个值只需要整数加法。



逐步执行代码可能会有所帮助你要了解代码正在做什么以及缺少什么。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较做。



建议:为问题的每个部分构建一些函数,它有助于在不破坏其他事情的情况下处理更改。


I'm currently trying to get the following output for my code that I'll provide later on:

Example
Enter an integer (1-46): 2

The 2nd number in the Fibonacci sequence is: 1

I have to insure the suffice "nd, st, th" is in the output at well. Below is the code I've created so far:

import java.util.*;

public class Somethingpart2 {

	public static void main(String[] args) {

		Scanner kbd = new Scanner(System.in);

		//Variable Declaration 
		int number;
		long Fibnumber;
		Boolean accepted, limit;

		//Beginning of user input for the Fibonacci sequence 
		System.out.print("Enter an integer (1-46): ");
		number = kbd.nextInt();

		Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5)); 
		accepted = number >= 1 && number <= 46;
		limit = number == Fibnumber;


		if (accepted) {
			do {
				Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5));
				if ()

			}
			while (limit);
		}
		else
			System.out.println("Not a valid number.");




Am I going about this the right way? Not sure how to go about the if statement portion, but I believe it's needed. Any advice would be much appreciated. I've only been using Java for a few months now.

v/r

OneStepForward

What I have tried:

So far I've worked on the outer portion of the application. I need some guidance on the meat of it all.

解决方案

This formula is certainly clever to get the answer in 1 go:

Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5));


but power, square root and division on floating point are all eating a lot of cpu clock cycles, it may prove more economical to compute each value of the sequence because the next value only require an integer addition.

Executing your code step by step may help you to understand what the code is doing and what is missing.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.

Advice: build some functions for each part of the problem, it help to handle changes without breaking other things.


这篇关于需要帮助处理java中的斐波那契序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆