Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Need help with a java code activity

The program will use a Loop-And-A-Half to ask the user to input a number between 1 and 12. This number is used as the times table. For example, if the user enters 8, the 8 times table will be displayed.

1 Answer

import java.util.Scanner;

Scanner reader = new Scanner(System.in); 
int n;
do {
    System.out.println("Enter a number between 1 & 12: ");
    n = reader.nextInt(); // Scans the next token of the input as an int.
} while(n < 1 || n > 13);

reader.close();

for(int i = 1; i < 11; i++) {
    System.out.println("%d x %d = %d\n", n, i, n*i);
}