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 1 year ago.
Improve this question
for(int i = 1; i != amount+1; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Append(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Append(Convert.ToDecimal(Console.ReadLine()));
}
(This section is what is affected)
It gets the inputs from the user (I.E. it gets the names and grades, though it doesn't add to the array. I am new to C#, coming from python. Do you guys have any idea how I could fix this issue? TIA
Are you looking for something like this?
using System.Collections.Generic;
int amount = 5;
List<string> people = new List<string>();
List<decimal> grades = new List<decimal>();
for (int i = 0; i < amount; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Add(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Add(Convert.ToDecimal(Console.ReadLine()));
}
Related
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 6 months ago.
Improve this question
i have to make a simple ticket system where a person inputs name, number of adults and children and gets back the amount of money they have to pay (children get a 75% discount) and i cant seem to figure out how to do the calculation in code, and since there is only one teacher for about 500 students getting an answer from them isnt much of an option. Thanks in advance for any kind of help!
Are you looking for sth like this?
double cost = 10;
Console.WriteLine("Number of Adults");
var adults = Console.ReadLine();
var numberOfAdults = 0;
if (adults != null) numberOfAdults = int.Parse(adults);
Console.WriteLine("Number of Children");
var children = Console.ReadLine();
var numberOfChildren = 0;
if (children != null) numberOfChildren = int.Parse(children);
double price = numberOfAdults * cost + numberOfChildren * cost * 0.25;
Console.WriteLine($"Price: ${price}.");
Console.ReadLine();
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 1 year ago.
Improve this question
I am new to c#, but have been coding in java before. I downloaded visual studio code and the c# extension, and it seems to be working when I do Console.WriteLine("Example"); yet when I have a for loop it seems to not run it. I have a really simple sum calculator, but it doesn't work:
static void Main(string[] args)
{
int sum = 0;
Console.Write("Started Program");
Console.WriteLine("...");
for (int i = 0; i < 10; i++)
{
Console.Write("Choose numbers you want to add: ");
int num = Console.Read();
sum = sum + num;
}
Console.WriteLine(sum);
}
Its probably passing the .read input as ASCII characters instead of the actual int ... try changing;
int num = Console.Read();
to
int num = Int32.Parse(Console.ReadLine());
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 5 years ago.
Improve this question
Quick question:
I am writing in unity c#, and have an array containing several instances of the same object. I want to change a non-static bool in every member of the array, and set it to true. How would I do this?
I have played around with for-loops, and searched both here and at the unity forums, but cannot find an answer. I don't believe my messy, faulty code would be of any help. Anybody care to enlighten me? :)
EDIT: Thanks for the tips, will try them now. The code was asked for, excuse the mess:
Component[] toMerge;
for (int t = 0; t < mergeTargets.Length; i++) {
toMerge[t] = mergeTargets[t].gameObject.GetComponent<Enemy>();
toMerge.readyToMerge = true;
}
for (int t = 0; t < toMerge.Length; t++) {
toMerge[t].readyToMerge = true;
}
You have to allocate mergeTargets.Length items in toMerge array and address not the array but its item:
//DONE: We need mergeTargets.Length items in toMerge
Component[] toMerge = new Component[mergeTargets.Length];
for (int t = 0; t < mergeTargets.Length; i++) {
toMerge[t] = mergeTargets[t].gameObject.GetComponent<Enemy>();
//DONE: [t] - we want to change item, not the array
toMerge[t].readyToMerge = true;
}
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 5 years ago.
Improve this question
Just trying to make a simple array that takes grades (double variables) and assigns them to the array elements. Also the user determines how many grades they want to assign. It seems simple, yet every time I execute the code, it prints out way too many lines and doesn't stop based on the user input. Not sure what I am missing here.
static void Main(string[] args)
{
int num = 0;
Write("How many grades are you wanting to process...");
num = Read();
Double[] grades = new Double[num];
for (int i = 0; i < grades.Length; i++)
{
WriteLine("Enter grade:");
grades[i] = ToDouble(Read());
WriteLine();
}
It has been a while and feel silly, just came back from python.
Just few modification
static void Main(string[] args)
{
int num = 0;
Write("How many grades are you wanting to process...");
num = Convert.ToInt32(Console.ReadLine());
Double[] grades = new Double[num];
for (int i = 0; i < num; i++)
{
Console.Write("Enter grade:");
grades[i] = Convert.ToDouble(Console.ReadLine());
}
}
ReadKey (returns a character): reads only one single character from
the standard input stream. Usually used when you're giving options to
the user in the console to select from, such as select A, B or C.
Another prominent example, Press Y or n to continue.
ReadLine (returns a string): reads only single line from the standard input stream. As an example, it can be used to ask the user
enter their name or age.
Read (returns an int): reads only one single character from the standard input stream. Similar to ReadKey except that it returns an
integer.
This was clearly described with examples in the MSDN documentation
(links are included above).
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
Hey guys I am a beginner at coding and I'm doing a linear search in c# and can't figure out how to make it show in what array the number was found when doing the search. It just makes it say that it's on the last array. Here's my code:
Random Generator = new Random();
int[] array = new int[100];
int count = 0;
for (int i = 0; i < array.Length; i++)
{
array[i] = Generator.Next(1, 100);
count++;
}
Console.WriteLine("Write a number between 1-100 and see if it's in a array and in what array.");
int guess = Convert.ToInt32(Console.ReadLine());
bool exists = array.Contains(guess);
if (exists)
{
Console.WriteLine("Your number {0} is in the {1}th Array.", guess, count);
}
else
{
Console.WriteLine("Your number {0} does not match any number in any of the Arrays.", guess);
}
Console.ReadKey();
There's something wrong with my count int, but I don't know what to do. Thanks in advance.
I think you are looking for Array.IndexOf
Int index = Array.IndexOf(amountOfArrays,guess);
If (index == -1)
{
// not found
}
else
{
// found
}
Count++ will be the last. How would you expect it to be anything else?