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 8 years ago.
Improve this question
I have been asked to create a mark to grade converter in Windows Form Application.
My code below is from the click of a button. Once a user has input their mark into 'Markbox' and the button is clicked the if statement will run and it will find the correct range of the mark. Then it will display a grade, relevant to the mark in 'Gradelb'.
Below is the code under the button click command. I wanted to condense it down to reduce code line space as well as making it more manageable.
void SubmitBtn_Click(object sender, EventArgs e)
{
int mark = int.Parse(Markbox.Text);
if (mark >= 45 && mark <= 50) Gradelb.Text = "A*";
else if (mark >= 40 && mark < 45) Gradelb.Text = "A";
else if (mark >= 35 && mark < 40) Gradelb.Text = "B";
else if (mark >= 30 && mark < 35) Gradelb.Text = "C";
else if (mark >= 25 && mark < 30) Gradelb.Text = "D";
else if (mark >= 20 && mark < 25) Gradelb.Text = "E";
else if (mark >= 0 && mark < 20) Gradelb.Text = "U";
else MessageBox.Show("Please enter a mark between 0-50");
Apologies for any errors or incorrect terminology, I am a new Apprentice employee.
string Gr = new string[] { "A*", "A", "B", "C", "D", "E", "U" };
if(mark >=0 && mark <= 50)
Gradelb.Text = Gr[10 - Math.Max(4, (int)Math.Ceiling(mark/5f))];
else
MessageBox.Show("Please enter a mark between 0-100");
A word of caution: After living a decade of "one-liner"'s life, I can advise you one thing: There is no guarantee that this code will be any more efficient than yours.
Explanation
Since the grades and the marks range they are linked with follow a fixed pattern, I created an array of grades so that I could refer to each grade by array index. Now all I need is a expression that could convert a given number to index.
46-50 => 0
40-45 => 1
and so on...
This can be done by dividing the number by 5 (since that is the group size in your example). So for example dividing 41 by 5 will give you 8.2. Doing a Ceiling() (which returns nearest greater or equal integer) on it will give 9. Subtracting this value from 10 will give you the index of second group (which is grade A).
Math.Max() (which returns larger of the two parameters) is simply there to ensure that values which are out of array bounds do not cause an exception. This will be the case when marks are 15 or less.
Related
This question already has answers here:
Or in IF statement not working properly
(4 answers)
Closed 9 months ago.
I'm a beginner in c#, and I am currently practicing conditional statements and others. I wanted to create a simple movie rating code, where the user would enter a value and show the movie's rating.
I'm having a little trouble because I get the wrong rating when I type in a specific number.
A NY help with this code would be appreciated. Thank you.
static void Main(string[] args)
{
int rating;
string movie;
Console.Write("Enter the name of the movie: ");
movie = Console.ReadLine();
Console.Write("What is your rating: ");
rating = Convert.ToInt32(Console.ReadLine());
if (rating == 10 || rating >= 8)
Console.WriteLine("Great movie");
else if(rating <= 7 || rating >= 5)
Console.WriteLine("Good movie");
else if (rating <= 4 || rating >= 1)
Console.WriteLine("Poor movie");
else
Console.WriteLine("Very Very Poor");
Console.WriteLine("The movie {0} is of {1} rating", movie, rating);
Console.ReadKey();
}
Looking at the conditional statements, let's think about what would happen if I were to enter, say, 4 for the rating:
if (rating == 10 || rating >= 8)
4 is not 10, and it's not >= 8. Moving on...
else if(rating <= 7 || rating >= 5)
4 is not >= 5, but it IS <= 7. Since this is an || condition, the result is true if either side is true, and that is the case here. I don't think that's what you intended.
You probably want && for these, instead of ||. Additionally, since we go in order from large to small, using else if blocks, you only need to check the lower bound of each rating.
As this is beginning work, I'll leave incorporating these suggestions into the original code for the OP.
The first conditions in all ifs are useless: rating == 10 || rating >= 8 - when rating >= 8 we have no need to check rating == 10; rating <= 7 || rating >= 5 meets every rating - all integer numbers either less than 7 or greater than 5 etc.
Film is
Great if its rating is at least 8.
Good if its rating is at least 5 (and it's not Great).
Poor if its rating is at least 1 (and it's not Good).
Very Very Poor in other cases.
if (rating >= 8)
Console.WriteLine("Great movie");
else if (rating >= 5)
Console.WriteLine("Good movie");
else if (rating >= 1)
Console.WriteLine("Poor movie");
else
Console.WriteLine("Very Very Poor");
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 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 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 6 years ago.
Improve this question
The basic idea we follow is
if (i > 0 && i < 100) {
} else if (i > 100 && 1 < 150) {
}
This is the basic idea for range check..
Is there any good way to check these type of conditions.
Thanks in advance for the help!
int endX=200;
Click on below link you will get what is the difference #Steve
var switchCond = new Dictionary<Func<int, bool>, Action>
{
{ x => x <= 290 , () => endX=244 },
{ x => x <= 530, () => endX=488 },
{ x => x <= 790 , () => endX=732 },
{ x => x <=1000 || x > 976 , () => endX=976 }
};
switchCond.First(sw => sw.Key(endX)).Value();
[Switch case: can I use a range instead of a one number
use ternary operator
var range =
i < 100? "Range1":
i < 150? "Range2":
i < 200? "Range3":
i < 250? "Range4":
i < 300? "Range5":
"Range6";
This is example only to show technique. obviously, you must adapt it to your code objectives and appropriate range definitions. (by the way, in your example, using if- else if, your code misses the value 100. you need to decide which range 100 should be included in, and change one of the inequality operators to either <= or >=.
you could have switch construct "handle" ranges by use of a List of your bounds:
List<int> bounds = new List<int>() {int.MinValue, 0, 100, 150, 200, 300, int.MaxValue };
switch (bounds.IndexOf(bounds.Last(x => x < j)))
{
case 0: // <=0
break;
case 1: // > 0 and <=100
break;
case 2: // > 100 and <= 150
break;
case 3: // > 150 and <=200
break;
case 4: // > 200 and <=300
break;
case 5: // >300
break;
}
where you may have to add some additional checks to
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.