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 4 years ago.
Improve this question
using System;
namespace Zadacha
{
class Zadacha
{
static int Read(int x, int y)
{
Random rnd = new Random();
Console.WriteLine("Vuvedete minimalna velichina");
string MinValue = Console.ReadLine();
Console.WriteLine("Vuvedete maximalna velichina");
string MaxValue = Console.ReadLine();
int.TryParse(MinValue, out x);
int.TryParse(MaxValue, out y);
int value = rnd.Next(x, y);
Console.WriteLine("Proizvodnoto chislo e: " + value);
Console.ReadKey(true);
return value;
}
static void Main()
{
}
}
}
This is my code, program just starts and shutdowns after a second with no text in the console app. Everything seems fine and i do not know what is wrong. It is a uni task.
You don't do anything in your Main method - you need to call your Read(x,y) method at least, and/or Console.ReadLine(); in order for something to happen.
For example
static void Main()
{
Read(1,2);
}
Is this what you are trying to achieve? Console apps start from Main() so you'll have to put something in there for the program to work. I guess you don't actually want to feed Read() two int you want to get the users input?
using System;
namespace Zadacha
{
class Zadacha
{
static int Read()
{
int x = 0;
int y = 0;
Random rnd = new Random();
Console.WriteLine("Vuvedete minimalna velichina");
string MinValue = Console.ReadLine();
Console.WriteLine("Vuvedete maximalna velichina");
string MaxValue = Console.ReadLine();
int.TryParse(MinValue, out x);
int.TryParse(MaxValue, out y);
int value = rnd.Next(x, y);
Console.WriteLine("Proizvodnoto chislo e: " + value);
Console.ReadKey(true);
return value;
}
static void Main()
{
Read();
}
}
}
Related
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 2 years ago.
Improve this question
I need to access an element from an array and I am getting this error right here Console.WriteLine(testChoice[0]);
Why is this happening? Here is my code:
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] m1 = { 100 };
int a = m1.ElementAtOrDefault(0);
Console.WriteLine("Type m1 underneath");
string test;
test = Console.ReadLine();
int testChoice;
testChoice = Convert.ToInt32(test);
for (int i = 0; i < 1; i++)
{
Console.WriteLine(testChoice[0]);
}
}
}
The variable which index you are trying to access, testChoice, is not an array. It is an int variable. To print it just get rid of the for loop and the index:
static void Main()
{
int[] m1 = { 100 };
int a = m1.ElementAtOrDefault(0);
Console.WriteLine("Type m1 underneath");
string test;
test = Console.ReadLine();
int testChoice;
testChoice = Convert.ToInt32(test);
Console.WriteLine(testChoice);
}
To access an array element by user input:
//class variable
private static int _testChoice;
static void Main()
{
int[] testChoices = { 100, 200, 300 };
//int a = m1.ElementAtOrDefault(0);
//Console.WriteLine("Type m1 underneath");
string indexInput;
Console.WriteLine("Please specify the index:");
indexInput = Console.ReadLine();
int index;
index = Convert.ToInt32(indexInput );
_testChoice = testChoices[index];
Console.WriteLine(_testChoice);
}
The code
class Program
{
static int Add(int x, int y)
{
x = 4;
y = 3;
int f = x + y;
return f;
}
static void Main(string[] args)
{
int x = 4;
int y = 3;
Console.WriteLine("Answer: ");
Add(x, y);
}
}
Doing a beginner course in C# and I have been stuck at this question for two days now.
I know its probably really simple, but I have tried so many different things that I think I have made it harder for me than it really.
I fixed to call strings in methods, but numbers seems hard.
The task is about to take two numbers in and that return the answer.
Tried searching around all the different errors I got with all the different tries, but didn't find the help, or the answers I understand.
You almost did all of it, just with 2 issues.
You should relay on the numbers you pass from Main to Add and not reassign the values inside Add otherwise passing them is useless and unusable for other numbers.
Add returns a value but you never save it + print it.
Example for #1
static int Add(int x, int y)
{
int f = x + y;
return f;
}
Example of #2
var result = Add(x, y);
Console.WriteLine(result);
Corrected Example:
class Program
{
static int Add(int x, int y)
{
// You don't need to redefine the variables x and y,
// because you get them when you call the method
// You can shorten the last part
// and just return the Addition
return x + y;
}
static void Main(string[] args)
{
int x = 4;
int y = 3;
// Prints the Word Answer
// as well as the Addition result into the Console now
Console.WriteLine("Answer: " + Add(x, y));
}
}
Your Errors:
You never printed the Result into the Console!
You shouldn't redefine the variables in the Function, because if you do that you don't need to use a function in the first place
You can shorten the return statement (you don't have to)
You can add Add(x,y) into the Console.WriteLine because it returns a Integer, therefore it is basically like writting Console.WriteLine("Answer: " + 7);
Here is an working version with explaination:
class Program
{
static int Add(int x, int y)
{
//x = 4; these are passed in as parameter, no need to set it
//y = 3;
int f = x + y;
return f;
}
static void Main(string[] args)
{
int someX = 4; //these are only known inside the scope of "Main"
int someY = 3;
int result = Add(someX, someY); //these are passed inside the function,
//the value is copied
Console.WriteLine("Answer: " + result.ToString());
}
}
You can do it even easier and simple In addition , this answer is more dynamic as you can choose the two numbers every time you run the program:
class Program
{
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
Console.WriteLine("Answer: " + Add(Convert.ToInt32(Console.ReadLine()),
Convert.ToInt32(Console.ReadLine())).ToString());
Console.ReadLine(); //In order to be able to see the result in the screen
}
}
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
I know how to calculate a factorial using a loop. Below is the code for loop, but I am getting an error while doing it by recursion. Below are both the code samples. How can I fix this?
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
}
}
}
Factorial using recursion:
Is there something as where I am going wrong? When am I calculating it using recursion?
Factorial using loop:
public double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number * factorial_recursion(number - 1);
}
public double factorial_WhileLoop(int number)
{
double result = 1;
while (number != 1)
{
result = result * number;
}
return result;
}
Your call name is not equal to your method name:
factorial_Recursion is the method name.
factorial_recursion is the call.
This worked for me:
namespace Testing
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(factorial_Recursion(5));
Console.WriteLine("press any Key");
Console.ReadLine();
}
public static double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number*factorial_Recursion(number - 1);
}
Hi I need a little help creating a simple program that will Generate multiplication table (using for-loop) by using Class Library from VS C#.
I have here below the incomplete code for the for-loop but I got lost coz it's a bit different than application form and console application. If you're using class library you cannot use debug, you can run or check the codes by using Test Explorer/ Test.
(Edited Update 1) For this 2 things are needed.
Class Library (Main program)
Same solution but another class name, now it comes with NUnit that is referenced to the Main Program.
(Edited Update 2)
I'll be back to check for some info
Update 3. Here's the new code
namespace FunctionTest
{
[TestFixture]
public class Class1
{
[Test]
public void Multiplication()
{
int i;
int n = 0;
n = Convert.ToInt32(Console.ReadLine());
for (i = 1; i < 13; i++)
{
Console.WriteLine(i + "x" + n + " = " + i * n);
}
}
}
Here's the idea or what it should be look like. (Below is the program)
using System;
namespace ExerciseFunction
{
public class Equations
{
public int addition(int x, int y)
{
int z = x + y;
return z;
}
public int subtraction(int x, int y)
{
int z = x - y;
return z;
}
public int multiplication(int x, int y)
{
int z = x * y;
return z;
}
public int division(int x, int y)
{
int z = x / y;
return z;
}
static void Main(string[] args)
{
}
}
}
Now this one is the NUnit to check if the input or answer is correct or not of the Program.
using NUnit.Framework;
using ExerciseFunction;
namespace ExerciseNunit
{
[TestFixture]
public class Operations
{
[Test]
public static void addition()
{
Equations result = new Equations ();
float r = result.addition(4, 5);
Assert.AreEqual(9, r);
Assert.AreNotEqual(13, r);
}
[Test]
public static void subraction()
{
Equations result = new Equations();
int t = result.subtraction(5, 3);
Assert.AreEqual(2, t);
Assert.AreNotEqual(5, t);
}
[Test]
public static void multiplication()
{
Equations result = new Equations();
int y = result.multiplication(6, 3);
Assert.AreEqual(18, y);
Assert.AreNotEqual(15, y);
}
[Test]
public static void division()
{
Equations result = new Equations();
int u = result.division(4, 2);
Assert.AreEqual(2, u);
}
}
}
Thanks and looking forwards hearing your response. Your help is appreciated!
If you want to write a program, you probably want to execute it too. So you need and executable, not a library.
So first create a new project "Console Application" or "Windows Forms Application", or maybe a "WPF application" but not a Class Library. Also writing some unit test is useful, but I don't thing that in this case.
Secondly: do not declare loop variable i before the cycle. Do it in the cycle like this
for (int i = 0; i < 15; ++i) //or i++ there is a difference but not relevant right now.
Then... You probably want to get some input from a user to get your n.
In console application you can do that like this
int n;
string input = Console.ReadLine();
if (int.TryParse(input, out n))
{
//do your math here.
}
else
{
Console.WriteLine("That was not a number.");
}
Your for-cycle would work but the formatting of the output will be poor and most importantly, you are not printing or giving the output anywhere. So let's fix it like this (put that to place "//do your math here."):
for (int i = 1; i < 15; ++i)
{
Console.WriteLine(string.Format("{0} x {1} = {2}", i, n, i * n));
}
In the end you might want the application not to exit immediately. If you add Console.ReadLine(); in the end. It will wait for pressing any key before it exits.
If you so much want to have the algebra part in another project (which doesn't really make sense, but OK), you can create another project (Class Library) with this class in it (or put just the class in the existing project):
public static class Algebra
{
public static int Multiply(int a, int b)
{
return a * b;
}
//.... other methods
}
and then call it in the for loop like this:
int product = Algebra.Multiply(i, n);
Console.WriteLine(string.Format("{0} x {1} = {2}", i, n, product));
And of course you can then unit-test the Multiply method as much as you want.
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 8 years ago.
Improve this question
How can I write a code in C# to find the sum of letters
If A=0;B=1,C=A+B,D=B+C,E=C+D.....
Example CD=1+2=3,
I have tried this way where input is string and output is sum of letter
using System;
public class Test
{
public static (int output1)
public static void Main(string input1)
{
// your code goes here
}
}
An answer without using dictionary list
class Program
{
static void Main(string[] args)
{
string test = "abcdef";
int sum = 0;
foreach (char c in test)
{
int letterNumber = char.ToUpper(c) - 64;
sum += rtnDegint(letterNumber);
}
Console.WriteLine(sum);
}
int rtnDegint(int n)
{
int first = 0, second = 1, next = 0, c;
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
}
return next;
}
}