Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MathSequence {
- public static Scanner newScanner = new Scanner(System.in);
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- System.out.println(CHOICEQUESTION);
- int userResponse = scanner.nextInt();
- while (!(userResponse == 1 || userResponse == 2 | userResponse == 3)) {
- System.out.println(ERRORMESSAGE);
- System.out.println(CHOICEQUESTION);
- scanner.nextLine();
- userResponse = scanner.nextInt();
- }
- if (userResponse == 1) {
- System.out.println("Enter the first term in the arithmetic sequence: ");
- int firstNumber = scanner.nextInt();
- System.out.println("Enter the common difference in the arithmetic sequence:");
- int commonDiffernce = scanner.nextInt();
- System.out.println("Enter the number of terms in the arithmetic sequence: ");
- int numTerms = scanner.nextInt();
- int[] array = new int[numTerms];
- array[0] = firstNumber;
- int sum = array[0];
- System.out.print("<" + array[0]);
- for (int i = 1; i < numTerms; i++) {
- array[i] = array[i - 1] + commonDiffernce;
- sum = sum + array[i];
- System.out.print(", " + array[i]);
- }
- System.out.println(">");
- System.out.println("Sum of the arithmetic sequence: " + sum);
- }
- else if (userResponse == 2) {
- System.out.println("Enter the first term in the geometric sequence: ");
- int firstNumber = scanner.nextInt();
- System.out.println("Enter the common ratio in the geometric sequence:");
- int commonRatio = scanner.nextInt();
- System.out.println("Enter the number of terms in the geometric sequence: ");
- int numTerms = scanner.nextInt();
- int[] array = new int[numTerms];
- array[0] = firstNumber;
- int sum = array[0];
- System.out.print("<" + array[0]);
- for (int i = 1; i < numTerms; i++) {
- array[i] = array[i - 1] * commonRatio;
- sum = sum + array[i];
- System.out.print(", " + array[i]);
- }
- System.out.println(">");
- System.out.println("Sum of the geometric sequence: " + sum);
- } else if (userResponse == 3) {
- int[] array = new int[10];
- int count = 0;
- while (true) {
- System.out.println("Enter a new number: ");
- array[count++] = scanner.nextInt();
- if (count == 10) {
- System.out.println("You have entered the maximum number of numbers!\n" +
- "We will start processing your sequence right away.");
- break;
- } else {
- System.out.println("You have entered " + count + " numbers ");
- System.out.println("You may still enter another " + (10 - count) + " numbers.");
- System.out.println("Would you like to enter another new number? (Y/N)");
- // Scanner.nextline()
- String continueGame = newScanner.nextLine();
- if (!(continueGame.equals("Y"))) {
- break;
- }
- }
- }
- if (count == 1) {
- System.out.println("Error: we cannot infer the common difference from a sequence of size one.");
- } else {
- int commonDifference = array[1] - array[0];
- boolean isSequence = true;
- for (int i = 1; i < count - 1; i++) {
- if (array[i + 1] - array[i] != commonDifference) {
- isSequence = false;
- break;
- }
- }
- if (isSequence) {
- int sum = array[0];
- System.out.print("<" + array[0]);
- for (int i = 1; i < count; i++) {
- sum = sum + array[i];
- System.out.print(", " + array[i]);
- }
- System.out.println("> is an arithmetic sequence with\n" +
- "first term " + array[0] + " common difference " + commonDifference + " length " + count + " and sum " + sum);
- } else {
- System.out.print("<" + array[0]);
- for (int i = 1; i < count; i++) {
- System.out.print(", " + array[i]);
- }
- System.out.println("> is not an arithemtic sequence ");
- }
- }
- }
- System.out.println("Do you want to play again? (Y/N)");
- scanner.nextLine();
- String continueGame = scanner.nextLine();
- if (!(continueGame.equals("Y"))) {
- break;
- }
- }
- System.out.println("Bye!");
- }
- 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.";
- private static String ERRORMESSAGE = "Error: task number must be 1, 2, or 3.";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement