Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scanner sc = new Scanner(System.in);
- System.out.println("What kind of processing task would you like to perform?");
- System.out.println("1: Generate an arithmetic sequence.");
- System.out.println("2: Generate an geometric sequence.");
- System.out.println("3: Determine an arithmetic sequence.");
- choice = sc.nextInt();
- while (anotherSequence.equals("Y")) {
- // Error prompt
- while (choice <= 0 || choice > 3) {
- System.out.println("Error: task number must be 1, 2, or 3.");
- System.out.println("What kind of processing task would you like to perform?");
- System.out.println("1: Generate an arithmetic sequence.");
- System.out.println("2: Generate an geometric sequence.");
- System.out.println("3: Determine an arithmetic sequence.");
- choice = sc.nextInt();
- }
- if (choice == 1) {
- System.out.println("Enter the first term in the arithmetic sequence: ");
- firstTerm = sc.nextInt();
- System.out.println("Enter the common difference in the arithmetic sequence: ");
- commonDifference = sc.nextInt();
- System.out.println("Enter the number of terms in the arithmetic sequence: ");
- numberOfTerms = sc.nextInt();
- int[] aSequence = new int[numberOfTerms];
- String delString = ", ";
- String newString = null;
- System.out.print("<");
- for (int i = 0; i < numberOfTerms; i++) {
- aSequence[i] = firstTerm;
- firstTerm = firstTerm + commonDifference;
- gResult = aSequence[i] + ", ";
- System.out.print(gResult);
- aSum = aSum + aSequence[i];
- if (i == numberOfTerms) {
- newString = gResult.replace(delString, "");
- System.out.println(">");
- }
- }
- // System.out.println(">");
- System.out.println("Sum of the arithmetic sequence: " + aSum);
- }
- if (choice == 2) {
- System.out.println("Enter the first term in the geometric sequence: ");
- firstTerm = sc.nextInt();
- System.out.println("Enter the common ratio in the geometric sequence: ");
- commonRatio = sc.nextInt();
- System.out.println("Enter the number of terms in the geometric sequence: ");
- numberOfTerms = sc.nextInt();
- int[] gSequence = new int[numberOfTerms];
- for (int i = 0; i < numberOfTerms; i++) {
- gSequence[i] = firstTerm;
- firstTerm = firstTerm * commonRatio;
- System.out.println(gSequence[i] + ", ");
- gSum = gSum + gSequence[i];
- }
- System.out.println("Sum of the geometric sequence" + gSum);
- }
- System.out.println("Bye!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement