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 8 years ago.
Improve this question
I am very new to c# and while working on a basic program I am receiving this error(Error Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement) and cannot figure out why it is appearing.
if (pizzaDiameter >= 12 && pizzaDiameter < 16)// begin nested if statement
{
Console.WriteLine("A" + pizzaDiameter + "will yield 8 slices.");
}
else if (pizzaDiameter >= 16 && pizzaDiameter < 24)//Second pizza diameter range
{
Console.WriteLine("A" + pizzaDiameter + "will yield 12 slices.");
}
else if (pizzaDiameter >= 24 && pizzaDiameter < 30)
{
Console.WriteLine("A" + pizzaDiameter + "will yield 16 slices.");
}
else (pizzaDiameter >= 24 && pizzaDiameter <= 30)
{
Console.WriteLine("A" + pizzaDiameter + "will yield 24 slices.");
}
else // pizza diameter was not a whole number
{
Console.WriteLine("Pizza diameter must be between 12-36 inclusive.");// Error appears here.
}
else //pizza diameter must be between 12-36
{
Console.WriteLine("Pizza diameter must be a whole number.");
}
There's something wrong with your control structure, i.e. you've got only one if(), but three times else.
Also, try to think about the problem and you'll notice that you can simplify the whole structure significantly (and also skip many checks):
if (pizzaDiameter < 12) // All diameters below 12 will use this branch.
Console.WriteLine("Your pizza seems to be too small.");
else if (pizzaDiameter < 16) // You don't have to ensure it's bigger than 12, since those smaller already picked the branch above.
Console.WriteLine("A diameter of " + pizzaDiameter + " will yield 8 slices");
else if (pizzaDiameter < 24) // Again you won't have to care for less than 16.
Console.WriteLine("A diameter of " + pizzaDiameter + " will yield 12 slices");
// ...
else
Console.WriteLine("Your pizza seems to be too big.");
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 3 years ago.
Improve this question
This is a part of a code which determines whether a number is prime or not. I know it could be solved in other ways, but I cant figure out why this loop wont stop if I put in a "5" as a number. The divisor reaches 5 after the third "divisor++;" (the divisor starts from 2) but the while loop wont quit.
Can anyone help me?
do
{
if (number % divisor == 0)
{
Console.WriteLine("Number is not a prime.");
}
if (divisor == number - 1)
{
Console.WriteLine("Number is prime.");
}
divisor++;
} while (number % (divisor -1) != 0 || divisor != number);
The condition should probably be ... != 0 && divisor != number.
Difference being && rather than ||.
while (number % (divisor -1) != 0 || divisor != number)
Above statement would never let it break.
For example if divisor is 5 then
5 % (5-1) would 1 and loop will continue
when divisor is 6 then
6 != 5 and it will continue
you should use
while (number % (divisor) != 0);
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 4 years ago.
Improve this question
As per title; I have tried multiple ways I can't seem to get my head around where I'm going wrong. I feel this is as close as I've got, but something still seems scrambled. Can someone help me out on finalising this? (This current code consistently outputs 0).
To clarify; I want the code to be able to read the number inputted by a user and figure out how many times it can be halved before reaching 1.
Console.WriteLine("Please enter a number to find how many time it can be divided without becoming less than 1");
Int32 DiviNum = Int32.Parse(Console.ReadLine());
Int32 count = 0;
for (int i = 0; i > 1; i = i / 2)
{
count++;
}
Console.WriteLine("Number of times " + DiviNum + " is divisible by 2 is " + count);
Thanks in advance
Solution is
Console.WriteLine("Please enter a number to find how many time it can be divided without becoming less than 1");
Int32 DiviNum = Int32.Parse(Console.ReadLine());
Int32 count = 0;
for (int i = DiviNum/2 ; i > 1; i = i / 2)
{
count++;
}
Console.WriteLine("Number of times " + DiviNum + " is divisible by 2 is " + count);
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
I get the error in the title when i do this:
else if (celsius<73 != 0)
{
Console.WriteLine("Tyvärr så är " + celsius + "° för låg temperatur, ställ in värmen mellan 73 till 77 grader."); //
Console.Write("Välj temperatur igen : ");
I want it to come out false if celsius is 0, sry for my bad english.
I want it to come out false if celsius is 0
If you want to do multiple comparisons against a variable, they need to be specified separately.
Thus:
else if (celsius<73 != 0)
should instead be:
else if (celsius<73 && celsius != 0)
You cannot compare a bool(which is what celsius<73 returns) with an integer 0.
I think you want that celsius is <73 and != 0:
else if (celsius < 73 && celsius != 0)
{}
If this is true ....
I want it to come out false if celsius is 0, sry for my bad english.
... then change it to:
if (celsius != 0)
{
}
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 5 years ago.
Improve this question
i solved an assignment question on my own but i seems not to be able to print an else statement successfully even though the code works.
The questions is below,
Create a program that prompts the user for the first number. Show on the screen which number was chosen and if the
Number is greater than 10 then show your predecessors until you reach number 10.
My solution is below,
Console.WriteLine("Enter interger please: ");
int num = int.Parse(Console.ReadLine());
Console.WriteLine();
for (int num3 = num; 10 <= num3; num3-- )
{
if (num > 10)
{
Console.WriteLine(num3);
}
else
{
Console.WriteLine("The integer entered is less than zero and cannot be used in code ");
}
Well it's normal that in doesn't enter the else statement. You're doing if (num > 10) and num is the value entered by the user which never changes during the process. So if num = 15 you're always doing is 15 > 10.
Then only moment the else statement is gonna print is if the number entered is 10.
And the moment num is smaller than 10, you'll never get in the for loop so the number will never be smaller than 0 inside the loop so that line won't make sense even if it's played
Console.WriteLine("The integer entered is less than zero and cannot be used in code ");
Cause like i said if this line is printed that means the value in num was 10 which is not less than zero.
You could change it for something like this
if(num < 10)
{
Console.WriteLine("It's smaller than 10");
}
for(int num3 = num; 10 <= num3; num3--)
{
Console.WriteLine(num3);
}
You're decrementing "num3" on the for loop, but you're validating if "num" is greater than 10 which, by entering the loop in the first place, will always be true.
Change to:
if (num3 > 10)
{
Console.WriteLine(num3);
}
Are you able to provide the question?
The code you have written seems a little meaningless.
Firstly, the for loop will only run if you enter an integer >= 10
Rewriting your code:
Console.WriteLine("Please enter a positive integer: ");
var args = Console.ReadLine();
if (int.TryParse(args, out int num))
{
if (num < 0)
Console.WriteLine('Must enter a positive integer!');
for (var i = num; i >= 10; i-- )
{
//this only runs if the integer entered is >= 10
if (num > 10)
{
Console.WriteLine(i);
}
}
}
else
{
Console.WriteLine("A non-integer was entered!");
}
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
if (intDaysOverdue <= 30)
{
decInterestRate = 0m;
}
else if (intDaysOverdue >= 30 && intDaysOverdue <= 59)
{
decInterestRate = .5m;
}
else if (intDaysOverdue >= 60 && intDaysOverdue <= 89)
{
decInterestRate = .10m;
}
else if (intDaysOverdue >= 90)
{
decInterestRate = .15m;
}
I need to write this info using a switch statement, but can't seem to figure out how
You cannot do what you want with a switch, but you can simplify it to:
if (intDaysOverdue <= 30)
decInterestRate = 0m;
else if (intDaysOverdue <= 59)
decInterestRate = .5m;
else if (intDaysOverdue <= 89)
decInterestRate = .10m;
else
decInterestRate = .15m;
Your >= 30 and >= 60 conditions are not needed, as they are already true because of the prior if statements.
Switch/Case is more suited for specific values, not ranges. This is what the if statement is for.
If your interest rate increases .5 for every 30 days, similar to as #EZI suggested in the comments, you could further simplify the code to:
decInterestRate = ((int)Math.Min(intDaysOverdue, 90) /30) * .5;
you can't use switch here (you can manipulate it with a default case but why do that?)
switch and case are meant to test individual values, your condition needs an if statement.