Я закодировал приведенную ниже программу в Java, чтобы определить, в каком типе шаблона находится числовая последовательность. Однако у меня проблемы при делении дублей; даже если 2 двойных значения очень малы (меньше двух десятичных знаков; точность теряется, даже если есть только один десятичный разряд). Я собирался использовать класс bigdecimal, но хотел бы знать, есть ли более простой метод или альтернативы, так как было бы утомительно добавлять bigdecimal в более крупные программы с большим количеством функций и переменных. Кроме того, могу ли я сделать какие-либо другие улучшения?
package Numbers;
import javax.swing.*;
public class NumberPatterns {
public static void main(String[] args) {
double a, b, c, d;
a = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the first term.")); //first term
b = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the second term.")); //second term
c = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the third term.")); //third term
d = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the fourth term.")); //fourth term
double constantDifference, constantRatio, constantSecondDifference; //Creating variables to verify the sequence characteristic
constantDifference = (b-a) - (d-c); //Constant Difference
System.out.println("b/a value is !! "+b/a);
System.out.println("d/c value is !! "+d/c);
constantRatio = (b/a) - (d/c); //Constant Ratio
constantSecondDifference = ((d-c)-(c-b)) - ((c-b)-(b-a)); // Constant Second Difference
if (constantDifference == 0) // Arthmetic sequence
{
System.out.println("The given sequence is: " + a + "; " + b + "; " + c + "; " + d); // print sequence
System.out.println("This sequence is an arthmetic sequence."); // Sequence type
System.out.println("The constant difference is: " + (b-a)); // Constant difference
System.out.println("The general term is: Tn = " + (a + " + " + (b-a)+ "(n -1)")); //General term
}
else if (constantRatio == 0) // Geometric sequence
{
System.out.println("The given sequence is: " + a + "; " + b + "; " + c + "; " + d); // print sequence
System.out.println("This sequence is an geometric sequence."); // Sequence type
System.out.println("The constant ratio is: " + (b/a)); // Constant ratio
System.out.println("The general term is: Tn = " + a + "*" + (b/a) + "^" + "(n-1)"); //G eneral term
}
else if (constantSecondDifference == 0 && constantDifference != 0) // Quadratic sequence
{
System.out.println("The given sequence is: " + a + "; " + b + "; " + c + "; " + d); // print sequence
System.out.println("This sequence is an quadratic sequence."); // Sequence type
System.out.println("The constant second difference is: " + ((c-b)-(b-a))); // Constant second difference
double x, y, z; // The follwing variables are used to calculate the general term
x = ((c-b)-(b-a));
x = x/2;
y = (b-a) - 3*x;
z = a - x -y;
System.out.println("The general term is: " + x + "n^2 + " + y + "n + (" + z + ")"); // General term
}
}
}