Advertisement
aznGiLL

Untitled

Oct 28th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. /**
  2.  * @(#)oppgave3b.java
  3.  *
  4.  *
  5.  * @Anders Gill
  6.  * @version 1.00 2011/10/23
  7.  */
  8.  
  9. import java.util.Scanner; // Scanner
  10.  
  11. public class oppgave3b { // name of the class
  12.  
  13.     public static void main(String[] args) { // startingbracket for mainz
  14.         Scanner console = new Scanner(System.in); // decleration of scanner
  15.         System.out.print("Oppgi øvre grense: "); // requests int maxNumber from user
  16.             int maxNumber = console.nextInt();
  17.         System.out.print("Oppgi antall tall på hver linje: "); // requests int numbersPerLine from user
  18.             int numbersPerLine = console.nextInt();
  19.  
  20.         printNumbers(maxNumber, numbersPerLine); // Prints the numbers thru the method printNumbers
  21.  
  22.     } // endingbracket for main
  23.  
  24.     public static void printNumbers(int maximumNumber, int numbersPerLine) {
  25.         int counter = 0; // how many numbers currently on the line
  26.         for(int x = 1; x <= maximumNumber; x++) { // startingbracket for the loop
  27.             System.out.printf("%4d", x);
  28.             counter++; // add 1 to the counter because new number on line
  29.             if(counter == numbersPerLine) { // once x amount of numbers are on the line
  30.                 System.out.println(); // start new line
  31.                 counter = 0; // reset counter, new line
  32.                 }
  33.             } // endingbracket for the loop
  34.         } // endingbracket for the method printNumbers
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement