Advertisement
letsdoitjava

Bounds

Jun 12th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package Bounds;
  2. import java.util.Scanner;
  3.  
  4. public class Bounds {
  5.     public static void main(String[] args) {
  6.         // MP2 Bounds a java program that reads input from user and
  7.         // prompts user for 3 numbers: starting number, upper bound, step size
  8.         // printing no more than 10 numbers per line
  9.  
  10.         // @ author Renee Waggoner
  11.  
  12.         int startingNumber, upperBound = 0, stepSize = 0, count = 0;
  13.  
  14.         // prompting user for starting number
  15.         Scanner input = new Scanner(System.in);
  16.         System.out.println("Enter starting number: ");
  17.         // obtaining starting number input
  18.         startingNumber=input.nextInt();
  19.  
  20.         // prompt for upper bound number from user and obtaining upper bound number from user
  21.         System.out.println("Enter upper bound number: ");
  22.         upperBound = input.nextInt();
  23.  
  24.         // prompting for step size from user and obtaining step size from user
  25.         System.out.println("Enter step size: ");
  26.         stepSize = input.nextInt();
  27.  
  28.         // spacing for printed numbers
  29.         System.out.print(startingNumber + " ");
  30.         // after printing every number in loop, count by 1
  31.         count++;
  32.  
  33.         // equals is not used here to stop numbers from crossing the upper bound number
  34.         while (startingNumber < upperBound)
  35.         {  
  36.             // after printing every number in loop, counting by 1
  37.             System.out.print((startingNumber += stepSize) + " ");
  38.             count++;
  39.  
  40.             // if the count value is a multiple of 10, print empty line
  41.             // good habit here to use brackets for if statement in case of future modification
  42.             if ((count%10) == 0) {
  43.                 System.out.println();
  44.             }
  45.             input.close();
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement