Results 1 to 20 of 21
- 10-23-2011, 09:31 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Printing numbers as asterisks & spaces
Hello, forum. I have a problem where I need to print a users number input and print that number as a series of asterisks and spaces as a result. It has to be in diagonal order so if the user types the number 2 the screen would show
The problem i am having is if the user types any number higher than 2, say 4 for example, the remaining numbers just print vertically instead of diagonally like this: User types 4 and the result is:Java Code:* *
I am trying to make the ouput go as follows when some types a number like 4Java Code:* * * *
for the number 4 or any number the user types like 6 for example would be:Java Code:* * * *
My code so far for this is here:Java Code:* * * * * *
import java.util.Scanner;
public class Hwk2
{//Start Class
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String ast = "*";
String space = "";
int theNumber;
System.out.println("Type a number: ");
theNumber = input.nextInt();
for (int count = 1; count<=theNumber; count++)
{
System.out.printf("*\n %s", space);
}
}//end main
}//End Class
Can anyone give me some insight on how o do this?Last edited by Cnos; 10-24-2011 at 05:37 AM.
- 10-23-2011, 07:12 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
The string is empty. You didn't actually put a space in there.String space = "";
Also, for future reference, use [code] tags when posting source code.
- 10-24-2011, 02:12 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
I added the space = " "; but im having the same problem
- 10-24-2011, 05:06 AM #4
Re: Priting numbers as asterisks & spaces
Take a look at your first post. Notice how all the formatting of the stars has disappeared. We have no idea what the desired output should look like. Try wrapping code tags around how the stars should look.
- 10-24-2011, 05:38 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
Sorry about the formatting issues originally with the first post but i have edited it and it now shows what im trying to do. Can anyone help?
- 10-24-2011, 05:54 AM #6
Re: Priting numbers as asterisks & spaces
This is a simple task achieved with nested loops. The outer loop prints N (user input) rows. While the inner loop prints M spaces. Then after the inner (spaces) loop you print a single star and a new line.
- 10-24-2011, 06:06 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
So with the code i have now, according to what you said, I need to create the inner loop, print the statement outside of the loop? I have tried so several different combos of doing this with the same results
- 10-24-2011, 06:15 AM #8
Re: Priting numbers as asterisks & spaces
What your current code (above) does is print a star, then a newline and then a space (or whatever that String holds). It is not in the order it should be.Java Code:System.out.printf("*\n %s", space);
- 10-24-2011, 06:27 AM #9
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
- 10-24-2011, 06:31 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
This is my outter and inner loop so far. What am i doing wrong in the inner loop, as i know the outter loop works?Java Code:for (int count = 1; count<=theNumber; count++) { for(int now = 1; now<count; now++) { System.out.println(" "); }
- 10-24-2011, 06:36 AM #11
- 10-24-2011, 06:37 AM #12
Re: Priting numbers as asterisks & spaces
Do you know how many spaces should be printed on each line? Write the pattern out on graph paper if it helps. You should see a pattern.
- 10-24-2011, 07:40 AM #13
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
- 10-24-2011, 07:56 PM #14
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
try this-
for(int i=1;i<=theNumber;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
System.out.println("*")
}
- 10-24-2011, 11:08 PM #15
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
Cosmos
Thank you very much! That works. I cant believe it was that simple. I was confused with the nesting part at first, but after studying your code, I now completely understand what was done which will help me out for future reference. Thank you again
- 10-25-2011, 01:33 AM #16
Re: Priting numbers as asterisks & spaces
@Cosmos
Please do not post fully coded solutions. It is the task of the OP to derive a solution not yours. Although Cnos claims they have learnt from your code, 99% of the time people do not. They simply copy and paste your solution and hand it in. The next time they have a problem they will be back here expecting you to spoonfeed them again. We try to encourage people to think for themselves.
- 10-25-2011, 01:57 AM #17
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
Re: Priting numbers as asterisks & spaces
I agree with Junky. A better approach might have been to ask: "do you know what nested loops are?" Or advise: "break Junky's advice down into sections - first write a loop which prints N (user input) rows."Java Code:// This is a simple task achieved with nested loops. for(int i=1;i<=theNumber;i++) // The outer loop prints N (user input) rows. { for(int j=1;j<i;j++) // While the inner loop prints M { System.out.print(" "); // spaces. } System.out.println("*") // Then after the inner (spaces) loop you print a } // single star and a new line.
Just my opinion, of course. By "better" I mean more useful, because I doubt whether drawing star patterns on the console is a skill which will be much referenced in the future. While knowledge of what the term "nested loops" means, and translating an English description of one, step by step, into code are skills which - if practiced and exercised - will be useful over and over again.
- 10-25-2011, 02:32 AM #18
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Priting numbers as asterisks & spaces
The reason I came here in the first place was because i needed help. And he didnt give me the entire code, he showed me how to include the spaces the way I wanted them. From that code I was able to bring up a notepad test it and compare to the actual code to find out how it worked, which broke down the original loop incremented as many times as the user input, and the second loop incremented off the first loop minus one. I was not able to understand that until i could break it down this way with your instructions @Junky, so if you wanted to be a jerk-off about it you didnt have to reply. 99% of the people just copy and pasted it and hand it in. For your information, i didnt just copy and paste and hand it in, becasue the assignment was due yesterday. Everything from me and cosmos posts are after the fact. Yes I studied that code after handing in an incomplete assignment. I was not simply looking to copy and paste it, I needed help as to how to do it. Now I have a better understanding of for loops which i am studying, i told you i was a newbie and i had tried this for quite awhile before i decided to come to this forum. And @ pbrockway2, I was not sure at the time if i should do it with a nested statement or not, because i almost had it without the nested statement and I was told that this could be done with just one statement.
- 10-25-2011, 02:33 AM #19
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
- 10-25-2011, 03:28 AM #20
Re: Priting numbers as asterisks & spaces
There is a difference between help and being given the answer. I was helping you by trying to get you to arrive at the solution yourself. Just giving you the code does not help you. If you think it does then write a program that does this:
Java Code:input = 4 **** *** ** * ** *** ****
Similar Threads
-
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
Turning numbers into asterisks.
By BugginVT in forum New To JavaReplies: 7Last Post: 02-25-2009, 07:38 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
printing an "E" out of asterisks via strings
By hokieman07 in forum New To JavaReplies: 1Last Post: 04-08-2008, 05:45 AM -
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks