Java 错误:类中的方法不能应用于给定类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28445862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:14:21  来源:igfitidea点击:

Java error: method in class cannot be applied to given types

java

提问by Klutchxv

I am just trying to call the methods to the main for each switch when it happens, but i just get the error message everytime i try to call any methods, not trying to return anything. ex. if the user enters a or A i want to call the add method to main

我只是想在每次切换时将方法调用到 main 中,但是每次我尝试调用任何方法时都会收到错误消息,而不是尝试返回任何内容。前任。如果用户输入 a 或 A 我想调用 add 方法到 main

public static void main(String[] args) 
{ 
    char character; 

    Scanner keyboard = new Scanner(System.in); 

    while (character != 'E' || character != 'e') 
    {

    System.out.println(" A:Addition \n S:Subtraction \n M:Multiplication \n D:Division \n R:Modulus \n E:exit");
    switch (character)
    {
        case 'a':
        case 'A':
            System.out.println("your choice A");
            add(); 
            break; 

        case 's':
        case 'S':
            System.out.println("your choice S");
            subtraction();
            break; 

        case 'm':
        case 'M':
             System.out.println("your choice M");
             multiplication();
             break; 

        case 'd':
        case 'D':
             System.out.print("your choice D");
             division();
             break;

        case 'r':
        case 'R':
             System.out.println("your choice R");
             modulus();
             break;

        default: 
             System.out.println("Error: please enter a valid letter");
             break;

    } 
   }
 } 
public static void add(Scanner keyboard) 
{
    int a,b; 
    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();

     int total = a + b; 
    System.out.println(a + "plus" + b + "is" + total );
}
public static void subtraction(Scanner keyboard) 
{
    int a,b;

    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();
     int total = a-b;
    System.out.println(a + "minus" + b + "is " + total);
}
public static void multiplication(Scanner keyboard) 
{
    int a,b; 
    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();

    int total = a*b; 
    System.out.println(a + "times" + b + "is " + total);
}
public static void division(Scanner keyboard) 
{
    int a,b;

    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();
    int total = a/b;
    System.out.println(a + "divided" + b + "is " + total);
}
public static void modulus(Scanner keyboard) 
{
    int a,b;

    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();
    int total= a%b; 
    System.out.println(a + "modulus" + b + "is " + total);

    System.out.println("The program is terminating");
 }

}

}

回答by Vivin

You have defined the method which takes Scanner as argument but you are calling the methods with no args.

您已经定义了以 Scanner 作为参数的方法,但您正在调用没有参数的方法。

回答by Jean-Fran?ois Savard

All the method you are using are supposed to receive a Scannerobject while you pass no argument.

您使用的所有方法都应该Scanner在您不传递任何参数时接收一个对象。

For example you call add();while it signature is

例如,您add();在签名时调用

public static void add(Scanner keyboard)

Which is why you get the error.

这就是您收到错误的原因。

Instead, use add(keyboard)and repeat the same for substraction, multiplication, division and modulus methods.

相反,add(keyboard)对减法、乘法、除法和取模方法使用并重复相同的方法。

So that your switchwould now look like

所以你switch现在看起来像

switch (character) {
    case 'a':
    case 'A':
        System.out.println("your choice A");
        add(keyboard);
        break;

    case 's':
    case 'S':
        System.out.println("your choice S");
        subtraction(keyboard);
        break;

    case 'm':
    case 'M':
        System.out.println("your choice M");
        multiplication(keyboard);
        break;

    case 'd':
    case 'D':
        System.out.print("your choice D");
        division(keyboard);
        break;

    case 'r':
    case 'R':
        System.out.println("your choice R");
        modulus(keyboard);
        break;

    default:
        System.out.println("Error: please enter a valid letter");
        break;

}

回答by Dyrandz Famador

you're calling the method but you didn't include an argument

您正在调用该方法,但没有包含参数

take a look at this.

看看这个。

public static void add(Scanner keyboard) 

you have an argument, so you must include an argument when calling this method

您有一个参数,因此在调用此方法时必须包含一个参数

so

所以

you must call the method like this.

你必须像这样调用方法。

add(keyboard);

回答by Sivaramvt

You are missing the arguments in the method call.

您缺少方法调用中的参数。

 case 'a':
    case 'A':
        System.out.println("your choice A");
        add(keyboard); // Add arguments.
        break;