简体   繁体   English

方法调用和类内的类

[英]Method call and a class inside a class

I'm in a college summer class that is going super fast and I'm extremely new to programming in general (be nice haha). 我上的一门大学暑期课程进展很快,而且我对编程总体上还是陌生的(哈哈)。

One of our assignments this week is to create a simple calculator. 本周的任务之一是创建一个简单的计算器。 Here are a few of the instructions. 以下是一些说明。

"The Driver class is the only class with main(String[] args)method.Driver class should call a method in the Menu class to print the menu, and from that method there should be calls to methods in Actions class for respective functionality from the menu." “ Driver类是唯一具有main(String [] args)方法的类。Driver类应调用Menu类中的方法以打印菜单,并且从该方法中应调用Actions类中的方法以从菜单。”

My code is no where near completed I'm just trying to figure out what I'm doing wrong with calling the getInput method that's inside the Menu class. 我的代码几乎没有完成,我只是想弄清楚我在调用Menu类内的getInput方法时做错了什么。 Per the instructions it seems like the method call needs to be in the Driver class. 按照说明,方法调用似乎需要在Driver类中。 However, I'm getting a few errors when I try and compile. 但是,在尝试编译时出现了一些错误。

Driver.java:6: error: cannot find symbol
        String user = getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");
              ^
  symbol:   method getInput(String)
  location: class Driver
Driver.java:11: error: cannot find symbol
        String s1 = getInput("Enter a numeric value: ");
                ^
  symbol:   method getInput(String)
  location: class Driver
Driver.java:12: error: cannot find symbol
        String s2 = getInput("Enter a numeric value: ");
                ^
 symbol:   method getInput(String)
 location: class Driver
Driver.java:14: error: non-static variable user cannot be referenced from a static context
        int userInt = Integer.parseInt(user);
                                   ^
Driver.java:70: error: Illegal static declaration in inner class Driver.Menu
        public static String getInput(String prompt) {
                             ^
  modifier 'static' is only allowed in constant variable declarations
5 errors

................ ................

import java.util.Scanner;
import java.io.*;

public class Driver {

String user = getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");

public static void main(String[] args) {


    String s1 = getInput("Enter a numeric value: ");
    String s2 = getInput("Enter a numeric value: ");

    int userInt = Integer.parseInt(user);
    double result = 0;

    switch (userInt) {
    case 1:
        result = addValues(s1, s2);
        break;
    case 2:
        result = subtractValues(s1, s2);
        break;
    case 3:
        result = multiplyValues(s1, s2);
        break;  
    case 4:
        result = divideValues(s1, s2);
        break;  

    default:
        System.out.println("You entered an incorrect value");
    }

    System.out.println("The answer is " + result);
}

private static double divideValues(String s1, String s2) {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 / d2;
    return result;
}

private static double multiplyValues(String s1, String s2) {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 + d2;
    return result;
}

private static double subtractValues(String s1, String s2) {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 - d2;
    return result;
}

private static double addValues(String s1, String s2)
        throws NumberFormatException {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 + d2;
    return result;
}



public class Menu { 
    public static String getInput(String prompt) {
        String user;
        Scanner scan = new Scanner(System.in);
        System.out.println(prompt);
        return user = scan.nextLine();
    }
}

} }

This post is already pretty long so I won't post the rest of the instructions unless you'd like me to. 这篇文章已经很长了,因此除非您愿意,否则我不会发布其余说明。

getInput() is a static method in another class, you should call it as follows: getInput()是另一个类中的静态方法,应按以下方式调用它:

String s1 = Menu.getInput("Enter a numeric value: ");
//            ^   the name of the class should come before the method name 

The javadoc says - Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in javadoc说- 静态方法(在声明中具有static修饰符)应使用类名称来调用,而无需创建该类的实例,例如

ClassName.methodName(args)

So you have to change 所以你必须改变

String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");

to

String s1 = Menu.getInput("Enter a numeric value: ");
String s2 = Menu.getInput("Enter a numeric value: ");

Moreover you can also refer to static methods with an object reference like 此外,您还可以使用对象引用来引用静态方法,例如

instanceName.methodName(args)

Edit: 编辑:

Menu class 菜单类

class Menu {
    public static String getInput(String prompt) {
        String user;
        Scanner scan = new Scanner(System.in);
        System.out.println(prompt);
        return user = scan.nextLine();
    }
}

Driver class 驾驶舱

public class Driver {
   public static void main(String[] args) {
     String user = Menu.getInput("Enter: 1=Add  2=Subtract 3=Multiply 4=Divide");
     String s1 = Menu.getInput("Enter a numeric value: ");
     String s2 = Menu.getInput("Enter a numeric value: ");
        //rest of the codes same
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM