In the inner for-loop all you use is j, which always runs from 1 to 3. You need an offset added. [Edit: or do it as Bantai proposes, he’s been typing faster]
But actually I’d rather not use two for-loops. As long as you use two for-loops (at least in this fixed way, simply running from 1 to n), it translates to a table with a fixed number of rows and columns, each cell being used. But your last line is shorter than the others in most cases. (I don’t want to give more clues right now, as it’s an assignment )
Assuming that you only need to work with positive numbers, you could take advantage of the modulo operator (aka mod, modulus, remainder, etc) to make your code a bit cleaner and more compact.
In Java (and many other languages) the modulo operator is represented by %
A nice little guide on its usage here:
import java.util.Scanner;
public class getSum {
public static void main(String [] args) {
Scanner input = new Scanner (System.in);
int fnum = input.nextInt();
int snum = input.nextInt();
for (int i = 1; i <= fnum; i++) {
System.out.print(i + " ");
if (i % snum == 0) {
System.out.println();
}
}
}
}
thought I’d let you know how my progress is going, since there is obviously alot of attention here.
import java.util.Scanner;
public class getSum {
public static void main(String [] args) {
Scanner input = new Scanner (System.in);
System.out.println(“How many numbers?”);
int fnum = input.nextInt();
System.out.println(“Hvor mange rows?”);
int snum = input.nextInt();
System.out.println();
int tnum = 0;
for (int i = 1; i <= fnum; i++) {
for (int j = 1; j <= snum; j++) {
if (tnum < fnum){
tnum++;
System.out.printf("%4s", tnum);
}
}
System.out.println();
}
}
}
My only problem now is, it keeps on making the last System.out.println(); as long as i = fnum, meaning, i’m getting ALOT of spaces after the numbers are done printing.