hi guys i have a problem i need to solve that i have been working on serveral of hours and i just cant solve it..
i did a Multiplication table with 2 loops and a Console.Write();
and it work fine.
but now i need to do it only with Console.WriteLine();
and i just cant find a way to do it.
can somone help me "get there"
without showing me any code so my stupid brain will get it?
p.s
sorry for my english
Multiplication table
Page 1 of 112 Replies - 4198 Views - Last Post: 11 January 2011 - 01:14 PM
Replies To: Multiplication table
#2
Re: Multiplication table
Posted 10 January 2011 - 08:04 AM
You're saying you need to print out a multiplication table without loops? Start writing each individual 1 x 1, 1 x 2, 1 x 3, etc...because you need loops to do this right.
#3
Re: Multiplication table
Posted 10 January 2011 - 08:11 AM
insertAlias is right. You can't do it with *just* console.WriteLine().
Go back to the homework instructions and read them again.
If that doesn't help, go to the teacher who assigned this and get a better explanation of the assignment.
Go back to the homework instructions and read them again.
If that doesn't help, go to the teacher who assigned this and get a better explanation of the assignment.
#4
Re: Multiplication table
Posted 10 January 2011 - 08:16 AM
oops i didnt explain my self good.
i can use loops
but i cant use the command
Console.Write();
and
i can do any thing i want like using string and stuff
i can use loops
but i cant use the command
Console.Write();
and
i can do any thing i want like using string and stuff
#5
Re: Multiplication table
Posted 10 January 2011 - 08:18 AM
So this is a school project then?
Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
#6
Re: Multiplication table
Posted 10 January 2011 - 08:23 AM
insertAlias, on 10 January 2011 - 07:18 AM, said:
So this is a school project then?
Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
Quote
but i cant use the command Console.Write();
Buffering it isn't going to matter if the OP can't use Console.Write() to output the results.
What's the alternative?
#7
Re: Multiplication table
Posted 10 January 2011 - 08:23 AM
i still dont know about buffer and string builders
can u expline me the
use string and keep addint to it part?
(without showing me the answer to my specific problem)
becuse i want to understand to way and apply it by my self.
can u expline me the
use string and keep addint to it part?
(without showing me the answer to my specific problem)
becuse i want to understand to way and apply it by my self.
#8
Re: Multiplication table
Posted 10 January 2011 - 08:26 AM
tlhIn, on 10 January 2011 - 08:23 AM, said:
insertAlias, on 10 January 2011 - 07:18 AM, said:
So this is a school project then?
Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
Quote
but i cant use the command Console.Write();
Buffering it isn't going to matter if the OP can't use Console.Write() to output the results.
What's the alternative?
According to the initial post, he can use Console.WriteLine. I assume the lesson is in buffering values and outputting them all at once.
@thesamy
The general idea is that inside the loop where you normally would do your Console.Writes, you create an empty string as a buffer. Then every place you would have Console.Write(someString), you instead add what you would have outputted to the buffer (use the += operator). Then, at the point where you're ready to go to the next line, you Console.WriteLine the entire buffer.
This post has been edited by insertAlias: 10 January 2011 - 08:27 AM
#9
Re: Multiplication table
Posted 10 January 2011 - 08:36 AM
thank u i think i get it
i will post qustion about it if i get in trouble agian
agian thank u both for fast reply!
i will post qustion about it if i get in trouble agian
agian thank u both for fast reply!
#10
Re: Multiplication table
Posted 11 January 2011 - 09:33 AM
got another problem that i need somone to expline it to me
i need to make a program that write line of stars with the user input.
exmple.
user type in 3
he get
*
**
***
now at first i need to do it with 2 loops and i did and everything work fine
but now i need to do it with 1 loop but i got a problem that i dont know how to
like restrt the string i buffer afther i Console.WriteLine() it.
here is the code:
i just start to use the buffer in my last program so i realy not sure how to do it.
can somone expline it to me without show in the sulotion?
i need to make a program that write line of stars with the user input.
exmple.
user type in 3
he get
*
**
***
now at first i need to do it with 2 loops and i did and everything work fine
but now i need to do it with 1 loop but i got a problem that i dont know how to
like restrt the string i buffer afther i Console.WriteLine() it.
here is the code:
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the number of lines");
int line = int.Parse(Console.ReadLine());
string stars = "*";
for (int i = 1 ; i <= line ; i++)
{
Console.WriteLine(stars);
stars += stars;
}
}
}
}
i just start to use the buffer in my last program so i realy not sure how to do it.
can somone expline it to me without show in the sulotion?
#11
Re: Multiplication table
Posted 11 January 2011 - 09:40 AM
Your logic is only slightly wrong here. You don't want to reset the buffer, in this case. The best thing you can do is take a pen and paper, and write down everything that is happening. Draw a long box for your variable stars and a box for line and i.
Then trace your execution. Change the content of the boxes as you go. You should notice that you're adding stars to stars, in effect doubling the number of stars you put on each line rather than increasing them by one.
Then trace your execution. Change the content of the boxes as you go. You should notice that you're adding stars to stars, in effect doubling the number of stars you put on each line rather than increasing them by one.
This post has been edited by insertAlias: 11 January 2011 - 09:41 AM
#12
Re: Multiplication table
Posted 11 January 2011 - 09:48 AM
wow i get it now its working!
thank u!!
thank u!!
#13
Re: Multiplication table
Posted 11 January 2011 - 01:14 PM
You can construct the triangle using only one line of code. You would use the constructor of the string class. Here is an example:
That would print 5 *'s. You can use the initalizer variable of the for loop ('i') in place of the 5 in the above code.
Think about it this way, your looping through each line in your for loop aren't you? So, on the first loop through, 'i' is 1 (representing line 1), on the second loop through, it's 2 (representing line 2) etc. The line number is equal to the number of stars you have to print (line 1 = 1 stars, line 2 = 2 stars.
Now, can you use the code above, in a for loop, to print out the triangle by replacing the 5 with i?
Console.WriteLine(new String('*',5);
That would print 5 *'s. You can use the initalizer variable of the for loop ('i') in place of the 5 in the above code.
Think about it this way, your looping through each line in your for loop aren't you? So, on the first loop through, 'i' is 1 (representing line 1), on the second loop through, it's 2 (representing line 2) etc. The line number is equal to the number of stars you have to print (line 1 = 1 stars, line 2 = 2 stars.
Now, can you use the code above, in a for loop, to print out the triangle by replacing the 5 with i?
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|