Java 循环、方法和一些新手问题
我正在写一个程序,可以接收一组数字作为字符串,然后使用数学过程来找到该数字序列的多项式函数。我上了两节课。好的,三个。但对于这个问题,两个因素很重要。这些类是顺序和术语,如下所示 顺序:Java 循环、方法和一些新手问题,java,Java,我正在写一个程序,可以接收一组数字作为字符串,然后使用数学过程来找到该数字序列的多项式函数。我上了两节课。好的,三个。但对于这个问题,两个因素很重要。这些类是顺序和术语,如下所示 顺序: import java.util.ArrayList; import java.util.Arrays; import java.lang.Math; public class Sequence { // the numbers in the sequence private double[] s
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Math;
public class Sequence
{
// the numbers in the sequence
private double[] sequence;
// sets up sequence by parsing s
// the numbers in s will be separated by commas
public Sequence(String s)
{
sequence = new double[] {1, 4, 19, 52};
}
// returns sequence
public double[] getSequence()
{
return sequence;
}
// returns 1 * 2 * ... * (x-1) * x
public double factorial(double x)
{
for (double i=1;i<=x;i++){
x = x*i;
}
return x;
}
// returns true iff all of the items on sequence are equal
public boolean allEqual(double[] sequence)
{
boolean checker = true;
double first = sequence[1];
for (int i = 1; i<sequence.length ; i++) {
if(sequence[i] == first) {
checker = true;
}
}
return checker;
}
// returns a new array holding the differences between adjacent items on sequence
public double[] differences(double[] sequence)
{
double[] diffs = new double[sequence.length - 1];
for(int i = 0;i<=sequence.length;i++) {
diffs[i] = sequence[i+1]- sequence[i];
}
return diffs;
}
// subtracts from each item in sequence the effect of the term t
// implements Step 4 of the algorithm description on the project web page
public void updateSequence(Term t)
{
for(int i=0;i<=sequence.length;i++) {
sequence[i] = sequence[i] - t.getCoefficient()*Math.pow(i,t.getExponent());
}
}
// returns the next term in the simplest polynomial that generates sequence
// implements Steps 1-3 of the algorithm description on the project web page
public Term nextTerm()
{
double[] lastSequence = sequence;
int steps = 0;
while ( !allEqual(lastSequence)) {
lastSequence = differences(lastSequence);
steps++;
}
return new Term(lastSequence[1]/factorial(steps), steps);
}
// returns the simplest polynomial that generates sequence and displays the polynomial as a String
// implements the algorithm description on the project web page
public Polynomial solveSequence()
{
// TODO
return null;
}
}
多项式:
import java.util.ArrayList;
public class Polynomial
{
// the terms making up the polynomial
private ArrayList<Term> polynomial;
// creates a zero polynomial
public Polynomial()
{
polynomial = new ArrayList<>();
}
// returns the number of terms in polynomial
public int numberOfTerms()
{
return polynomial.size();
}
// adds a new term to the end of polynomial
public void addTerm(Term t)
{
polynomial.add(t);
}
// returns the indicated term of polynomial
public Term getTerm(int k)
{
if (0 <= k && k < numberOfTerms())
return polynomial.get(k);
else return null;
}
// returns polynomial as a String for display
// see the sample file for the layout required
public String display()
{
// TODO
return "";
}
}
import java.util.ArrayList;
公共类多项式
{
//构成多项式的项
私有ArrayList多项式;
//创建一个零多项式
公共多项式()
{
多项式=新的ArrayList();
}
//返回多项式中的项数
public int numberOfTerms()
{
返回多项式.size();
}
//在多项式的末尾添加一个新项
公共条款(t条款)
{
多项式加(t);
}
//返回指定的多项式项
公共术语getTerm(int k)
{
如果(0
all equal方法应返回标志false或true,这取决于它们在顺序上是相同的还是不同的
正如我所见,您的all equal方法总是返回true。请以这样的方式编写逻辑,如果所有元素都不相同,那么它将返回false。根据我的理解,您希望在方法中执行某种操作,以创建术语并将其添加到结果中。您希望这样做,直到所有序列都相同。因此,我的建议on是迭代序列并检查序列中的相似性。如果发现了中断循环。我是指某种无限循环你好。是的。我有点理解这一点,并且正在试图弄清楚如何编写它。我是一个新手。无论如何,我真正想知道的是如何使用String.split和Double.parseDouble在构造函数中,将数字字符串分隔,然后存储在数组中并转换为双精度。请在该问题上提供帮助。关于拼图,您可以通过string string=“1,2,3,4”string[]parts=string.split(“,”);而不是“-”来实现…那是一个逗号,对吗?把它们存储在一个数组中,然后进行解析怎么样?我实际上不知道那是什么。是的,你能粘贴整个代码吗?我在多项式解序列中遇到错误。如果我将字符串s作为参数,我的设置中的这个计算不存在。我只需执行以下操作:string string=s;string[]parts=string.split(“,”);double.parsedouble(string);这将给我一个double数组?对不起。我是新手,需要帮助。
import java.util.ArrayList;
public class Polynomial
{
// the terms making up the polynomial
private ArrayList<Term> polynomial;
// creates a zero polynomial
public Polynomial()
{
polynomial = new ArrayList<>();
}
// returns the number of terms in polynomial
public int numberOfTerms()
{
return polynomial.size();
}
// adds a new term to the end of polynomial
public void addTerm(Term t)
{
polynomial.add(t);
}
// returns the indicated term of polynomial
public Term getTerm(int k)
{
if (0 <= k && k < numberOfTerms())
return polynomial.get(k);
else return null;
}
// returns polynomial as a String for display
// see the sample file for the layout required
public String display()
{
// TODO
return "";
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String Args[]) {
parse("1,2,3,4");
}
public static void parse(String s) {
String[] parts = s.split(",");
List<Double> list = new ArrayList<>();
for (String string : parts) {
list.add(Double.parseDouble(string));
}
for (double d : list) {
System.out.println(d);
}
}
}
While(!allEqual(sequence))
{
do other operation
}