英文:
Java Program To Solve The Quadratic Equation Does Not Work
问题
以下是您提供的代码的翻译部分:
我正在使用类来尝试完成这个任务,我感觉计算机认为我把数字排列顺序搞错了,它们不能按正确的顺序求解。
二次方程求解器
import java.util.Scanner;
import static java.lang.System.*;
public class QuadraticRunner
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner( System.in );
out.print("Enter a :: ");
int quadA = keyboard.nextInt();
out.print("Enter b :: ");
int quadB = keyboard.nextInt();
out.print("Enter c :: ");
int quadC = keyboard.nextInt();
Quadratic test = new Quadratic();
System.out.println( test.getRootOne( quadA,quadB,quadC ) );
System.out.println( test.getRootTwo( quadA,quadB,quadC ) );
}
}
二次方程求解器
public class Quadratic
{
public static double getRootOne( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad + b);
Quad = ((double)Quad/(2*a));
return Quad;
}
public static double getRootTwo( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad - b);
Quad = ((double)Quad/(2*a));
return Quad;
}
}
当我尝试将数字输入程序中,数字为1、1、1时,结果如下:
Root 1 = -1.0
Root 2 = -2.0
英文:
I am using classes to try to do this task, I feel like the computer thinks I made the numbers out of order, and they do not solve in the correct sequence.
The Quadratic Runner
import java.util.Scanner;
import static java.lang.System.*;
public class QuadraticRunner
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner( System.in );
out.print("Enter a :: ");
int quadA = keyboard.nextInt();
out.print("Enter b :: ");
int quadB = keyboard.nextInt();
out.print("Enter c :: ");
int quadC = keyboard.nextInt();
Quadratic test = new Quadratic();
System.out.println( test.getRootOne( quadA,quadB,quadC ) );
System.out.println( test.getRootTwo( quadA,quadB,quadC ) );
}
}
The Quadratic
public class Quadratic
{
public static double getRootOne( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad + b);
Quad = ((double)Quad/(2*a));
return Quad;
}
public static double getRootTwo( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad - b);
Quad = ((double)Quad/(2*a));
return Quad;
}
}
When I try to plug the numbers into the program the numbers being 1, 1, 1 it comes out as
Root 1 = -1.0
Root 2 = -2.0
答案1
得分: 2
你的代码中有2个小错误:
- 你在方法的第二行定义了
sqrt
变量,但从未在方法的第三行中使用它(应该在方法的第三行中使用)。 - 你在方法的第三行中颠倒了参数的顺序(二次方程的公式是
b +/- sqrt(...)
而不是sqrt(...) +/- b
)。
我想这样会给你正确的答案:
public class Quadratic {
public static double getRootOne(int a, int b, int c) {
double quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(quad);
quad = (b + (double) sqrt); //原始:((double)quad + b);
quad = ((double)quad/(2*a));
return quad;
}
public static double getRootTwo(int a, int b, int c) {
double quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(quad);
quad = (b - (double) sqrt); //原始:((double)quad - b);
quad = ((double)quad/(2*a));
return quad;
}
}
英文:
There are 2 minor mistakes in your code:
- You never use the
sqrt
variable you define in the second line of your methods (should be used in line 3 of the methods) - You have switched up the order of arguments in the 3rd line of your methods (the quadratic formula is
b +/- sqrt(...)
and notsqrt(...) +/- b
)
I suppose this should give you the correct answers:
public class Quadratic {
public static double getRootOne(int a, int b, int c) {
double quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(quad);
quad = (b + (double) sqrt); //original: ((double)quad + b);
quad = ((double)quad/(2*a));
return quad;
}
public static double getRootTwo(int a, int b, int c) {
double quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(quad);
quad = (b - (double) sqrt); //original: ((double)quad - b);
quad = ((double)quad/(2*a));
return quad;
}
}
答案2
得分: 0
import java.util.Scanner;
import static java.lang.System;
public class QuadraticRunner
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner( System.in );
out.print("Enter a :: ");
int quadA = keyboard.nextInt();
out.print("Enter b :: ");
int quadB = keyboard.nextInt();
out.print("Enter c :: ");
int quadC = keyboard.nextInt();
System.out.println( Quadratic.getRootOne( quadA,quadB,quadC ) );
System.out.println( Quadratic.getRootTwo( quadA,quadB,quadC ) );
}
}
public class Quadratic
{
public static double getRootOne( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad + b);
Quad = ((double)Quad/(2*a));
return Quad;
}
public static double getRootTwo( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad - b);
Quad = ((double)Quad/(2*a));
return Quad;
}
}
英文:
import java.util.Scanner;
import static java.lang.System.*;
public class QuadraticRunner
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner( System.in );
out.print("Enter a :: ");
int quadA = keyboard.nextInt();
out.print("Enter b :: ");
int quadB = keyboard.nextInt();
out.print("Enter c :: ");
int quadC = keyboard.nextInt();
System.out.println( Quadratic.getRootOne( quadA,quadB,quadC ) );
System.out.println( Quadratic.getRootTwo( quadA,quadB,quadC ) );
}
public class Quadratic
{
public static double getRootOne( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad + b);
Quad = ((double)Quad/(2*a));
return Quad;
}
public static double getRootTwo( int a, int b, int c )
{
double Quad = (b*b)-(4*a*c);
double sqrt = Math.sqrt(Quad);
Quad = ((double)Quad - b);
Quad = ((double)Quad/(2*a));
return Quad;
}
}
}
In the above code, you should use the class name not the object as you created a test to call the static method to the public static void main method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论