简体   繁体   English

Java中的返回类型和方法

[英]Return types and Methods in java

Can you guys help me fix the following program? 你们可以帮我修复以下程序吗?

It is giving me the following error: 它给了我以下错误:

error: method absoluteValue in class Pset3Ex4 cannot be applied to given types; 错误:Pset3Ex4类中的方法absoluteValue无法应用于给定类型;

import java.util.Scanner;

public class Pset3Ex4 {

    public static void main(String[] args) {
        absoluteValue();
    }

    public double absoluteValue(double d) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Input a number: ");
        d = sc.nextDouble();

        if (d < 0) {
            return -d;
        } else {
            return d;
        }
    }
}

You are calling method 您正在呼叫方法

 absoluteValue() 

without any argument, while you have defined a method with a double argument 没有任何参数,而您定义了带有双参数的方法

absoluteValue(double d), 

So java compiler did not find the method 所以java编译器没有找到方法

absoluteValue() 

without argument. 没有争论。

Second, you are trying to call a non-static method from the static main method which is not permitted. 其次,您试图从不允许的静态main方法中调用非静态方法。

Your absoluteValue(double d) is neither static and does not take zero arguments. 您的absoluteValue(double d)既不是静态的,也不接受零参数。 Therefore you will get an error when you try to compile this. 因此,当您尝试编译它时,您将得到一个错误。

To fix that, just change the signature and modifiers for absoluteValue : 要解决此问题,只需更改absoluteValue的签名和修饰符:

public static double absoluteValue() {
    double d;
    ...

First, you need to pass an argument to that function (a double argument!) Second, this function need to be static, it is a part of a class that has not been instanced yet. 首先,您需要将一个参数传递给该函数(一个双参数!)。第二,此函数需要是静态的,它是尚未实例化的类的一部分。 If you make a new instance, meaning : 如果创建一个新实例,则意味着:

 Pset3Ex4 p = new Pset3Ex4();

You'll be able to call the function from p (the new instance). 您将能够从p(新实例)调用该函数。 Otherwise, you'll have to make this method static, it will allow you to access it without having to make new instance of it. 否则,您将必须使此方法为静态,它将允许您访问它而不必创建新的实例。

  1. Ths signature of the function is absoluteValue(double d) so it requires an double as parameter, but you call it like absoluteValue(); 该函数的签名为absoluteValue(double d)因此它需要一个double作为参数,但您可以像absoluteValue();那样调用它absoluteValue(); without. 没有。 You need to set you function signature to 您需要将功能签名设置为

     public double absoluteValue(double d){ //... } 
  2. Your function is not static, it requires a instance to be called, so : 您的函数不是静态的,它需要调用一个实例,因此:

    • use an instance 使用实例

       public static void main(String[] args){ new Pset3Ex4().absoluteValue(); } 
    • make it static 使其静态

       public static double absoluteValue(double d){ //... } 

Check if this what you are looking at. 检查是否您正在查看。 Method 'absoluteValue' is not static and you are not passing any argument when you are are calling the method in main. 方法“ absoluteValue”不是静态的,并且在main中调用该方法时不会传递任何参数。

import java.util.Scanner;

public class Pset3Ex4 {

public static void main(String[] args){
System.out.print("Result is " + absoluteValue());
}

public static double absoluteValue(){
Scanner sc = new Scanner(System.in);

System.out.print("Input a number: ");
d = sc.nextDouble();

if (d < 0){
return - d; 
}
else {
return d;
}

}  
}

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

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