用Java求一元二次方程ax2+bx+c=0的根,要求a、b、c从控制台输入
4个回答
展开全部
import java.util.Scanner;
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a");
int a = sc.nextInt();
System.out.println("请输入b");
int b = sc.nextInt();
System.out.println("请输入c");
int c = sc.nextInt();
if(b*b - 4*a*c < 0){
System.out.println("对不起,无解!");
}else if(b*b - 4*a*c == 0){
System.out.println("此方程解x=" + -b/(2*a));
}else{
System.out.println("此方程解x1=" + ((-b+ "/"+ 2*a +"+√(" + (b*b - 4*a*c)) +")/" +(2*a)) + " , x2=" + ((-b+ "/"+ 2*a + "-√(" + (b*b - 4*a*c)) +")/" +(2*a)) );
}
}
}
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a");
int a = sc.nextInt();
System.out.println("请输入b");
int b = sc.nextInt();
System.out.println("请输入c");
int c = sc.nextInt();
if(b*b - 4*a*c < 0){
System.out.println("对不起,无解!");
}else if(b*b - 4*a*c == 0){
System.out.println("此方程解x=" + -b/(2*a));
}else{
System.out.println("此方程解x1=" + ((-b+ "/"+ 2*a +"+√(" + (b*b - 4*a*c)) +")/" +(2*a)) + " , x2=" + ((-b+ "/"+ 2*a + "-√(" + (b*b - 4*a*c)) +")/" +(2*a)) );
}
}
}
展开全部
输出格式
请输入a
4
请输入b
7
请输入c
1
此方程解x1=-7/8+√(33)/8 , x2=-7/8-√(33)/8
==============================================
import java.util.Scanner;
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a");
int a = sc.nextInt();
System.out.println("请输入b");
int b = sc.nextInt();
System.out.println("请输入c");
int c = sc.nextInt();
if(b*b - 4*a*c < 0){
System.out.println("对不起,无解!");
}else if(b*b - 4*a*c == 0){
System.out.println("此方程解x=" + -b/(2*a));
}else{
System.out.println("此方程解x1=" + ((-b+ "/"+ 2*a +"+√(" + (b*b - 4*a*c)) +")/" +(2*a)) + " , x2=" + ((-b+ "/"+ 2*a + "-√(" + (b*b - 4*a*c)) +")/" +(2*a)) );
}
}
}
请输入a
4
请输入b
7
请输入c
1
此方程解x1=-7/8+√(33)/8 , x2=-7/8-√(33)/8
==============================================
import java.util.Scanner;
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a");
int a = sc.nextInt();
System.out.println("请输入b");
int b = sc.nextInt();
System.out.println("请输入c");
int c = sc.nextInt();
if(b*b - 4*a*c < 0){
System.out.println("对不起,无解!");
}else if(b*b - 4*a*c == 0){
System.out.println("此方程解x=" + -b/(2*a));
}else{
System.out.println("此方程解x1=" + ((-b+ "/"+ 2*a +"+√(" + (b*b - 4*a*c)) +")/" +(2*a)) + " , x2=" + ((-b+ "/"+ 2*a + "-√(" + (b*b - 4*a*c)) +")/" +(2*a)) );
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a");
int a = sc.nextInt();
System.out.println("请输入b");
int b = sc.nextInt();
System.out.println("请输入c");
int c = sc.nextInt();
if(b*b - 4*a*c < 0){
System.out.println("对不起,无解!");
}else if(b*b - 4*a*c == 0){
System.out.println("此方程解x=" + -b/(2*a));
}else{
System.out.println("此方程解x1=" + ((-b+ "/"+ 2*a +"+√(" + (b*b - 4*a*c)) +")/" +(2*a)) + " , x2=" + ((-b+ "/"+ 2*a + "-√(" + (b*b - 4*a*c)) +")/" +(2*a)) );
}
}
}
public class Compare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a");
int a = sc.nextInt();
System.out.println("请输入b");
int b = sc.nextInt();
System.out.println("请输入c");
int c = sc.nextInt();
if(b*b - 4*a*c < 0){
System.out.println("对不起,无解!");
}else if(b*b - 4*a*c == 0){
System.out.println("此方程解x=" + -b/(2*a));
}else{
System.out.println("此方程解x1=" + ((-b+ "/"+ 2*a +"+√(" + (b*b - 4*a*c)) +")/" +(2*a)) + " , x2=" + ((-b+ "/"+ 2*a + "-√(" + (b*b - 4*a*c)) +")/" +(2*a)) );
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
其实你可以搜搜二分搜索算法 对你会更有用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询