方法调用的问题 [英] Problems with method call

查看:66
本文介绍了方法调用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种方法但我得到2个错误



错误1(方法''calculatePay''没有重载需要0个参数)

错误2(方法''DisplayPaySlip''没有重载需要0个参数)



I have 3 methods but i get 2 errors

Error 1 (No overload for method ''calculatePay'' takes 0 arguments)
Error 2 (No overload for method ''DisplayPaySlip'' takes 0 arguments)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prac4_s208050509_
{
    class Program
    {
        private static string message;
        static void Main(string[] args)
        {
            string calc;

            getDetails();
           
           calc= calculatePay();
           Console.WriteLine(calc);
            DisplayPaySlip();
           // Console.WriteLine(message);
           // Console.ReadLine();
            
        }
        public static void getDetails()
        {
            string fullName;
            string employeeNumber;
            double noOfHoursWorked;
            double hourlyRate;

            //I've set the hourlyRate to 50 for there was no default hourly rate given
            int count;
            Console.WriteLine("Please enter the number of employees");
            count = int.Parse(Console.ReadLine());

          for(int i =0;i<= count;i++)
            {
                Console.Write("Please enter your full name:");
                fullName = Console.ReadLine();
                Console.Write("Please enter your employee number:");
                employeeNumber = Console.ReadLine();
                Console.Write("Please enter the amount of hours worked:");
                noOfHoursWorked = double.Parse(Console.ReadLine());
                Console.Write("Please enter your hourly rate:");
                hourlyRate = double.Parse(Console.ReadLine());
               // count++;
            }
            //hourlyRate ;
        }
        public static  void calculatePay(ref double hourlyRate,ref double noOfHoursWorked)
        {
            //double overtime;
            double weeklyPay;
            if (noOfHoursWorked <= 40)
            {
                weeklyPay = noOfHoursWorked * hourlyRate;
                Console.WriteLine("Weeekly Pay:",weeklyPay);
                Console.ReadLine();
            }
            else
            {
                weeklyPay = (hourlyRate * 1.5)*noOfHoursWorked;
                Console.WriteLine("Overtime Pay",weeklyPay);
                Console.ReadLine();
                
            } 
            
        }
          public static void DisplayPaySlip(  string fullName, double noOfHoursWorked, string employeeNumber,string hourlyRate, double weeklyPay)
        {
            string message;
            message="Employee Number"+employeeNumber;
            message += "Full Name"+fullName;
            message += "Hours Worked"+noOfHoursWorked;
            message += "Hourly Rate"+hourlyRate;
            message += "Weekly Pay"+weeklyPay;

            Console.WriteLine(message);
            Console.ReadLine();
            

        }
    }
}

推荐答案

看看你的代码:

Look at your code:
calc= calculatePay();
...
public static  void calculatePay(ref double hourlyRate,ref double noOfHoursWorked)





And

DisplayPaySlip();
...
  public static void DisplayPaySlip(  string fullName, double noOfHoursWorked, string employeeNumber,string hourlyRate, double weeklyPay)



你已经声明了moth方法来获取参数,但是在调用方法时你没有提供它们。由于参数对于完成该方法的工作非常重要,因此在调用方法时,需要找到它们并提供它们!我也非常确定你想要从CalculatePay方法中返回一个值,而不是返回一个void - 并且没有充分的理由让你的参数 ref 不要(也不应该)改变它们。


You have declared moth methods to take parameters, but you are not supplying them when you call the methods. Since the parameters look pretty important to doing the job the method is intended to do, tyou need to find them and supply them when you call the methods! I''m also pretty sure you want to return a value from teh CalculatePay method, rather than return a void - and there is no good reason for making the parameters ref since you don''t (and shouldn''t) change them.

double rate = 15.0;
double hours = 48.0;
calc = calculatePay(ref rate, ref hours);
DiaplyPaySlip("Joe Blogs", 48, "666", "15.0", calc);





BTW:为什么是你的hourlyRate DisplayPaySlip方法的字符串?当然最好像使用CalculatePay一样使用双倍?



BTW: Why is your hourlyRate a string for the DisplayPaySlip method? Surely it would be better to use a double like you do for CalculatePay?






你应该传递输入参数对于这两种方法。



Hi,

you should pass the input parameters for both the methods.

calculatePay(hourlyRate, noOfHoursWorked)

DisplayPaySlip(fullName, noOfHoursWorked, employeeNumber, hourlyRate, weeklyPay)





getDetails()工作正常,因为没有输入参数。



Bye。



And getDetails() works fine because there is no input parameters.

Bye.


错误信息清晰且非常简单:您在不提供必要参数的情况下调用方法。

例如,方法 calculatePay 获取两个参数(即 hourlyRate noOfHoursWorked )通过它没有一个。



要解决这个问题,你可以:

  • 制作 calculatePay getDetails 方法直接调用(因为后者持有必要的信息)
The error message is clear and pretty straightforward: you are calling the methods without providing the necessary arguments.
For instance, the method calculatePay takes two arguments (namely hourlyRate and noOfHoursWorked) while you are passing it none of them.

To fix the issue you may:
  • Make calculatePay directly called by getDetails method (since the latter holds the necessary info)
  • 修改 getDetails 方法,以便将收集的数据提供给班级的其他成员(例如您可能有一个雇员列表作为类成员,并使 getDetails 填写它。
  • Modify getDetails method in order to make collected data available to other members of the class (for instance you may have a list of Employes as class member, and make getDetails fill it).


这篇关于方法调用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆