-4

The text of the problem is:

printed next to any number that is by the first [user] entered number.

public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    int add = num+=num;

    for (int i = 1; i <= 10; i++){
    
        String s1 = String.valueOf(i);
        String s2 = c;
        String s3 = s1.concat(s2);
        String r1 = String.valueOf(i);
        String r2 = c1;
        String r3 = s1.concat(r2);

        System.out.println(i + s2 + r2);
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Josh
  • 3
  • 2
  • 1
    Hello and welcome. What is your question? – Federico klez Culloca Sep 25 '21 at 08:14
  • "the character you choose will be added to the number you pick and the next number added by itself" — I don't know what that means. Is there further or better explanation? – khelwood Sep 25 '21 at 08:16
  • Like the [Fizz buzz game](https://en.wikipedia.org/wiki/Fizz_buzz). The first character is printed next to any number that is divisible by the first [user] entered number. In the example in the question, the number is 2 and the character is `#`. Hence any number divisible by 2 is printed with a `#` next to it. Similarly for the second number (4 in the example in the question) and second character (`@` in the example). Any number divisible by both 4 and 2 has both characters printed next to it. – Abra Sep 25 '21 at 08:30

1 Answers1

1

In the for loop you need to check if i is divisible by either num or num1 and print the appropriate letter if it is.

Method nextLine should be called after the call to nextInt and before a subsequent call to nextLine. Refer to Scanner is skipping nextLine() after using next() or nextFoo()?

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("\nEnter 1st Number: ");
    int num = input.nextInt();
    input.nextLine();
    System.out.print("\nEnter character: ");
    String c = input.nextLine();

    System.out.print("\nEnter 2nd Number: ");
    int num1 = input.nextInt();
    input.nextLine();
    System.out.print("\nEnter character: ");
    String c1 = input.nextLine();
    for (int i = 1; i <= 10; i++) {
        System.out.print(i);
        if (i % num == 0) {
            System.out.print(c);
        }
        if (i % num1 == 0) {
            System.out.print(c1);
        }
        System.out.println();
    }
}

Here is output from a sample run of the above code.

Enter 1st Number: 2

Enter character: &

Enter 2nd Number: 3

Enter character: *
1
2&
3*
4&
5
6&*
7
8&
9*
10&

You can also achieve the result using string concatenation, as shown in the below code.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("\nEnter 1st Number: ");
    int num = input.nextInt();
    input.nextLine();
    System.out.print("\nEnter character: ");
    String c = input.nextLine();

    System.out.print("\nEnter 2nd Number: ");
    int num1 = input.nextInt();
    input.nextLine();
    System.out.print("\nEnter character: ");
    String c1 = input.nextLine();
    String output;
    for (int i = 1; i <= 10; i++) {
        output = "" + i;
        if (i % num == 0) {
            output += c;
        }
        if (i % num1 == 0) {
            output += c1;
        }
        System.out.println(output);
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41