fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.Scanner;
  8.  
  9. class numbersAssignment {
  10.  
  11. static int numberOne = 0;
  12. static int numberTwo = 0;
  13. static int numberThree = 0;
  14. static int largest = 0;
  15. static int smallest = 0;
  16. static int product1 = 0;
  17. static int sum = 0;
  18. static int average = 0;
  19. static boolean numbersDiffer = false;
  20. static int doItAgain = 1;
  21. static Scanner input = new Scanner(System.in);
  22.  
  23. public static void main(String[] args) {
  24.  
  25.  
  26. while (doItAgain != 0) {
  27. numbersDiffer = false;
  28. while (numbersDiffer != true) {
  29.  
  30. numberOne = getNumber();
  31. numberTwo = getNumber();
  32. numberThree = getNumber();
  33. if (verifyDiff(numberOne, numberTwo, numberThree)) {
  34. calcPrintNumbers(numberOne, numberTwo, numberThree);
  35. numbersDiffer = true;
  36. }
  37.  
  38.  
  39. }
  40. //where it all goes wrong - doAgain() stuck...
  41. doItAgain = (doAgain());
  42.  
  43. }
  44.  
  45. }//main
  46.  
  47.  
  48. /*
  49.   ******************************************************************************
  50.   * getNumber: *
  51.   * This method will ask the user for the number that is to be used in the *
  52.   * program. All numbers MUST BE INTEGERS, and must use DIFFERENT values. * *
  53.   ******************************************************************************/
  54. public static int getNumber()
  55. {
  56. int number = 0;
  57.  
  58.  
  59. boolean done = false;
  60. while (done != true) {
  61. try {
  62. System.out.println("Please enter a UNIQUE integer for the program ===> ");
  63. number = input.nextInt();
  64. if (number <= 0){
  65. throw new NumberFormatException();
  66. }
  67. done = true;
  68. }//try
  69. catch (Exception message) {
  70. input.nextLine();
  71. System.out.println("Bad input, retry");
  72. }//catch
  73.  
  74. }//while
  75. return number;
  76.  
  77. }//getNumber
  78.  
  79. /*
  80.   ******************************************************************************
  81.   * calcPrintNumbers: *
  82.   * This method will recieve the three user input variables. The program *
  83.   * will then calculate and print, the SUM,AVERAGE,PRODUCT,LARGEST, as well*
  84.   * as the SMALLEST of the three numbers. It will then print the results, *
  85.   * AS WELL AS THE VALUES STORED IN THE THREE VARIABLES. *
  86.   ******************************************************************************/
  87. public static void calcPrintNumbers(int numberOne, int numberTwo, int numberThree)
  88. {
  89. System.out.println("The smallest number is: " + Math.min(numberOne, Math.min(numberTwo, numberThree)));
  90. System.out.println("The largest number is: " + Math.max(numberOne, Math.max(numberTwo, numberThree)));
  91. System.out.println("The average is: " + ((numberOne + numberTwo + numberThree) /3));
  92. System.out.println("The product is: " + Math.multiplyExact(numberOne, Math.multiplyExact(numberTwo, numberThree)) );
  93. System.out.println("The sum is: " + (numberOne + numberTwo + numberThree));
  94.  
  95.  
  96. }//End of the calcSumPrint method
  97. /*
  98.   *******************************************************************************
  99.   * doAgain: *
  100.   * This method will NOT receive incoming data, but it will it will *
  101.   * ask for, verify, and return the users choice of whether to continue the *
  102.   * program or not. The code looks for a choice of 1 to end the program, *
  103.   * ANY OTHER INTEGER will continue to run the program. *
  104.   ******************************************************************************/
  105. public static int doAgain()
  106. {
  107. int usersChoice = 0;
  108. System.out.println("Enter '0' to quit, or '1' to run again: ");
  109. usersChoice = input.nextInt();
  110. return usersChoice;
  111. }//End of the getChoice method
  112. /*
  113.   *******************************************************************************
  114.   * verifyDiff: *
  115.   * This method accepts the three variable as arguments. It then compares all* *
  116.   * three to see if any values are the same. If they ARE, the method returns *
  117.   * a false, if all three variable are NOT the same, the method returns true.*
  118.   *******************************************************************************/
  119. public static boolean verifyDiff(int numberOne, int numberTwo, int numberThree)
  120. {
  121. boolean allDiff = false;
  122. if(numberOne != numberTwo && numberOne != numberThree && numberTwo != numberThree)
  123. allDiff = true;
  124. else {
  125. System.out.println("You tried to use a duplicate number, try again: ");
  126. }
  127. return allDiff;
  128. }//End of the getChoice method
  129.  
  130.  
  131. }
Success #stdin #stdout 0.15s 321024KB
stdin
2
3
4
1
6
7
8
0
stdout
Please enter a UNIQUE integer for the program ===>   
Please enter a UNIQUE integer for the program ===>   
Please enter a UNIQUE integer for the program ===>   
The smallest number is: 2
The largest number is: 4
The average is: 3
The product is: 24
The sum is: 9
Enter '0' to quit, or '1' to run again: 
Please enter a UNIQUE integer for the program ===>   
Please enter a UNIQUE integer for the program ===>   
Please enter a UNIQUE integer for the program ===>   
The smallest number is: 6
The largest number is: 8
The average is: 7
The product is: 336
The sum is: 21
Enter '0' to quit, or '1' to run again: