【问题标题】:Calculation (beginner)计算(初级)
【发布时间】:2014-08-31 07:59:15
【问题描述】:

我一直在努力做一个简单的计算器

    import java.util.Scanner;

    public class Math {

    static Scanner input = new Scanner(System.in);

    private static void Add(int a, int b,int c){

        System.out.println("First nummber...");
        a = input.nextInt();
        System.out.println("secound nummber...");
        b = input.nextInt();

        c = a + b;

        System.err.println(c);



    }

    public static void main(String[] args) {

        Add();

    }

}

但我收到此错误“Math 类型中的方法 Add(int, int, int) 不适用于参数 ()

然后我尝试将变量添加到主函数中的 Add()

public static void main(String[] args) {

    Add(a,b,c);

}

然后我收到此错误“此行有多个标记 - b 无法解析为变量 -c 无法解析为变量 - a 无法解析为变量 "

如何调用 Add() 函数?

【问题讨论】:

  • 我建议您阅读有关变量和参数声明的内容。尤其是在这里将c声明为参数是不合适的。

标签: java function add calculator


【解决方案1】:

因为你还没有声明 a、b 和 c。

像这样试试。

public static void main(String[] args) {

  Add(0,0,0);

 }

附带说明,您正在函数中再次读取 a 和 b。您可以在 main 中读取它们并将它们传递给函数。

 public static void main(String[] args) {

  Scanner scanner =  new Scanner(System.in);

  int a = scanner.nextInt();

  int b = scanner.nextInt();      

  int c =  Add(a,b);

  System.out.println("Addtion of " + a + " and " + b + " is " + c);

 }

然后您的 Add 函数将获得 a 和 b 并添加它。

  public static int Add(int a, int b){
     return a + b;
  }

【讨论】:

  • 您的新版本 Add() 缺少返回类型。
【解决方案2】:

所有其他答案都指出了这个问题,但没有提供我认为正确的解决方案。由于您要求用户输入,因此将变量声明为参数是不合适的。相反,您应该声明局部变量:

public class Math {
    static Scanner input = new Scanner(System.in);

    private static void Add(){
        System.out.println("First nummber...");
        int a = input.nextInt();
        System.out.println("secound nummber...");
        int b = input.nextInt();

        int c = a + b;

        System.err.println(c);
    }

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

【讨论】:

  • 从你的方法中移除参数
  • @practice2perfect 完成。我错过了复制/粘贴中最重要的细节。感谢您指出。
【解决方案3】:
import java.util.Scanner;

public class Math {

static Scanner input = new Scanner(System.in);

private static void Add(){ //do not use arguments here for just calling it 

    System.out.println("First nummber...");
    int  a = input.nextInt();
    System.out.println("secound nummber...");
    int b = input.nextInt();

    int c = a + b;

    System.err.println(c); //use System.out.println(c);



}

public static void main(String[] args) {

    Add();

}

}

根据您的代码,修改它会起作用

【讨论】:

    【解决方案4】:

    您已将 Add() 声明为接受 3 个参数的方法:Add(int a, int b, int c) 并且您没有调用它。

    更改方法签名:

    private static void Add(int a, int b,int c)
    

    到:

    private static void Add()
    

    除此之外,您还必须在方法中声明 a,b,c:

    public static void main(String[] args) {
        Add();
    }
    
    private static void Add(){
        System.out.println("First number...");
        int a = input.nextInt();
        System.out.println("second number...");
        int b = input.nextInt();
        int c = a + b;
        System.out.println(c);
    } 
    

    旁注
    不要使用System.err.打印调试打印,使用System.out.println()

    【讨论】:

    • 感谢您的快速回答 :) 但我尝试将整数添加到方法 Add(int a, int b, int c) 中,我收到此错误“此行有多个标记 - 语法错误在标记“int”上,删除此标记 - 标记“int”上的语法错误,删除此标记 - 标记“int”上的语法错误,删除此标记 - a 无法解析为变量 - b 无法解析为变量 - c 无法解析为变量“
    【解决方案5】:

    你可以像这样重写你的代码。

    import java.util.Scanner;
    
    public class Math {
    
       static Scanner input = new Scanner(System.in);
    
       private static int Add(int a, int b){    
           return a + b;
       }
    
       public static void main(String[] args) {
           System.out.println("First nummber...");
           int a = input.nextInt();
           System.out.println("secound nummber...");
           int b = input.nextInt();
           int c = Add(a,b);
           System.out.println(c);        
    
       }
    

    }

    但是从您的代码中,我可以看出您对 java 很陌生,也许对编程很陌生。所以我会建议你先学习基本的java和编程。 您可以在线学习

    或者可以看一些书

    【讨论】:

      【解决方案6】:

      ADD(1,2,3) 会起作用 ADD() 不会

      你已经用 3 个参数声明它并且你没有提供它

      【讨论】:

      • 由于参数值会被忽略,所以最好将参数全部删除。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 2018-03-13
      • 2021-12-14
      • 2012-01-31
      相关资源
      最近更新 更多