Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class MathSequence {
  3. public static Scanner newScanner = new Scanner(System.in);
  4. public static void main(String[] args) {
  5.  
  6.  
  7. Scanner scanner = new Scanner(System.in);
  8. while (true) {
  9. System.out.println(CHOICEQUESTION);
  10. int userResponse = scanner.nextInt();
  11.  
  12. while (!(userResponse == 1 || userResponse == 2 | userResponse == 3)) {
  13.  
  14. System.out.println(ERRORMESSAGE);
  15. System.out.println(CHOICEQUESTION);
  16. scanner.nextLine();
  17. userResponse = scanner.nextInt();
  18.  
  19.  
  20. }
  21. if (userResponse == 1) {
  22.  
  23.  
  24. System.out.println("Enter the first term in the arithmetic sequence: ");
  25. int firstNumber = scanner.nextInt();
  26. System.out.println("Enter the common difference in the arithmetic sequence:");
  27. int commonDiffernce = scanner.nextInt();
  28. System.out.println("Enter the number of terms in the arithmetic sequence: ");
  29. int numTerms = scanner.nextInt();
  30.  
  31.  
  32. int[] array = new int[numTerms];
  33. array[0] = firstNumber;
  34. int sum = array[0];
  35. System.out.print("<" + array[0]);
  36. for (int i = 1; i < numTerms; i++) {
  37.  
  38. array[i] = array[i - 1] + commonDiffernce;
  39. sum = sum + array[i];
  40.  
  41. System.out.print(", " + array[i]);
  42. }
  43. System.out.println(">");
  44.  
  45. System.out.println("Sum of the arithmetic sequence: " + sum);
  46.  
  47. }
  48.  
  49.  
  50. else if (userResponse == 2) {
  51.  
  52.  
  53. System.out.println("Enter the first term in the geometric sequence: ");
  54. int firstNumber = scanner.nextInt();
  55. System.out.println("Enter the common ratio in the geometric sequence:");
  56. int commonRatio = scanner.nextInt();
  57. System.out.println("Enter the number of terms in the geometric sequence: ");
  58. int numTerms = scanner.nextInt();
  59. int[] array = new int[numTerms];
  60. array[0] = firstNumber;
  61. int sum = array[0];
  62. System.out.print("<" + array[0]);
  63. for (int i = 1; i < numTerms; i++) {
  64.  
  65. array[i] = array[i - 1] * commonRatio;
  66. sum = sum + array[i];
  67.  
  68. System.out.print(", " + array[i]);
  69. }
  70. System.out.println(">");
  71.  
  72. System.out.println("Sum of the geometric sequence: " + sum);
  73.  
  74.  
  75. } else if (userResponse == 3) {
  76.  
  77. int[] array = new int[10];
  78.  
  79. int count = 0;
  80.  
  81. while (true) {
  82. System.out.println("Enter a new number: ");
  83.  
  84. array[count++] = scanner.nextInt();
  85.  
  86. if (count == 10) {
  87. System.out.println("You have entered the maximum number of numbers!\n" +
  88. "We will start processing your sequence right away.");
  89. break;
  90.  
  91. } else {
  92. System.out.println("You have entered " + count + " numbers ");
  93. System.out.println("You may still enter another " + (10 - count) + " numbers.");
  94.  
  95. System.out.println("Would you like to enter another new number? (Y/N)");
  96.  
  97. // Scanner.nextline()
  98. String continueGame = newScanner.nextLine();
  99.  
  100. if (!(continueGame.equals("Y"))) {
  101.  
  102. break;
  103.  
  104.  
  105. }
  106. }
  107. }
  108.  
  109. if (count == 1) {
  110.  
  111. System.out.println("Error: we cannot infer the common difference from a sequence of size one.");
  112. } else {
  113. int commonDifference = array[1] - array[0];
  114. boolean isSequence = true;
  115. for (int i = 1; i < count - 1; i++) {
  116.  
  117.  
  118. if (array[i + 1] - array[i] != commonDifference) {
  119.  
  120. isSequence = false;
  121. break;
  122. }
  123.  
  124.  
  125. }
  126.  
  127. if (isSequence) {
  128.  
  129. int sum = array[0];
  130. System.out.print("<" + array[0]);
  131. for (int i = 1; i < count; i++) {
  132.  
  133. sum = sum + array[i];
  134.  
  135. System.out.print(", " + array[i]);
  136. }
  137. System.out.println("> is an arithmetic sequence with\n" +
  138. "first term " + array[0] + " common difference " + commonDifference + " length " + count + " and sum " + sum);
  139.  
  140.  
  141. } else {
  142. System.out.print("<" + array[0]);
  143. for (int i = 1; i < count; i++) {
  144.  
  145.  
  146. System.out.print(", " + array[i]);
  147. }
  148. System.out.println("> is not an arithemtic sequence ");
  149.  
  150. }
  151. }
  152. }
  153.  
  154. System.out.println("Do you want to play again? (Y/N)");
  155. scanner.nextLine();
  156.  
  157. String continueGame = scanner.nextLine();
  158.  
  159. if (!(continueGame.equals("Y"))) {
  160.  
  161. break;
  162.  
  163.  
  164. }
  165.  
  166. }
  167. System.out.println("Bye!");
  168. }
  169.  
  170. private static String CHOICEQUESTION = "What kind of processing task would you like to perform? \n1: Generate an arithmetic sequence. \n2: Generate a geometric sequence. \n3: Determine an arithmetic sequence.";
  171. private static String ERRORMESSAGE = "Error: task number must be 1, 2, or 3.";
  172.  
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement