Can anyone help?
Hello
I am having some trouble getting this code to work, it is meant to print out the character and the number of rows that the user inputs in a triangular pattern using nested loops. Can anyone assist please.Thanks
import java.util.Scanner;
public class pattern
{
public static void main(String[] args) {
System.out.println("Enter the number of rows in pattern: ");
row = sc.nextInt();
System.out.println("Enter the character to print: ");
c = sc.next().charAt(0);
for (int i=0; i<=row; i++) {
for (char ch=c; ch<=c; ch++)
System.out.print(ch + " ");
}
System.out.println();
}
[698 byte] By [
geloatoa] at [2007-11-27 9:42:40]

Your inner loop is all wrong. Remember that chars are simply numbers, ie the letter a is repesented by the number 97. So if you increment it by one it becomes 98 or the letter b. Is this what you want?
BTW when posting code, use code tags. Highlight the code and click the code button. Don't use bold.
Hi
Sorry no that is not what i want but i am very confused, i have been going at this for hours.
import java.util.Scanner;
public class apples
{
public static void main(String[] args) {
System.out.println("Enter the number of rows in pattern: ");
row = sc.nextInt();
System.out.println("Enter the character to print: ");
c = sc.next().charAt(0);
for (int i=0; i<=row; i++) {
for (char ch=c; ch<=c; ch++)
System.out.print(ch + " ");
}
System.out.println();
}
OK, let me try another tack. Your outer loop controls how many rows to print. You inner loop should control how many times the character should be printed on the current line. Does your inner loop do this?
I keep getting it wrong, it aint working i am not sure how to fix it.
import java.util.Scanner;
public class pattern
{
public static void main(String[] args) {
System.out.println("Enter the character to print: ");
c = sc.next().charAt(0);
System.out.println("Enter the number of rows in pattern: ");
row = sc.nextInt();
for (char ch=c; ch<=c; ch++)
System.out.print(ch + " ");
for (int i=0; i<=row; i++) {
}
System.out.println();
}
>for (char ch=c; ch<=c; ch++)What do you expect this to do?
jverda at 2007-7-12 23:46:35 >

Did you even read my previous reply. You outer loop was correct. Your inner loop was wrong. So your solution was to swap them around!!!!!
Lets llok at your inner loop. You assign ch to be the same as c. Lets say I entered a(97 is the ascii value). Is ch <= c (ie is 97<= 97) yes so enter loop. You then increment ch (it is now b or 98) is 98 <= 97, no so you do not enter the loop again. THIS IS NOT WHAT YOU WANT. What you want is for the inner loop to print once for the first row, twice for the second row, three times for the third row. See a pattern?
Assuming you want this:
*
**
***
****
*****
HiIt is meant to check for when the loop should end
I know but the check you are using is wrong.
> Hi> > It is meant to check for when the loop should endI meant in detail.
jverda at 2007-7-12 23:46:35 >

for (int i=0; i<=row; i++){for (char ch=c; ch<=i; ch++){System.out.print(ch + " "); System.out.println( );this is what i have tried but it still aint happening for me.
sighWe know it isn't working. We have been telling you that but you don't seem to be listening.Is the output you want like this?***************
Hiyes that is the result i am hoping for, in regard to me not listening i have changed it around several times so i have been listening i just dont understand.Thanks
Like I said earlier:row 1 should print 1 charrow 2 should print 2 charsrow 3 should print 3 charsetcSee the pattern? Tell me how using ch is going to help you determine how many times to print a char.
should i not be using the char instead be using the int to count the number of characters per row
No, why? chars are for characters, ints are for integers. You count with integers, you display with characters.
ejpa at 2007-7-21 23:08:42 >

import java.util.Scanner;
public class pattern
{
public static void main(String[] args) {
System.out.println("Enter the number of rows in pattern: ");
row = sc.nextInt();
System.out.println("Enter the character to print: ");
c = sc.next().charAt(0);
for (int i=0; i<=row; i++) {
for (char ch=c; ch<=c; ch++)
System.out.print(ch + " ");
}
System.out.println();
Well what is the correct input then because i am going round in circles changing things around and its not working?
Stop. Stop going around in circles, stop just 'changing things around', get a cup of coffee, and have a proper think. This is a very logical business, and random approaches to problem-solving are ineffective.
ejpa at 2007-7-21 23:08:42 >

Can someone please tell me if i am changing the right part of the code or else i am going around in circles.for (int i=0; i<=row; i++) {for (char ch=i; ch<=i; ch++)System.out.print(ch + " ");}System.out.println();
> for (char ch=i; ch<=i; ch++)You haven't answered my question: What, in detail, is this supposed to do? Why did you write it? Explain each piece of it.If you can't or won't do that, then you're not programming; you're just guessing.
jverda at 2007-7-21 23:08:42 >
