stuck on an assignment and need help with repetition
Hi, Im new to java and have been issued my first assignment, ive coded my menu and am beginning to code my first sub section of the menu which is a character patter printer, basically what it does is prompts the user to enter the ammound of rows and the character they would like to use.
please enter number of rows : 5
please enter a character : c
output :
c
cc
ccc
cccc
ccccc
i cant figure out how to get the character to appear on the line more then once. here is my code for when the user selects a, bare in mind that i am new and this is my first assignment and loops may nit be the correct way to go about it, but its all i could think of with what i've learned so far
// Menu selection A, character pattern, will ask user
// for number of rows and a character to enter
if (menuChoice == 'A' || menuChoice == 'a'){
// If a number exceeding 25 is entered, the loop wont allow
// the program to continue untill a number between 1 - 25
// is entered
while (patternRows > 25) {
System.out.println("Welcome to Character pattern printing");
System.out.println("Please enter # of Rows (max 25)");
System.out.print("Wont allow a number higher than 25 : ");
// Taking user response and storing it in the
// variable patternRows
patternRows = scanner.nextInt();
}
System.out.print("Please enter a character : ");
patternChar = charInput.readLine().charAt(0);
while (linesPrinted != patternRows){
System.out.println(patternChar);
linesPrinted ++;
}
Re: stuck on an assignment and need help with repetition
If you're like adding only one more character for each row, and not having variations, for instance
you're
a
aa
aaa
aaaa
aaaaa
and not
a
aa
a
aaaa
aa
Then you should use a String to keep adding one char and perhaps use a for or while loop.
You could have, before the for or while loop, a String variable called chars
String chars = "";
Also, I'd recommend that you have only 1 Scanner object unless you're reading in from a file as well as from the keyboard.
After you've read in the char value and the number of rows, you could
for (int i =0; i < patternRows; i++)
{
chars = chars + String.valueOf(patternChar);
System.out.println(chars);
}