My sort of first program that doesn't work
-
I have been studying C# for about 4 or 5 months and then thought "Well I'll just write something from scratch. Well I felt like I've been reading a book on "How To Swim" and then when I jumped in the water I just gasped for air! Well anyway I hoping that someone could look at this and tell me how to use the return for the "public bool TrueOrFalse(). Thanks and sorry this is so elementary. I think I would like to write a book some day named a Beginner Beginner for the Basics of C#. Well any way here it is. using System; using System.Collections.Generic; using System.Text; namespace StartingOver { class RunIt { public bool TrueOrFalse() { int four = 4; int five = 5; bool answer; Console.WriteLine("4 * 5 should equal: {0}", four * five); four*five return answer; } public void Getyear() { Console.WriteLine("What year will you be beginning?"); string year = Console.ReadLine(); Console.Clear(); Console.WriteLine("Starting all over again in {0}\n", year); } static void Main(string[] args) { RunIt ri = new RunIt(); ri.Getyear(); ri.TrueOrFalse(); } } }
-
I have been studying C# for about 4 or 5 months and then thought "Well I'll just write something from scratch. Well I felt like I've been reading a book on "How To Swim" and then when I jumped in the water I just gasped for air! Well anyway I hoping that someone could look at this and tell me how to use the return for the "public bool TrueOrFalse(). Thanks and sorry this is so elementary. I think I would like to write a book some day named a Beginner Beginner for the Basics of C#. Well any way here it is. using System; using System.Collections.Generic; using System.Text; namespace StartingOver { class RunIt { public bool TrueOrFalse() { int four = 4; int five = 5; bool answer; Console.WriteLine("4 * 5 should equal: {0}", four * five); four*five return answer; } public void Getyear() { Console.WriteLine("What year will you be beginning?"); string year = Console.ReadLine(); Console.Clear(); Console.WriteLine("Starting all over again in {0}\n", year); } static void Main(string[] args) { RunIt ri = new RunIt(); ri.Getyear(); ri.TrueOrFalse(); } } }
Hi, Since the function is returning some value, you would need to extract the value to use it. ri.TrueOrFalse() has a value of Type Boolean. Therefore you would need a variable of Type Boolean to save the value ri.TrueOrFalse() is returning. If you would like to see if ri.TrueOrFalse() is returning True or False, you may simply check it using If Else conditions. Hope this helps.
Vinay ComponentOne LLC. www.componentone.com
-
I have been studying C# for about 4 or 5 months and then thought "Well I'll just write something from scratch. Well I felt like I've been reading a book on "How To Swim" and then when I jumped in the water I just gasped for air! Well anyway I hoping that someone could look at this and tell me how to use the return for the "public bool TrueOrFalse(). Thanks and sorry this is so elementary. I think I would like to write a book some day named a Beginner Beginner for the Basics of C#. Well any way here it is. using System; using System.Collections.Generic; using System.Text; namespace StartingOver { class RunIt { public bool TrueOrFalse() { int four = 4; int five = 5; bool answer; Console.WriteLine("4 * 5 should equal: {0}", four * five); four*five return answer; } public void Getyear() { Console.WriteLine("What year will you be beginning?"); string year = Console.ReadLine(); Console.Clear(); Console.WriteLine("Starting all over again in {0}\n", year); } static void Main(string[] args) { RunIt ri = new RunIt(); ri.Getyear(); ri.TrueOrFalse(); } } }
Right you are almost there!! lets just look at the function you are talking about
public bool TrueOrFalse()
{
int four = 4;
int five = 5;
bool answer;
Console.WriteLine("4 * 5 should equal: {0}", four * five);
four*five
return answer;
}It will always return false as it stands, because you are declaring answer but not setting it, then returning it. By default it will be set to false. All you need to do is set it, so something like this (this is done freehand so may not compile)
public bool TrueOrFalse()
{
int four = 4;
int five = 5;
bool answer = false; // always good practice to set the default :)
Console.WriteLine("4 * 5 should equal: {0}", four * five);
if((four*five) == 20)
{
answer=true;
}
//no need for an else as answer is preset to false at the declarationreturn answer;
}Hope that helps and its what you wanted