方法未被调用

时间:2016-02-21 17:06:42

标签: java python

我有一些使用Python的经验,所以我一直在尝试通过编写我在Java中用Python编写的相同程序来学习Java。

我有这个函数,我输入两个整数,它返回总和。如果整数相同,则返回两倍的总和。例如,5 + 5 = 20。 我有这个函数的以下代码。

public class sumDouble
{
   public int sumDouble(int a, int b) {
       int sum = a + b;

       if (a == b) {
           sum = sum * 2;
        }
        return sum;
    }
}

接下来,我想写一个脚本,我要求用户输入两个整数,主类调用这个函数。我有以下代码。我哪里做错了?

import java.util.Scanner;
public class GetSumFromUser
{
  public static void main (String[] args){

      Scanner in = new Scanner(System.in);

      int a;
      int b;
      int sumDouble;
      sumDouble sum = new sumDouble();

      System.out.println("Please enter an integer.");
      a = in.nextInt();
      System.out.println("You entered "+a);

      System.out.println("Please enter another integer.");
      b = in.nextInt();
      System.out.println("You entered "+b);

      System.out.println("Your sum is "+sum);

    }
}

在最后一行,输出显示"你的总和是sumDouble @ 1777aec"。

6 个答案:

答案 0 :(得分:1)

您实际上从未调用过sumDouble()方法。而不是打印sumObject),你应该这样打印:

System.out.println("Your sum is "+sum.sumDouble(a,b));

答案 1 :(得分:1)

试试这个:

public class SumDouble
{
   public static int sumDouble(int a, int b) {
       int sum = a + b;

       if (a == b) {
           sum = sum * 2;
        }
        return sum;
    }
}

...

System.out.println("Your sum is "+SumDouble.sumDouble(a, b));

答案 2 :(得分:0)

你从sumDouble类得到一个对象,但你永远不会调用它的sumDouble方法:

sumDouble sum = new sumDouble();

将其更改为:

sumDouble sd = new sumDouble();
int sum = sd.sumDouble(a,b);

答案 3 :(得分:0)

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;

    import java.util.Scanner;

     class sumDouble
{
    public  int sumDouble(int a,int b)
        {
            int sum=a+b;                 //add the numbers
            if(a==b)                     //check if both numbers are same
                sum=sum*2;              //double th value if same
            return sum;                 //return sum
        }
}

    public class GetSumFromUser
    {


      public static void main (String[] args){

          Scanner in = new Scanner(System.in);
    sumDouble s=new sumDouble();
          int a;
          int b;
          int sum;

          System.out.println("Please enter an integer.");
          a = in.nextInt();
          System.out.println("You entered "+a);

          System.out.println("Please enter another integer.");
          b = in.nextInt();
          System.out.println("You entered "+b);
       sum= s.sumDouble(a, b) ;                    //call the sum double function

          System.out.println("Your sum is "+sum);

        }
    }

答案 4 :(得分:0)

如果你print(sum),那么你正在打印对象......

改为

System.out.println("Your sum is "+sum.sumDouble(a,b));

答案 5 :(得分:-1)

System.out.println("Your sum is "+sum.sumDouble(a,b));

将其更改为:

let secondVC = self.storyboard.instantiateViewControllerWithIdentifier("secondVC") as! secondVC
secondVC.stringName = "Value"
self.navigationController.pushViewController(secondVC, animated: true)

您尚未调用此方法。 “你的总和是”+总和 - 这将调用总和的toString方法,即sumDouble @ 1777aec。