开发者

Invalid token errors with enums

I'm having a little trouble with this code that I'm writing for a simple program. I get tons of errors saying "invalid token".

The program basically asks for 2 integers and sums them up, but the program needs to be called in another method.

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

namespace AustinPDangeloJPA03
{
    class Add
    {
        static void Main(string[] arg开发者_JS百科s)
        {
            double num1,
                   num2,
                   sum;

            Console.Write("Enter the First integer: ");
            num1 = int.Parse(Console.ReadLine());
            //First Integer entered and storred 
            Console.Write("Enter the Second integer: ");
            num2 = int.Parse(Console.ReadLine());
            //Second Integer entered and storred
            sum = Display(double a, double b);
            //First and second numbers added together

            Console.WriteLine(" {0} + {1} = {2} ",num1,num2,sum); 
            //displays the sum

            //Instructs the user to press the Enter key to end the program
            Console.WriteLine("Press the Enter key to terminate the program...");
            Console.ReadLine();

            }//Closes Main Method

        static enum Display(a,b)
        {
            double c = a + b;
            return c;
        }//closes display method

    }//Closes Class Add
}


This is not correct:

static enum Display(a,b)
{
    double c = a + b;
    return c;

 }

The enum keyword is used to declare an enumeration. In order to define a method, you need a valid return type (such as int or double), and you need to provide the proper types for the individual arguments. You can optionally add static if you want it to be a static method, but that depends on its purpose.

I suspect you want to use something more like:

 double Add(double a, double b)
 {
     // ...

If you then correct the line that called this method:

 sum = Display(double a, double b);

This should compile and give you what you expect.


Your Display method is not declared correctly.

You need to declare a method that takes two numbers and returns a third number.
Consult your textbook and assignment for more information on how to declare a method and which types to use.

You're also not calling it correctly; method calls do not take types.


While it is not the source of your errors, it does indicate a misunderstanding of types:

double num1, num2,sum;
[...]
num1 = int.Parse(Console.ReadLine());

The first line declares some double variables.
The second line tries to parse int variables.

While the int will get auto-converted to a double, your code will be better if it is consistent with the use of types. You should switch to either int types, or to Double.Parse().


The enum keyword is for creating enumerations, for example:

public enum Color { Red, Green, Blue };

You have to specify a data type as return type for your Display method, and data types for the parameters:

static double Display(double a, double b) {
  double c = a + b;
  return c;
}

Also, you don't specify data types when you call the method, so change that to:

sum = Display(a, b);


Change this line to:

    double sum = Display(num1, num2);

And change your Display method to be a method.

    private static double Display(double a, double b)
    {
        double c = a + b;
        return c;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜