Quote:
Originally Posted by co_george
ty bud, e*pvp should thank you 4ever for your great releases and your willing to help this forum. ty again! im learning your guide right now and you know what? you explained this shit very well so everyone can learn it :) thanks again. i would rape the thanks button if i could thank you more than one time :D
Later edit: I think is a problem at lesson five - bool values
Code:
if (condition)
{
// code to execute if condition is true
}
else if (condition2)
{
// code to execute if condition is false
// and condition2 is true
}
else
{
// code to execute if condition is false
// and condition2 is false
}
i think you need to add the "if" for the 3rd condition also (where the code execute if the both conditions are false). ifi let it like it is right now without the "if" at the 3rd statement, error pop up but if i add "if" everything works fine. here's the code
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Tell me how much money you have in your pockets");
double amount = double.Parse(Console.ReadLine());
if (amount > 2000.00)
{
Console.WriteLine("You are rich");
Console.ReadLine();
}
else if (amount == 2000.00)
{
Console.WriteLine("You are fine");
Console.ReadLine();
}
else if (amount < 2000.00)
{
Console.WriteLine("You are poor");
Console.ReadLine();
}
}
}
}
tell me if i am wrong!!!
|
What you did was absolutely correct. Let's take a look at what you said. You wanted to know
why this was wrong:
Code:
Console.WriteLine("Tell me how much money you have in your pockets");
double amount = double.Parse(Console.ReadLine());
if (amount > 2000.00)
{
Console.WriteLine("You are rich");
Console.ReadLine();
}
else if (amount == 2000.00)
{
Console.WriteLine("You are fine");
Console.ReadLine();
}
else (amount < 2000.00)
{
Console.WriteLine("You are poor");
Console.ReadLine();
}
The reason it is wrong is because, is because the "else" case should only be used when all above conditions are false. What you were trying to do, was specify another condition to check:
(amount < 2000.00), so for this we would use
else if as you had done :).
Something to think about though is
Code:
Console.WriteLine("Tell me how much money you have in your pockets");
double amount = double.Parse(Console.ReadLine());
if (amount > 2000.00)
{
Console.WriteLine("You are rich");
Console.ReadLine();
}
else if (amount == 2000.00)
{
Console.WriteLine("You are fine");
Console.ReadLine();
}
else
{
Console.WriteLine("You are poor");
Console.ReadLine();
}
Would work perfectly fine! Because, above your final
else statement, we check if
amount > 2000.00 and
amount == 2000.00, and if both of these are false -- then
amount < 2000.00 must be true!
Quote:
Originally Posted by Korvacs
I was going to sticky this once Unit 2/3 had been completed so that there was alot of content available.
|
Thanks a lot Jack for the support. Really appreciated.
Quote:
Originally Posted by EmmeTheCoder
You can use ternary operations for calls like that. Here's an example:
Code:
static void Main(string[] args)
{
Console.WriteLine("Please insert how much money you have in your pockets");
Console.Write("Amount: ");
double amount = double.Parse(Console.ReadLine());
Console.WriteLine(amount > 2000 ? "You are rich!" : "You are poor!");
}
Ternary operation - Wikipedia, the free encyclopedia
|
While using the ternary operator is simpler, it is not
easier for early programmers to understand. The early-on unit-one series' goal isn't to create good programming habits, but to familiarize new programmers with the language.
Quote:
Originally Posted by co_george
Thats really awesome Emme but i just started with C# and i dont know very much about, just following Hybrid's guides and trying to understand them (now i'm stuck at Lesson 6 - While Loops). I still have a question. My shitty code it's actually working fine and there's not problem writing the code like mine or like you isnt it? I know your code it's smaller just 4 codelines but doesnt really matter if i do in my way. What i mean it's correct what i done or not ?:)
|
You're absolutely right. At this point in time, it doesn't matter how you do it
as long as you understand what you are doing!.
Directed more at Emme, not George:
However, it should be noted, your code and Emme's code carries a significant difference. Your code checks for 3 things
equality, greater than, & lesser than, using the ternary operator as Emme has done is checking for
greater than and if not,
lesser than or equal to. So
NO. One ternary operation in this case is not applicable and
wrong. You can tell this is wrong because Emme's code will show two outputs on the console (poor, or rich), while yours shows three (poor, rich and fine).
Edit:
Added (Unit Two)
Lesson Three - Enumerations.
Also, and edit to my last remarks in this post.