Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. Scanner sc = new Scanner(System.in);
  2. System.out.println("What kind of processing task would you like to perform?");
  3. System.out.println("1: Generate an arithmetic sequence.");
  4. System.out.println("2: Generate an geometric sequence.");
  5. System.out.println("3: Determine an arithmetic sequence.");
  6. choice = sc.nextInt();
  7.  
  8. while (anotherSequence.equals("Y")) {
  9.  
  10. // Error prompt
  11. while (choice <= 0 || choice > 3) {
  12. System.out.println("Error: task number must be 1, 2, or 3.");
  13. System.out.println("What kind of processing task would you like to perform?");
  14. System.out.println("1: Generate an arithmetic sequence.");
  15. System.out.println("2: Generate an geometric sequence.");
  16. System.out.println("3: Determine an arithmetic sequence.");
  17. choice = sc.nextInt();
  18. }
  19.  
  20. if (choice == 1) {
  21. System.out.println("Enter the first term in the arithmetic sequence: ");
  22. firstTerm = sc.nextInt();
  23. System.out.println("Enter the common difference in the arithmetic sequence: ");
  24. commonDifference = sc.nextInt();
  25. System.out.println("Enter the number of terms in the arithmetic sequence: ");
  26. numberOfTerms = sc.nextInt();
  27. int[] aSequence = new int[numberOfTerms];
  28. String delString = ", ";
  29. String newString = null;
  30. System.out.print("<");
  31. for (int i = 0; i < numberOfTerms; i++) {
  32. aSequence[i] = firstTerm;
  33. firstTerm = firstTerm + commonDifference;
  34. gResult = aSequence[i] + ", ";
  35. System.out.print(gResult);
  36. aSum = aSum + aSequence[i];
  37.  
  38. if (i == numberOfTerms) {
  39. newString = gResult.replace(delString, "");
  40. System.out.println(">");
  41. }
  42. }
  43. // System.out.println(">");
  44. System.out.println("Sum of the arithmetic sequence: " + aSum);
  45. }
  46.  
  47. if (choice == 2) {
  48. System.out.println("Enter the first term in the geometric sequence: ");
  49. firstTerm = sc.nextInt();
  50. System.out.println("Enter the common ratio in the geometric sequence: ");
  51. commonRatio = sc.nextInt();
  52. System.out.println("Enter the number of terms in the geometric sequence: ");
  53. numberOfTerms = sc.nextInt();
  54. int[] gSequence = new int[numberOfTerms];
  55. for (int i = 0; i < numberOfTerms; i++) {
  56. gSequence[i] = firstTerm;
  57. firstTerm = firstTerm * commonRatio;
  58. System.out.println(gSequence[i] + ", ");
  59. gSum = gSum + gSequence[i];
  60. }
  61. System.out.println("Sum of the geometric sequence" + gSum);
  62. }
  63.  
  64.  
  65.  
  66. System.out.println("Bye!");
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement