-1

So my homework assignment is to prompt a user to input a positive integer value, and use a while loop to make sure the value is not negative. Once the value is positive, the program should print out the input value as the number of lines with 2 pound signs starting with no space between them and spaces incrementing in each line.

so far I have this, but not really sure how to progress from here.

    import java.util.Scanner;

    public class Pattern
    {
        public static void main(String[] args)
        {
            Scanner kb = new Scanner(System.in);
            System.out.print("Enter a positive integer: ");
            int positiveInt = kb.nextInt();
            while (positiveInt < 0)
            {
                    System.out.print("That isn't positive, try again: ");
                    positiveInt = kb.nextInt();
            }
    }
/* an example would look like this:
##
# #
#  #
#   #
#    # */
javac
  • 2,431
  • 4
  • 17
  • 26
m3manny
  • 1
  • 1
  • 1
    Think about what needs to happen every iteration to make the next hashes/pounds further apart. The spaces are increasing by 1 every line/iteration, so you need to have something increase by 1 every loop that decides how many spaces to add. – Carcigenicate May 04 '15 at 22:07
  • would I need to add another while loop that uses the value inputted to print the program? Because if I'm remembering correctly this loop should end once a positive integer is entered. – m3manny May 04 '15 at 22:13
  • You need to add a line with pounds every time the user enters a positive number? You would only need 1 loop. I would add a `int` variable that gets incremented by 1 every iteration, and use that variable to decide how many spaces to add between the pounds. – Carcigenicate May 04 '15 at 22:57
  • maybe this is where I'm not really understanding it correctly. Wouldn't the while loop I already used just be finished once a positive integer is entered? So that loop is just used to determine if the original int is positive or negative, then another loop, possibly a for loop, would be used to make the spaces between the hashes. – m3manny May 04 '15 at 23:42
  • Again, is a line of hashes supposed to be output once every time the user enters a number, or are you only supposed to ask the user for a number once? – Carcigenicate May 04 '15 at 23:46
  • Oh, I thought your question was rhetorical for some reason. So the loop would be used to get the positive value. Then once a positive number is received, that value would used to determine the number of lines and the number of spaces. spaces would be value - 1. So only ask the user for a number once if that number is positive. – m3manny May 04 '15 at 23:51
  • Ah, sorry, that was my mistake. Do you know how to use a `for` loop? – Carcigenicate May 04 '15 at 23:55
  • yeah, i sort of do. We went over them quickly at the end of class. I guess the main problem I'm having is determining how to use the int variable you mentioned earlier to decide how many spaces to add. – m3manny May 05 '15 at 00:02
  • I'd use a `for` loop. `for` loops are similar to `while` loops, except they also initialize and manage a variable. Instead of `while(condition)`, it's `for(new variable; condition; what to do at the end of the loop)`. Ill post a small example as an answer. I've been hesitant to because I don't want to do your homework for you; that won't help you any, and will give me downvotes. – Carcigenicate May 05 '15 at 00:09
  • Sorry but if I helped you, can you vote on my answer please? I would appreciate it. – Carcigenicate May 05 '15 at 19:55
  • I would, but it says that my account is too new to vote. Once I get enough rep I'll make sure to come back and vote for your answer. Thanks once again! – m3manny May 06 '15 at 02:10
  • Oh, right, I forgot about that. And thank you. – Carcigenicate May 06 '15 at 02:13

1 Answers1

0

After your while loop gets the number of iterations from the user, I'd recommend using a new int variable to store how many times you've looped. Once the variable exceeds what the user gave you, you exit the loop. This could be written as:

int counter = 0;
while (counter < positiveInt) {

    //Do stuff with counter

    counter++ //Increase counter by 1
}

This is such an often used pattern though, for loops were introduced to allow this to be written easier:

for (int counter = 0; counter < positiveInt; counter++) {

    //Do stuff with counter

}

Notice all the counter stuff is tucked away inside the loop, but at the top so you can focus on using counter instead of managing it.

Now that you have a counter that increases every loop, you can use it to determine how many spaces to add. This could be done easily with a small function:

std::string createNSpaces(int n) {
    std::string spaces = "";

    for (int i = 0; i < n; i++) {

        spaces += " ";

    }

    return spaces;
}

Notice the for loop inside the function that adds spaces to spaces until the counter (i) exceeds n. Once we've made up a string of spaces, we return it.

Please don't blatantly copy-and-paste code here. You won't learn anything, and you'll pay for it down the road when you need to figure out another problem. Think about the thought process (What am I trying to do? What do I need?), and practice breaking problems down into smaller, more manageable pieces. In this code, the 3 pieces would be getting input from a user, creating enough spaces (which could arguably be written inlined instead of as a function), and outputting the #s.

Good luck.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • ok, I think I understand now. Your method for returning spaces is sort of unfamiliar to me but I'll definitely look into it. I definitely won't blatantly copy-and-paste, I enjoy doing this so I definitely want to learn correctly. Thank you! – m3manny May 05 '15 at 01:29
  • Note that the function is fairly inefficient. Using `+=` (add to it, to it, and reassign) is easy and short, but involves constantly traversing the string, which is slow. The contents of the function could also be put directly inside `main` instead if you think you'll never need the code again, but using a function lets you re-use the same code elsewhere. – Carcigenicate May 05 '15 at 01:34