我在哪里遇到错误的代码?我试图实现方法重载

时间:2015-05-10 13:55:43

标签: java methods

当我运行代码时,我会立即输入数字并输入数字,但之后没有任何反应!我试图在这里实现方法重载。任何帮助将不胜感激。

import java.util.Scanner;
public class MethodOverload {
 public static void main(String [] args){
     Scanner input = new Scanner(System.in);
     Scanner inputDoub = new Scanner (System.in);
     System.out.println ("Enter the int or double number");
     int x = input.nextInt();
     double y = inputDoub.nextDouble();
     //int x;
     //double y;
     System.out.printf("Square of integer value %d", x, "is", square(x));
     System.out.printf("Square of double value %f", y, "is", square(y));
      }

   public static int square(int intValue){
       System.out.printf ("\nCalled square method with int argument: %d", intValue);

       return intValue*intValue;
   }

   public static double square (double doubleValue){
       System.out.printf ("\nCalled sqauer method with double argument: %d", doubleValue);
       return doubleValue*doubleValue;
   }

}

2 个答案:

答案 0 :(得分:1)

    public class UDP_Server {
    private AsyncTask<Void, Void, Void> async;
    int udp;
    private boolean serverActive = true;
    InetAddress inetAddress;

    @SuppressLint("NewApi")
    public void runUdpServer(int udp1) {
        udp=udp1;
        async = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                byte[] lMsg = new byte[2048];
                DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
                DatagramSocket ds = null;
                My.saveLog("start receive ");

                try {
                    ds = new DatagramSocket(udp);

                    while (serverActive) {
                        My.saveLog("start receive2 ");
                        ds.setSoTimeout(0);
                        ds.receive(dp);
                        My.saveLog("start receive3");

                        ...

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (ds != null) {
                        ds.close();
                    }
                }

                return null;
            }
        };

        if (Build.VERSION.SDK_INT >= 11) async.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        else async.execute();
    }

    public void stop_UDP_Server() {
        serverActive = false;
    }
}

如果我理解正确你只想获得用户输入,如果用户输入双重使用一个重载方法,如果他输入一个整数使用另一个。上面的代码就是这样做的。

它只是将用户输入存储为double,如果用户输入modulo 1 = 0(意味着它是一个整数)将其转换为整数并调用重载方法传递整数参数。另外,在上一个重载方形方法中,你在printf函数中使用%d而不是%f,当你想要一个double时使用%f。

您的前两个printf语句也是错误的,语法只允许显示一个String,其他参数用于替换所有%符号。

答案 1 :(得分:0)

您尝试使用%d格式化双倍,这是不正确的。需要改变PFB:

    public DataTable DDLCity(Class1BO stateid)
    {

        SqlConnection cos = new SqlConnection("Data Source=admin-PC\\SQLEXPRESS;initial catalog=master;Integrated Security=True;");
        SqlDataAdapter sqd=new SqlDataAdapter("select cityid, cityname from city where stateid='" + stateid + "'", cos);
        DataTable dttablecity = new DataTable();

        sqd.Fill(dttablecity);
        return dttablecity;
    }

一个观察结果:使用2个独立的Scanner实例是没有意义的。没用 像这样纠正你的代码:

public static double square (double doubleValue){
    System.out.printf ("\nCalled sqauer method with double argument: %f", doubleValue);
    return doubleValue*doubleValue;
}