Mathew James Sideris Mathew James Sideris - 2 months ago 14
C# Question

Compile Error CS1001. Converting Java to C#

CS1001 = Identifier Expected

I took a snippet of code from Java that I would like to test in C#. It has a formula for calculating Experience needed to level up in a Video Game project I would like to use. I have just recently began teaching myself code, so converting this was trial and error for me, but I have eliminated the other 13 Errors and this one has me stuck.

Missing an identifier seems like a pretty rudimentary issue but it is also vague and i'm not sure when to begin to research. I have comment where the error occurs.

Any hints?

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

namespace ConsoleApplication1
{
class Program
{
float points = 0;
double output = 0; // Output, XP total at level
float minLevel = 2; // First level to Display
int maxLevel = 100; // Last Level to Display
int lvl = 0;

void Main()
{

for (lvl = 1; lvl <= maxLevel; lvl++)
{
points += Math.Floor(lvl + 300 * Math.Pow(2, lvl / 7.)); // Compile Error CS1001 at "));"
if (lvl >= minLevel)
Console.WriteLine("Level " + (lvl) + " - " + output + " EXP");
output = Math.Floor(points / 4);
}
}
}


}

Original JavaScript Code:

<SCRIPT LANGUAGE="JavaScript">
<!--
document.close();
document.open();
document.writeln('Begin JavaScript output:');
document.writeln('<PRE>');

points = 0;
output = 0;
minlevel = 2; // first level to display
maxlevel = 200; // last level to display

for (lvl = 1; lvl <= maxlevel; lvl++)
{
points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));
if (lvl >= minlevel)
document.writeln('Level ' + (lvl) + ' - ' + output + ' xp');
output = Math.floor(points / 4);
}

document.writeln('</PRE>');
document.close();
// -->
</SCRIPT>

Answer

That doesn't look like the only problem... :)

See inline comments:

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

namespace ConsoleApplication1
{
    class Program
    {
        float points = 0; 
        double output = 0;
        float minLevel = 2;
        int maxLevel = 100;
        int lvl = 0;

        void Main() //<-- Bad entry point public static void Main()
        {

            for (lvl = 1; lvl <= maxLevel; lvl++)
            {
                points += (float)Math.Floor(lvl + 300 * Math.Pow(2, lvl / 7.)); // <-- You have to explicitly specify the mantissa if you have a '.' is should be 7.0 or 7f
                            //^--- You also need to cast to a float here because the expression evaluates to a double
                if (lvl >= minLevel)
                    Console.WriteLine("Level " + (lvl) + " - " + output + " EXP");
                output = Math.Floor(points / 4);
            }
        }
    }
}