【问题标题】:Method for quadratic formula won't give output and errors二次公式的方法不会给出输出和错误
【发布时间】:2023-03-08 03:36:01
【问题描述】:

当我尝试为二次公式创建方法时,它不会给我任何输出,而且我一直在丢失精度错误。我目前需要任何帮助,因为我似乎无法弄清楚。这是我的代码:

import java.util.Scanner;

public class HelperMethod {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Pick an option:");
    System.out.println("Option 1: Quadratic Formula");
    System.out.println("Option 2: Newtons Method");
    System.out.println("Option 3: ISBN checker");
    int option = keyboard.nextInt();

    if(option == 1){
        System.out.print("Please enter an 'a' value:");
        double a = keyboard.nextDouble();
        System.out.print("Please enter a 'b' value:");
        double b = keyboard.nextDouble();
        System.out.println("Please enter 'c' value:");
        double c = keyboard.nextDouble();
    }
}
public int quadraticFormula(double a, double b, double c, boolean returnSecond){
    return (-b + Math.sqrt(b * b - 4.0 * a * c))/(2.0 * a);
}
}

输出:没有回答我的问题

Pick an option:
Option 1: Quadratic Formula
Option 2: Newtons Method
Option 3: ISBN checker
1
Please enter an 'a' value:2
Please enter a 'b' value:3
Please enter 'c' value:
4

Process completed.

【问题讨论】:

  • 公共双二次公式(>>>>)

标签: java methods return


【解决方案1】:

当您使用双打进行数学运算时,您正试图返回一个“int”。这就是您失去精度的原因。

【讨论】:

    【解决方案2】:

    你的方法应该返回一个double。并将您的 ints 十进制数字设为 4.0 而不是 4。这将有助于提高精度。

    编辑:调用方法

    由于您尝试从 main 调用该方法,因此您也必须将其设为静态

    public static int quadraticFormula(double a, double b, double c, boolean returnSecond){
        return (-b + Math.sqrt(b * b - 4.0 * a * c))/(2.0 * a);
    }
    

    然后确保从main 调用它以获取输出

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Pick an option:");
        System.out.println("Option 1: Quadratic Formula");
        System.out.println("Option 2: Newtons Method");
        System.out.println("Option 3: ISBN checker");
        int option = keyboard.nextInt();
    
        if(option == 1){
            System.out.print("Please enter an 'a' value:");
            double a = keyboard.nextDouble();
            System.out.print("Please enter a 'b' value:");
            double b = keyboard.nextDouble();
            System.out.println("Please enter 'c' value:");
            double c = keyboard.nextDouble();
        }
    
        System.out.println(quadraticFormula(a, b, c));
    }
    

    编辑:方法返回 void

    public static void quadraticFormula(double a, double b, double c){
        double quad = -b + Math.sqrt(b * b - 4.0 * a * c))/(2.0 * a)
        System.out.println(quad);
    }
    
    public static void main(String[] args){
        quadraticFormula(a, b, c);
    }
    

    【讨论】:

    • 一般来说,4 --> 4.0 建议很好,但在这种特定情况下它不会做任何事情。任何有int 的地方,都有一个double 乘以或除以。
    • 不,在这种情况下不是。
    • 好吧,它修复了错误,但它没有给我输出(输出在帖子上)
    • @user2809115 确保调用方法:System.out.println(quadraticFormula(a, b, c));
    • 好吧,我希望我的方法无效,我该怎么做?就像使用二次公式
    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2021-09-06
    • 2020-07-06
    • 2016-01-08
    相关资源
    最近更新 更多