我已经用java应用程序解决了这个等式。
看起来像这样
y*n-1 = y*n-2 + 1035 + [(n-1)^2 + (n-3)] * y
y*n = y*n-1 + (n^2 * y)
因此用户应该输入N然后Java应用程序应该计算Y是多少。 这有什么可能做的,如果是的话,怎么做?
提前致谢, 迈克尔。
编辑:
感谢mprabhat,它现在看起来像这样,但我仍然以某种方式做错了..
public class equation
{
private static double solveFirstEquation(double n){
double y =0;
if(n > 0) {
y = ((n -1) + Math.pow( n , 2))/ n;
}
return y;
}
private static double solveSecondEquation(double n){
double y = 0;
if(n > 1) {
y = ((n-2)+ (Math.pow(n-1, 2) + n-3) + 1035)/(n-1);
}
return y;
}
public static void main(String args[])
{
System.out.println("How much is n?");
int n = 0;
n = Keyboard.readInt();
}
}
我试图将“void main String args”放在顶部,但这不会让我运行应用程序。
答案 0 :(得分:1)
这样的事情:
import java.util.Scanner;
public class TestEquation {
private static double solveFirstEquation(double n) {
double y = 0;
if (n > 0) {
y = ((n - 1) + Math.pow(n, 2)) / n;
}
return y;
}
private static double solveSecondEquation(double n) {
double y = 0;
if (n > 1) {
y = ((n - 2) + (Math.pow(n - 1, 2) + n - 3) + 1035) / (n - 1);
}
return y;
}
public static void main(String args[]) {
System.out.println("How much is n?");
double n = 0;
Scanner scanner = new Scanner(System.in);
n = scanner.nextDouble();
System.out.println(solveFirstEquation(n));
System.out.println(solveSecondEquation(n));
}
}
答案 1 :(得分:1)
将您的main
替换为
public static void main(String args[])
{
System.out.println("How much is n?");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.print("First equation: ");
System.out.println(solveFirstEquation(n));
System.out.print("Second equation: ");
System.out.println(solveSecondEquation(n));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 2 :(得分:0)
如果你想解决这个特定的等式
一种方法是将方程转换为y=
形式
/*
* y*n-1 = y*n-2 + 1035 + [(n-1)^2 + (n-3)] * y
* y = -1034 /(n^2-n-2)
*/
public static double eq1(double n){
double m =n*n-n-2;
return -1034 /m;
}
/*
* y*n = y*n-1 + (n^2 * y)
* y = 1/n^2
*/
public static double eq2(double n){
return 1/(n*n);
}
有趣的方法是使用 wolframalpha !