Сравнение двух строк по одному символу за раз в Java - PullRequest
0 голосов
/ 13 октября 2019

Я пытаюсь сравнить две разные строки, по одному символу за раз. Мне нужно вернуть правильное количество цифр, пока они больше не будут равны друг другу. Тем не менее, я не могу включить символ «.»в ответном заявлении. Как бы я поступил так?

import java.util.Scanner;
import java.math.BigDecimal;

public class PiEstimate {


    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String a;
        String b;
        char y;
        char c;
        char d;
        String userInput;

        do {

            System.out.print("Enter a number of randomly generated points:");
            userInput = in.nextLine();


            if (!isValid(userInput)) {
                System.out.print("\n" + "You entered an invalid integer. Please enter a valid integer greater than 0: ");
                userInput = in.nextLine();
                BigDecimal estimate = new BigDecimal((Math.PI / 4) * 4);
                estimate.toString();
                System.out.println("\n" + "Your estimate is: " + calculation(userInput));
                System.out.println("\n" + "Accuracy of digits is :" + comparison(estimate.toString(),userInput));
            } else {
                BigDecimal estimate = new BigDecimal((Math.PI / 4) * 4);
                estimate.toString();
                System.out.println("\n" + "Your estimate is: " + calculation(userInput));
                System.out.println("\n" + "Accuracy of digits is :" + comparison(estimate.toString(),userInput));
            }


            System.out.println("\n" + "Would you like to play again? Enter 'Y' for yes or 'N' for no: ");
            String optionToPlay = in.nextLine();
            c = optionToPlay.charAt(0);
            d = Character.toUpperCase(c);
                if (d == 'n' || d == 'N') {
                    BigDecimal estimate2= new BigDecimal( (Math.PI / 4) * 4);
                    System.out.println("\n" + "The best estimate is: " + estimate2);
                }


            } while (d == 'Y');


    } // end psvm


    public static boolean isValid(String a) {
        boolean isFlag = true;
        char holder;

        for (int i = 0; i < a.length(); i++) {
            holder = a.charAt(i);
            if (!Character.isDigit(a.charAt(i))) {
                return false;
            } if (i == 0 && holder == '-') {
                return false;
            }

        } // end for
        return isFlag;
    } // end isValid

    public static double calculation(String a) { // String a means 'looking for a string
        double calc = Double.parseDouble(a);
        int i;
        double x;
        double y;
        double c = 0;
        double runningCounter = 0;
        double totalCounter;

        for (i = 0; i < calc; i++) {
            x = Math.random();
            y = Math.random();
            c = Math.sqrt((x * x) + (y * y));

            if (c <= 1) {
                runningCounter++;
            }

        } // end for
        totalCounter =  ((runningCounter / calc) * 4);
        calc = totalCounter;
        return calc;
    }

    public static int comparison (String bear, String userInput) {
        int i = 0;
        String s = calculation(userInput) + "";
        int b;
        int counter2 = 0;


        for (i=0; i < s.length(); i++) {
            if (s.charAt(i) != bear.charAt(i)) {
                return i;
            }
        }

       return i;


    } // end comparison
} // end class

Код из IDE

...