Compile Error CS1001. Converting Java to C# [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
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>

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);
}
}
}
}

Related

Why is division's answer wrong in c#? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 6 months ago.
I'm fifteen and I'm new in this job.I try to learn this language for my future life.I entered 1,2,3,4,5. Finally he says conclusion is zero i searched but i couldn't find anything.can you help me please.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project3
{
internal class Program
{
static void Main(string[] args)
{
//variable assigment
int evennumber = 0;
int oddnumber = 0;
float ratio;
//its for get numbers
for(int i = 1; i <= 5; i++)
{
Console.WriteLine("Enter" + i ". number : ");
int number=Convert.ToInt32(Console.ReadLine());
//its for determine even and odd number
if (number%2 == 0)
{
evennumber = evennumber + number;
}
else
{
oddnumber = oddnumber + number;
}
}
ratio =evennumber/oddnumber;
Console.WriteLine("Ratio : " + ratio);
Console.Read();
}
}
}

How to align the output as shown in screenshot using C#?

As a Newbie to C#, I have some code in C#.
The Program is a kind of Daily Money Saving Algorithm!
I'm struggling to produce the output in some alignment way as shown in below image:
Code I have written:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RemainingDaysCalculation
{
internal class DailyMoneySavingMultiplier
{
public static void Main()
{
DateTime currentDate = DateTime.Now;
int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
int finisheddaysCount = daysInYear - daysLeftInYear;
Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
Console.WriteLine(savings);
//Case 2:
Console.WriteLine("_________________________________");
Console.WriteLine(" Day No || Daily Saving || Total Saved");
for (int i=1; i <= daysInYear; i++)
{
Console.WriteLine("{0,-10} || {1,-10} || {2,5}", i, (i * 2), (i * (i + 1)));
//Console.Write(i);
//Console.Write(i * 2);
//Console.Write(i * (i + 1));
}
Console.WriteLine("_________________________________");
}
}
}
Output:
Update:
I have tried the 2 ways from the given workarounds in this reference, but i'm getting the exception like
Thanks to #Oliver and #Oleg for pointing me in the right direction and giving me the helping references.
Finally, I figured out this issue using the code below:
using ConsoleTables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RemainingDaysCalculation
{
internal class DailyMoneySavingMultiplier
{
public static void Main()
{
DateTime currentDate = DateTime.Now;
int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
int finisheddaysCount = daysInYear - daysLeftInYear;
Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
Console.WriteLine("Saving Value " +savings);
//Case 2:
Console.WriteLine("_________________________________");
var table = new ConsoleTable("Day No", "Daily Saving", "Total Saved");
for (int i = 1; i <= daysInYear; i++)
{
table.AddRow(i, (i * 2), (i * (i + 1)));
}
table.Write();
Console.ReadKey();
}
}
}
Result:

Does the ILNumerics asin() function give incorrect results for complex values?

I have run into this issue on Community Edition (version 2.14), but recently I have installed a trial version of ILNumerics ver. 6.0.127 and see that the issue still persists.
The code below describes the problem: I start with the Matlab asin(z) example for several input complex z values (provided here: https://www.mathworks.com/help/matlab/ref/asin.html) and show that the ILNumerics ILMath.asin() function produces incorrect results (complex conjugate ones), while the C# Numerics Asin() function provides correct answers.
As a result, in ILNumerics: sin(asin(z)) != z, while in Matlab and in C# Numerics: Sin(Asin(z)) == z.
To obtain the correct results, one should override the ILMath.asin() function and use ILMath.asin(ILMath.conj(z)), as shown in example below:
using System.Numerics;
using ILNumerics;
public class Example
{
public static void Main()
{ // start from Matlab example ( https://www.mathworks.com/help/matlab/ref/asin.html ):
// x = [ 0.5i, 1 + 3i, - 2.2 + i ];
// y = asin(x)
// 0 + 0.4812i 0.3076 + 1.8642i - 1.1091 + 1.5480i
Console.WriteLine(" C# Complex_values from Matlab example:");
Complex[] values = { new Complex(0, 0.5), new Complex(1, 3), new Complex(-2.2, 1) };
foreach (Complex value in values) Console.WriteLine($"value = {value}");
Console.WriteLine();
foreach (Complex value in values) Console.WriteLine($"Asin({value}) = {Complex.Asin(value)}");
Console.WriteLine("\n Now try to check that Sin(Asin(z)) == z: ");
foreach (Complex value in values) Console.WriteLine($"Sin(Asin({value})) = {Complex.Sin(Complex.Asin(value))}");
Console.WriteLine("\n So, C# gives correct results for Asin() function (+)\n");
Console.WriteLine("\n ILNumerics complex_values from Matlab example:\n");
ILNumerics.Array<complex> valuesIL = new complex[] { 0.5 * complex.i, 1 + 3 * complex.i, -2.2 + 1 * complex.i };
for (int i = 0; i < valuesIL.Length; i++)
{
Console.WriteLine($"valuesIL[{i}] = {valuesIL[i].ToString().Remove(0, 17)}");
}
Console.WriteLine("\n Compare with Matlab (and C#) results: [0+0.4812i 0.3076+1.8642i -1.1091+1.5480i]");
for (int i = 0; i < valuesIL.Length; i++)
{
Console.WriteLine($"ILMath.asin({valuesIL[i].ToString().Remove(0, 17)}) = " +
$"{ILMath.asin(valuesIL[i]).ToString().Remove(0, 17)}");
}
Console.WriteLine("\n Now try to check that ILMath.sin(ILMath.asin(z)) == z:");
for (int i = 0; i < valuesIL.Length; i++)
{
Console.WriteLine($"ILMath.sin(ILMath.asin({valuesIL[i].ToString().Remove(0,17)}) = " +
$"{ILMath.sin(ILMath.asin(valuesIL[i])).ToString().Remove(0, 17)}");
}
Console.WriteLine("\n So, ILNumerics gives wrong results for ILMath.asin() function (-) \n" +
" [one should use ILMath.asin(ILMath.conj(z)) or ILMath.conj(ILMath.asin(z)) \n" +
" to obtain correct results for complex z in ILNumerics]:\n");
for (int i = 0; i < valuesIL.Length; i++)
{
Console.WriteLine($"ILMath.sin(ILMath.asin(ILMath.conj({valuesIL[i].ToString().Remove(0, 17)})) = " +
$"{ILMath.sin(ILMath.asin(ILMath.conj(valuesIL[i]))).ToString().Remove(0, 17)}");
}
Console.ReadLine();
}
}
Is this a real bug or am I missing something?

sum of any four number always one in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
always one is the result of sum four numbers that less than one.
i changed the number to decimal and float
i used math,truncate
i wrote double before each number in calculation
still the result of sum is one
this my code and I added comment in the code in the place of my quistion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
public double[,] array = new double[4, 4];
public double b1vsb1c1=1.0;
public double b1vsb2c2;
public double b1vsb3c3;
public double b1vsb4c4;
// // .....here cotinue declear variable ,I deleteed to shoritng the code;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string isChecked2 = RadioButtonList1.SelectedItem.Value.ToString();
if (RadioButtonList1.SelectedItem.ToString() == "1")
{
b1vsb1c1 = 1.0;
b2vsb1c1 = 1.0;
}
else if (RadioButtonList1.SelectedItem.ToString() == "3")
{
b1vsb2c2 =3.0;
b2vsb1c1 =1.0/3.0;
}
// .....here cotinue test radio button value ,I deleteed to shoritng the code;
array[0,0]=b1vsb1c1;
array[0,1]=b1vsb2c2;
//....here cotinue assign value to array ,I deleteed to shoritng the code;
for (int i = 0; i < 4; i++)
{
sum = 0.0;
av = 0.0;
for (int j = 0; j < 4; j++)
{
sum=(double)sum + (double)array[i,j];
}
// here the sum always one why?!!
av = (double)sum/(double)4.0;
Response.Write(av + "| |");
// here is always result 0.25 why?
}
}
}
}
Try to use an Implicit typing : var to declare your varibales
I tried what you said; I have logic result : here is a sample exmaple:
public class Program
{
public static void Main(string[] args)
{
var p1 = 0.120336943441637;
var p5 = 0.01323706377858;
var p9 = 0.120336943441637;
var p13 = 0.120336943441637;
var sum = p1 + p5 + p9 + p13;
Console.WriteLine(sum);
}
}
Result = 0.374247894103491
Code Run

Arbitrary Precision for decimals in C# help?

Here is my current code for computing Pi using the chudnovsky method in c#:
using System;
using System.Diagnostics;
using System.IO;
using java.math;
namespace pi.chudnovsky
{
public class Program
{
static Double Factorial(Double fact)
{
//begin factorial function
if (fact <= 1)
return 1;
else
return fact * Factorial(fact - 1); //loops multiplication until the factorial is reached
}
static Double doSummation(Double maxPower)
{
//begin chudnovsky summation function
Double sum = 0;
for (int i = 0; i <= maxPower; i++) //starts at i=0
{
sum += ((Math.Pow(-1, i)) * Factorial(6 * i) * (13591409 + 5451401 * i)) / (Factorial(3 * i) * Factorial(i) * Factorial(i) * Factorial(i) * Math.Pow(640320, (3 * i + 1.5))); //chudnovsky algorithm
}
return sum;
}
static void Main(string[] args)
{
int num;
Console.WriteLine("Enter how many terms to compute Chudnovsky summation: ");
//begin stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//parse user input
num = Convert.ToInt32(Console.ReadLine());
//perform calculation
Double inv = 1 / (12 * doSummation(num));
//stop stopwatch
stopwatch.Stop();
//display info
Console.WriteLine(inv);
Console.WriteLine("3.14159265358979323846264338327950288419716939937510");
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed.TotalMilliseconds);
//write to pi.txt
TextWriter pi = new StreamWriter("pi.txt");
pi.WriteLine(inv);
pi.Close();
//write to stats.txt
TextWriter stats = new StreamWriter("stats.txt");
stats.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
stats.Close();
}
}
}
So, I've included the J# library, and included java.math. Now when I replace all the "double"s with "BigDecimal"s, I get these compile errors:
http://f.cl.ly/items/1r2X26470d0d0n260p0p/Image%202011-11-14%20at%206.16.19%20PM.png
I know that this isn't the problem with me using Int for the loops, as it worked perfectly with Doubles. My question is, how do you resolve these errors relating to int and BigDecimal, or can you recommend another arbitrary precision library?
I've tried using XMPIR, have gotten everything to compile, but I get:
http://f.cl.ly/items/1l3C371j2u3z3n2g3a0j/Image%202011-11-14%20at%206.20.24%20PM.png
So I can I use p/invoke to include xmpir so I can use whatever the bigdecimal class is?
Thank you for your time!
Can you not convert your int's to BigDecimal's before comparing them?
I assume you understand the problem here is that there is no operator overload for the greater, less than, etc. signs on the BigDecimal class that accepts an int.

Categories

Resources