0
import java.util.Scanner;

public class Question5 {

    public static void main(String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int x = keyboard.nextInt();
        System.out.print("Enter a char: ");
        String c1 = keyboard.nextLine();
        System.out.print("Enter a char: ");
        String c2 = keyboard.nextLine();
        printChars(x, c1, c2);

    }

    public static void printChars(int x, String c1, String c2) {
        for (int i = 1 ; i <= x ; i++) {//
            for (int z = 1 ; z <= x - i ; z++)//
                System.out.print(c1);
            for (int j = 1 ; j <= x ; j++)//
                System.out.print(c2);
            System.out.println();
        }
    }
}

it's the output i got now

 ----jGRASP exec: java Question5

Enter a number: 8
Enter a char: 
Enter a char: 
6
66666666
66666666
66666666
66666666
66666666
66666666
66666666
66666666

----jGRASP: operation complete.

but it shoud print like this

Enter a number: 5
Enter a char: %
Enter a char: $

% % % % %
% % % % $
% % % $ $
% % $ $ $
% $ $ $ $
$ $ $ $ $

please help me with this it's my first question on this forum

null
  • 2,146
  • 3
  • 21
  • 36

2 Answers2

1

Instead of using keyboard.nextLine(), just use keyboard.next() for reading strings.

0

This looks like a homework question; don't expect homework answers here. Instead, try and debug it yourself, it's more instructive.

First of all, try and clearly describe to yourself (or someone else) what behaviour exactly is 'wrong' (e.g. capturing input or processing of captured input). It might also help to break down the program into 2 parts e.g.

  1. Input - which behaved unexpectedly when I tested your code
  2. Processing - basically the contents of the printChars method which also behaved unexpectedly.

Identify which section you want to tackle first (personally, I'd tackle the printChars method first since I know exactly what I expect of it) and work through it methodically.

Good luck.

P.S. You're really close so don't give up.

user3337410
  • 94
  • 1
  • 10