• 0

[C#] Adding text problem


Question

Ok here we go. I'm doing my degree in network engineering and i have programming as one of my classes. As part as my assignment i have to create a quiz. I have created the quiz everything works now im just adding bits to it. Here is my problem

i have a line of code that tells you how many questions you got right. (aka the code below)

Console.WriteLine("You Scored " + rightAnswer);

the 'rightAnswer' tells me how many you got right (e.g. 6) What i want is after the number i want another piece of text that says "out of 12)

so when it comes up it will say "You scored '12' out of 12"

How do i do that?

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0
Ok here we go. I'm doing my degree in network engineering and i have programming as one of my classes. As part as my assignment i have to create a quiz. I have created the quiz everything works now im just adding bits to it. Here is my problem

i have a line of code that tells you how many questions you got right. (aka the code below)

Console.WriteLine("You Scored " + rightAnswer);

the 'rightAnswer' tells me how many you got right (e.g. 6) What i want is after the number i want another piece of text that says "out of 12)

so when it comes up it will say "You scored '12' out of 12"

How do i do that?

Console.WriteLine("You Scored " + rightAnswer.ToString() + " out of 12");

Link to comment
Share on other sites

  • 0

Wahey thank you...i struggle so much at this...i just can't seam to get my head around it. Just need to work out the percentage then im pretty much done. Thanks for your help :)

Link to comment
Share on other sites

  • 0
Wahey thank you...i struggle so much at this...i just can't seam to get my head around it. Just need to work out the percentage then im pretty much done. Thanks for your help :)

glad to help ;)

Link to comment
Share on other sites

  • 0

It's actually preferred and better to use the following:

Console.WriteLine("You Scored {0} out of 12", rightAnswer);

The {0} will be replaced with the first variable or value after the string. You can keep adding more such as {1}, {2} in your string. So you could have.

Console.WriteLine("You scored {0} out of {1}", rightAnswer, totalQuestions);

You don't need to call .ToString() if you are working with built in datatypes like integers and etc.

Link to comment
Share on other sites

  • 0
Ok here we go. I'm doing my degree in network engineering and i have programming as one of my classes. As part as my assignment i have to create a quiz. I have created the quiz everything works now im just adding bits to it. Here is my problem

i have a line of code that tells you how many questions you got right. (aka the code below)

Console.WriteLine("You Scored " + rightAnswer);

the 'rightAnswer' tells me how many you got right (e.g. 6) What i want is after the number i want another piece of text that says "out of 12)

so when it comes up it will say "You scored '12' out of 12"

How do i do that?

Just add to the string at the end.

Like this:

Console.WriteLine("You Scored " + rightAnswer + " out of 12");

I hope that helps.

Whoops, I was beaten to it. LOL

Link to comment
Share on other sites

  • 0

so many ways...i dunno which one to choose haha. Now i have to figure out how to work out a percentage for the correct answers...hmmm

Thanks a lot :)

Link to comment
Share on other sites

  • 0

The one I posted is the cleanest and more preferred solution.

total correct / number of questions * 100

This is the basic formula to calculate percentages.

double percentComplete = (double) rightAnswer / totalQuestions * 100;
Console.WriteLine("Percent correct is {0:0.0}", percentComplete);

The {} is for string replacement which I described above. The : is used to say we will format this string a certain way. The 0.0 is how the string will be format. So, {0:0.0} would say this is the 1st argument (programming starts with 0), and the number will be formatted and rounded to 1 decimal place. Using (double) will tell the computer you want everything interpreted as a double (fractional number). Computers will do math differently based on the variable type.

For instance if you did 3 / 2, you would get 1. However, if you did (double) 3 / 2, you would get 1.5.

Console.WriteLine("Percent correct is {0:0.0}", 10.293);

This would print "Percent correct is 10.3".

double percentComplete = (double) rightAnswer / totalQuestions * 100;
Console.WriteLine("Percent correct{0:0}", percentComplete);

If you only want to display a whole number percentage, you this. Using string formatting carries out proper rounding.

Edited by Xilo
Link to comment
Share on other sites

  • 0

your a great help Xilo

i used this way

int percentage = Convert.ToInt32(rightAnswer * 100 / 12);

and it worked, well gave the right answer anyway. is this way wrong? or not a done way of doing what im asking even tho it gave the right answer?

Link to comment
Share on other sites

  • 0

Yeah. You can use that way too. :)

If you want whole number percentages, that would be a way to do it. Not sure if it does rounding though.

What I posted is basically for making things look pretty. :D

Edited by Xilo
Link to comment
Share on other sites

  • 0

I'm really appreciative of your help. I don't want to make things look to pretty and put in a lot code that the tutor hasn't taught us how to do, encase he thinks ive gotten someone else to code it for me.

Link to comment
Share on other sites

  • 0

I don't see the problem, you asked for help and you learned how to do it a better way. If you understand the code and can repeat it then I don't see how the tutor will think you got someone else to do the code. Hell, that is learning.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.