English 中文(简体)
扫描仪部分精加工部分
原标题:Scanner fractionSum finishing part
  • 时间:2010-10-27 04:30:52
  •  标签:
  • java

我需要编写一个方法,其中参数接受一个整数(在本例中为n),并将序列的前n项之和作为双值返回。所以说我把<code>分数(5)输出将是

1++1/5,等于2.8333~(最终结果)

我现在拥有的是:

public static void main(String[] args) {
  fractionSums(5);
}

public static void fractionSums(int n) {
  Scanner console = new Scanner(System.in);

  System.out.print("How many terms do you have? ");
  int totalterms = console.nextInt();
}

我被困在这里,不知道如何做得更远,也不知道在哪里实施。你如何建议我继续做这件事。我必须编辑这个问题吗?

最佳回答

尝试将扫描仪放置在main中,并将println放置在main中。您应该创建一个方法(您在这里称之为fractionSems()),并调用它来计算分数和并返回一个double(将int n传递给该方法)。该方法可以是递归的,也可以是迭代的,但由于您说“我需要编写一个方法,其中参数接受一个整数(在这种情况下为n),并且返回序列前n项的总和”,因此应该使该方法返回一些值(浮点值或双精度值)。

邻苯二甲酸:)

问题回答

更明确地编写您的输入,并查看模式。问问自己,这些数字是从哪里来的?

定义一个double变量来保存结果,并将其初始化为零。

您必须合计totalTerms条款。您可以先编写一个循环(例如,循环的),该循环将运行totalTerms迭代。

在迭代1中,您需要将结果加上1/1。在迭代2中,您需要添加1/2。在迭代3中,您需要添加1/3,依此类推。您可以看到迭代次数和您需要添加的项之间存在关系。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...