Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
for(i = 2000; i >= 0; i -= 40)
{
Console.WriteLine("Your score is {0}", i);
}
i'm trying to count backwards from 2000 by 40 every time someone loops another for loop:
int count = 0;
for (int i = 0; i < random || i > random; i++)
{
count++;
Console.WriteLine("\r\nGuess [{0}]: Please enter a number:", i + 1);
string guessString = Console.ReadLine();
I'm trying to make a scoreboard so every time you don't guess a number correctly, it deducts 40 from 2000.
If I understand correctly, the variables you need are: the amount of guesses, the score, the value to find, and then in each iteration you need the user's input. This can be solved with a single loop.
Here's some pseudocode to show how you can approach this:
remainingGuesses = 50
score = 2000
while remainingGuesses > 0
userInput = GetInput()
if userInput == random
break
remainingGuesses = remainingGuesses - 1;
score = score - 40
return score
You could otherwise go from attempt 0 to 49 and say score = 2000 - 40 * attempts
If I understand correctly from the comments above, you want to deduct 40 points every time someone guesses a number incorrectly.
You can have a variable that holds the score and initialize it with 2000 points and create a function that deducts 40 points each time someone guesses wrong.
int startingPoints = 2000;
public void DeductPoints() {
startingPoints -= 40;
}
And then, you can do some if/else statements to do checks on the score and the corresponding output you want to show to the user.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So perhaps, you've generated some fancy text, and you're content that you can now copy and paste your fancy text in the comments section of funny cat videos, but perhaps you're wondering how it's even possible to change the font of your text? Is it some sort of hack? Are you copying and pasting an actual font?
days++;
earth++;
mars++;
earth %= 365;
mars %= 687;
}
Console.Write(days);
Console.Write("\n");
Console.ReadLine();
n++;
}
}
}
It appears you're only missing a way to process the sample input. It sounds like it's coming in "lines", but that's not really defined, so in this example I'm just using an array. I'm ignoring the fact that the first item specifies the number of testcases, since in this implementation the number is calculated.
Basically I'm just walking through the array, grabbing two items at a time (skipping the first one), and using those values for earth and mars. The rest is mostly your code (except some changes to outputting to the console):
int[] sampleInput = {5, 0, 0, 364, 686, 360, 682, 0, 1, 1, 0};
int n = 1;
for (var i = 1; i < sampleInput.Length - 1; i += 2)
{
int earth = sampleInput[i];
int mars = sampleInput[i + 1];
int days = 0;
while (earth != 0 || mars != 0)
{
days++;
earth++;
mars++;
earth %= 365;
mars %= 687;
}
Console.WriteLine($"Case {n}: {days}");
n++;
}
Console.ReadLine();
Output
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 years ago.
Improve this question
So I wrote a short code in c#, and it's function is like this:
First you choose how many times you want to put in a number, then you pick a number to sum
static void number()
{
Console.WriteLine("how many numbers, max is 999");
int n = int.Parse(Console.ReadLine());
if (n > 999)
{
return;
}
Console.WriteLine("enter number here:");
int d = int.Parse(Console.ReadLine());
if (d < -99999 || d > 99999)
{
return;
}
Console.WriteLine(n + " " + d + "which number has to be counted up");
for (int i = 0; i < n; i++)
{
int e = int.Parse(Console.ReadLine());
int c;
c = e + d;
Console.WriteLine(e + " " + c);
Console.WriteLine("press enter to input a new number");
Console.ReadKey();
i++;
}
}
If I put positive numbers, it works correctly. But if I put in negative numbers, It asks "enter number here" and after I put in a number, it shows the which number to count up writeline very quickly and then the application stops for no reason.
Any thoughts why this happens?
If you put a negative number into n, then i will never be less than n in your for loop (which you've set to run while i < n). This means the the for loop will never run and the as this is the last bit of code in you application, the program will end.
Here's what I get:
how many numbers, max is 999
-2
enter number here:
3
-2 3which number has to be counted up
When I enter the -2 it gets stored in the variable n. Later, in the for loop, the variable i starts out at zero, which means the i < n, so the loop quits without executing anything in the loop body.
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
I'm trying to do a Prime Number finder but cant see why it's not working.
When i run the debug test nothing show in the console. Could someone check it and tell me what i do wrong?
List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
int maxPrime = 11; //The maximum found Primes
int primeCount = primes.Count; //Current Number of Primes
int num = 4; //Current Number
int x = 0; //
int curPrime = primes[x];
while (primeCount < maxPrime)
{
if (x != primeCount)
{
if (num % primes[x] == 0)
{
num++;
x = 0;
}
else
{
x++;
}
}
else
{
primes.Add(num);
primeCount=primes.Count;
x = 0;
}
}
primes.ForEach(i => Console.Write("{0}\t", i));
You have an infinite loop.
Since you never modify primeCount or maxPrime, this will always be true:
while (primeCount < maxPrime)
In order to end that loop, you need to modify one of those two values in such a way that the condition will evaluate to false.
(Note: There appear to also be other bugs/problems in the code aside from this. For example, num = num++; doesn't do what you probably think it does.)
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?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Console.WriteLine ("Enter the number of book to delete (1 to {0})", amount);
int posToDelete = Convert.ToInt32 (Console.ReadLine ())-1;
for (int i = posToDelete; i < amount - 1; i++)
b [i] = b [i + 1];
amount--;
// hi! i am new to programming... could anyone please explain this to me in detail
// * what is the use of -1 after the readline
// * explain me the loop plz
//link of the question
//http://practiceexercisescsharp.blogspot.com.es/2013/05/411-books-database.html
fisrt question:
An array is start from 0.
for example: There is an array b includes 4 elements.
b[4] = {1,2,3,4};
so b[0] is 1 and b[3] is 4.
there is no b[4].
if you want to delete the second item(which element is 2) ,you should delete b[1].
so the position is 1.
This is why we use -1 after the readline.
second question:
for (int i = posToDelete; i < amount - 1; i++)
{
b [i] = b [i + 1];
amount--;
}
int = posToDelete is the position of the item we want to delete.
we use the next item to replace it.
this is b[i] = b[i+1];
because we delete one item,the amount need -1;
we also use b[4] for example:
if we delete 2.
initial:[1,2,3,4] amount=4;
loop begin: [1,2,3,4] posDelete is 1,amount is 3;
b[1]=b[2];we use 3 to replace 2;
b[2]=b[3];we use 4 to replace 3;
i is increasing until i is 3;the loop end.
the new array is [1,3,4].
Chances are the indexer of your book array is layout like the following:
book[0] = "Book1" //Where user inputs 1 and then subtract 1 from it to access index 0
Consider the following examples:
book[0] = "Book1" //Book input is 1 less 1 = index of 0
book[1] = "Book2" //Book input is 2 less 1 = index of 1
book[2] = "Book3" //Book input is 3 less 1 = index of 2
Of course the logic of deleting the book is in question, since we are not sure how the book is really arranged in Arrays or Collections. But in your case, you are specifying the position so it can be said that position 1 is
equal to index 0.