Advertisement
drmomentum

FunnySum for a Desk Check Example

Jun 19th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. /**
  2.  *
  3.  * Purpose: This funny number summer sums numbers in a funny way, and it's meant
  4.  * to provide an example of how to desk check a program.
  5.  *
  6.  *
  7.  * @author James P. Burke
  8.  *
  9.  */
  10.  
  11. public class FunnySum {
  12.  
  13. /**
  14.  * The main method sums some numbers and outputs some progress along the way.
  15.  * @param args
  16.  */
  17.     public static void main(String[] args) {
  18.  
  19.         int a;
  20.         int x = 0;
  21.         int y = 0;
  22.         int r = 0;
  23.         while (x < 10) {
  24.             a = x * 2;
  25.             r = a % 3;  // r is set to the remainder of a divided by 3
  26.             switch (r) {
  27.             case 0:
  28.                 y += a;
  29.                 System.out.println(a + " must be divisible by three!");
  30.                 break;
  31.             case 1:
  32.                 y += x;
  33.                 break;
  34.             case 2:
  35.                 y += r;
  36.                 break;
  37.             default:
  38.                 System.out.println("We should never actually get here.");
  39.                 break;
  40.             }
  41.             x ++;
  42.         }
  43.         System.out.println("The sum is " + y);
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement