m0_73634697 2023-01-05 17:05 采纳率: 33.3%
浏览 41

java桌面应用程序

设计一款求解一元二次方程的桌面应用程序 开发工具:Netbeans 8.2+jdk1.8
封装方程根信息的 Java Bean:
/*

  • To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates
  • and open the template in the editor.
  • /
  • 写回答

1条回答 默认 最新

  • Echo-Niu 2023-01-06 08:26
    关注
    
    import java.util.Scanner;
    
    public class QuadraticEquation {
        private double a;
        private double b;
        private double c;
    
        public QuadraticEquation(double a, double b, double c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    
        public double getA() {
            return a;
        }
    
        public double getB() {
            return b;
        }
    
        public double getC() {
            return c;
        }
    
        public double getDiscriminant() {
            return Math.pow(b, 2) - 4 * a * c;
        }
    
        public double getRoot1() {
            if (getDiscriminant() < 0) {
                return 0;
            } else {
                return (-b + Math.sqrt(getDiscriminant())) / (2 * a);
            }
        }
    
        public double getRoot2() {
            if (getDiscriminant() < 0) {
                return 0;
            } else {
                return (-b - Math.sqrt(getDiscriminant())) / (2 * a);
            }
        }
    }
    
    public class QuadraticEquationTest {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a, b, c: ");
            double a = input.nextDouble();
            double b = input.nextDouble();
            double c = input.nextDouble();
    
            QuadraticEquation equation = new QuadraticEquation(a, b, c);
            double discriminant = equation.getDiscriminant();
    
            if (discriminant > 0) {
                System.out.println("The roots are " + equation.getRoot1() + " and " + equation.getRoot2());
            } else if (discriminant == 0) {
                System.out.println("The root is " + equation.getRoot1());
            } else {
                System.out.println("The equation has no real roots");
            }
        }
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 1月5日