java - 在 switch 语句中调用类?

标签 java class methods switch-statement

有三个程序可以运行不同的功能,我被告知根据用户输入的编号选项来调用它们。首先,我将程序放在最后添加 switch 语句之前。我在 switch 语句中遇到了不同的错误 - 对于每种情况,我到底需要在这些括号中放入什么?参数?

我收到错误,包括预期的错误类。

import java.util.Scanner;
import java.lang.Math;

public class HelperMethod{
    public static boolean BookNumber(String a) {
        char f;
        int e, g, h;
        int result = 0;

        System.out.println ("Please enter a thirteen digit number");
        String a = scanner.nextLine();

        if (a.length() == 13){
            for (int i = 0; i < 13; i ++) {
                f = a.charAt(i);
                e = Character.digit(f, 10); 
                if (i % 2 == 0) {
                    g = e * 1;
                    result = result + g;
                } else {
                    g = e * 3;
                    result = result + g;
                }
            }
            System.out.println ("The added sum of you numbers is " + result);
            if (result % 10 == 0) {
                System.out.println ("This combination IS a ISBN number");
            } else {
                System.out.println ("This is NOT an ISBN number");
            } 
        } else {
            System.out.println ("This combination is not thirteen digits long");
        }
    }

    public static boolean NewtonsMethod (double guess) {
        double guess, fX, fPrimeX, newGuess;

        System.out.println ("enter in a value give"); 
        guess = userInputScanner.nextDouble();
        System.out.println ("Your guess is " + guess);
         double guess; 

        while (true) {
            fX = (6 * Math.pow (guess,4)) - (13 * Math.pow (guess,3)) - (18 * Math.pow (guess,2)) + (7 * guess) + 6;
            fPrimeX = (24 * Math.pow (guess,3)) - (39 * Math.pow (guess,2)) - 36 * guess + 7;
            newGuess = guess - (fX / fPrimeX);
            System.out.println ("A possible root is " + newGuess);
            if (Math.abs(newGuess - guess) < 0.00001) {
                break;
            } else {
                guess = newGuess;
            }
        }
        System.out.println ("The root is: " + newGuess);
    }

    public static void QuadraticFormula (double a, double b, double c) {
        Scanner keyboard = new Scanner(System.in);

        // Input 
        System.out.println("Please enter an a value:");
        double a = keyboard.nextDouble();
        System.out.println("Please enter a b value:");
        double b = keyboard.nextDouble();
        System.out.println("Please enter a c value:");
        double c = keyboard.nextDouble();

        // Calculations
        double discriminant = b * b - 4 * a * c;
        double rootOne = 0, rootTwo = 0;

        // Option A
        if (discriminant < 0) {
            System.out.println("There are no roots!");
        } else if (discriminant > 0){
            rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
            rootTwo = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.println("Your roots are: " + rootOne + " and " + rootTwo);
        } else {
            rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
            System.out.println("Your unique root is: " + rootOne);
        }

        // Option B
        if (discriminant < 0) {
            System.out.println("There are no roots!");
        } else {
            rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
            rootTwo = (-b - Math.sqrt(discriminant)) / (2 * a);
            if (discriminant > 0) {
                System.out.println("Your roots are: " + rootOne + " and " + rootTwo);
            } else {
                System.out.println("Your unique root is: " + rootOne);
            }
        }
    }

    public static void main (String[] args) {
        Scanner userInputScanner = new Scanner (System.in);

        System.out.println ("You have three options. press one for the quadratic Formula, 2 for the newtons Method, and 3 for an ISBN checker.");
        int input = userInputScanner.nextInt();

        switch (input) {
            case 1:
                NewtonsMethod (double guess);
                break;
            case 2:
                BookNumber(String a);
                break;
            case 3: 
                QuadraticFormula (double a, double b, double c);
                break; 
        }
    }
}

最佳答案

是的,这里有一些语法错误:

NewtonsMethod(double guess);

您正在尝试调用一个方法,但这看起来像一个声明。您在其他地方有正确的方法调用,例如:Math.sqrt(discriminant)。所以应该看起来像:

NewtonsMethods(guess);

但是,这些方法看起来根本不应该接受参数。

public static boolean NewtonsMethod (double guess) {
    double guess, fX, fPrimeX, newGuess; 
    System.out.println ("enter in a value give"); 
    guess = userInputScanner.nextDouble();
    ...

当您调用它们时,您没有任何东西可以传递给它们,并且您在方法体内收集这些变量的用户输入。这会导致您声明的方法局部变量与这些参数发生冲突,从而导致更多语法错误。因此,只需不要向它们传递任何参数,您的声明将类似于:

public static boolean NewtonsMethod (double guess) {

您的返回类型也有问题。您使用 boolean 返回类型声明方法,但从不返回值。似乎不太需要在这里返回 boolean 值,所以我只是给它们一个 void 返回类型:

public static void NewtonsMethod (double guess) {

之后,我只看到其他三个语法错误。您在两个地方尝试使用尚未初始化的扫描仪。不过,您可以在 QuadraticFormulamain 中初始化它们,因此请遵循该模式。此外,您已在牛顿方法中声明了变量 guess 两次。解决这个问题。我相信,这至少应该让你编译。我主要把逻辑错误留给你,除了这个让我直接跳出来的错误:你告诉用户按下的数字并没有按照你说的那样做。

关于java - 在 switch 语句中调用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494305/

相关文章:

java - Spring Roo 的替代用法

java - Java 通配符类型的下限和上限

java - Maven 在 CI 服务器上并发构建

java - Standalone hibernate 和 narayana JTA 独立平台

html - 什么是 page-wrap div,它有什么用?

java - 如果方法没有被重写,是否应该将其声明为 final方法?

Java:对重复的代码部分使用静态方法

python - User 类应该是不可变的吗?

C++ 大整数类运算符=

java - 如何将文本打印到文本区域