【问题标题】:How to invoke multiple method signatures within a main method?如何在主方法中调用多个方法签名?
【发布时间】:2014-12-12 00:41:51
【问题描述】:

所以我已经找到了三个必需的代码。它们是二次公式的代码、ISBN 检查器的代码和牛顿法的代码。我应该制作一个包含选项 1、2 和 3 的菜单,每个选项分别包含这些代码。

我想这意味着我需要不同的方法来解决这个问题,对吧?我从来没有真正被教导过——有人告诉我,我们必须始终将所有内容都放在 public class void main (String []args){ 中,而我只是被告知这有一些变化?

对于二次公式,信息是:返回 - void 和三个双精度数的参数,牛顿法:返回 - 双精度数和 1 个双精度数的参数,以及 ISBN 检查器:返回:布尔值和 1 个字符串的参数。我也不太了解参数的事情。帮助将不胜感激。我知道这在美学上看起来很糟糕,但是因为我现在的代码相对较短,所以我只是在完成后编辑样式。我也知道很多事情都是错误的,我花了很多时间试图弄清楚它们。

import Java.util.Scanner;

public class HelperMethod{

    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();

        if (input = 1){

        }else if (input = 2) {
            private class NewtonsMethod {

            public static void NewtonsMethod(String[] args) {   
                Scanner userInputScanner = new Scanner (System.in);
                double guess, fX, fPrimeX, newGuess;

                System.out.println ("enter in a value give"); 
                guess = userInputScanner.nextDouble();
                System.out.println ("Your guess is " + 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);
            }
        }
    }else{
        private class BookNumber {

            public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                char f;
                int e, g, h;
                int result = 0;

                System.out.println ("Pleas 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");
                }
            }
        }
    }

  }
}

【问题讨论】:

    标签: java methods


    【解决方案1】:

    首先,现在您在第一个 if 语句中将 input 设置为 1。要将输入与 1 进行比较,请使用 == 运算符,即 if (input == 1) {...。其次,你真的不需要使用类,你可以简单地拥有NewtonsMethod()BookNumber()等方法,并在输入正确时运行它们。

    public class HelperMethod{
        public static void main(String[] args) {
            int input;
    
            //Handle user input
    
            switch (input) {
                case 1:
                    newtonsMethod();
                    break;
                case 2:
                    bookNumber();
                    break;
                case 3: 
                    anotherMethod();
                    break;
            }
        }
    
        public static void newtonsMethod() {
            //Your code
        }
    
        public static void bookNumber() {
            //Your code
        }
    
        public static void anotherMethod() {
            //Your code
        }
    }
    

    【讨论】:

    • 在java中方法和非静态变量的命名遵循以小写字母开头的驼峰式命名。
    • 你当然是对的,我只是复制了原题的类名,忘记改了,谢谢。我已经编辑了我的答案。
    • 感谢您的帮助 - 但我可以将所有方法放在各自的情况下而不是放在后面吗?
    • 没问题。附带说明一下,由于您是 Stackoverflow 的新手,请参阅 Accepting Answers: How does it work?。回答您的问题:可以,请参阅Classes 以获得更多帮助。
    【解决方案2】:

    方法不应该在另一个内部。这就是课程的用途。方法是类中的一个元素。例如,在您的情况下,您的课程被命名为“HelperMethod”。您的方法需要在主方法的代码块用大括号“}”关闭后开始

    举个例子

    // This would be your main method.
    public static void main(String args[]) {
        // Your method is CALLED here.
        someMethod();
    {
    
    // Then this is where your next method would start.
    public static void someMethod() {
        // Your code for the method of course goes here.
    }
    

    当然,您需要设置类并且需要在 main 方法之上导入,但您已经正确设置了该设置。使用此设置,可以轻松调用其他类中的公共方法。除非您打算使用多个类,否则您的私有方法并不是真正需要的,此时您需要导入该类,然后像这样调用该方法

    SomeClass.someMethod();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      相关资源
      最近更新 更多