Как мне объявить переменную на основе пользовательского ввода? - PullRequest
1 голос
/ 04 октября 2019

Я пытаюсь создать программу, которая проверяет правописание того, что набрал пользователь. Они должны правильно ввести слово «тип» в 10 раз, и появится сообщение о том, сколько времени прошло.
Моя проблемав том, что конец никогда не наступает, он постоянно просит вас ввести «тип». Я считаю, что это из-за 'while (attempts < required)'. Я думаю, что мне нужно сделать обязательное = правильная попытка. if attempt = type, тогда он выдаст 1 правильное значение. Любая помощь приветствуется. также извинения, я не знаю, как структурировать вопрос

    public static void printHeading(String heading) {

        System.out.println(heading.toUpperCase());

        for (int i = 0; i < heading.length(); i++)
            System.out.print("=");
        System.out.println();
        System.out.println();
    }


    public static int runTutorial(Scanner in, String word, int required) {
        Scanner sc = new Scanner(System.in);
        String attempt = word;
        int correct = 0;
        int attempts = 0;

        while (attempts<required) 
        {
            System.out.print("Enter '" + word + "': ");
            attempt = sc.nextLine();

            if(attempt.equals("type")) {

                System.out.println("Correct");
            }   
            else {
                System.out.println("Try again");
            }
        }
        return attempts;

    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int TIMES = 10; //required number of correct repetitions
        final String WORD = "type"; //the word to type
        long startTime, endTime; //start and end time of typing test
        double seconds; //elapsed time in seconds
        int attempts;

        printHeading("Typing Tutor");
        System.out.println("You need to type a word " + TIMES +
                           " times correctly, as quickly as you can");
        System.out.println("Your word today will be '" + WORD + "' (do not enter the quotes)");
        System.out.println();
        System.out.println("Type anything and press enter to begin");

        //The test
        System.out.println("Press enter to start the test");
        sc.nextLine();
        startTime = System.currentTimeMillis();
        attempts = runTutorial(sc, WORD, TIMES);
        endTime = System.currentTimeMillis();

        //Test report
        seconds = (double)(endTime - startTime) / 1000;
        System.out.println("You took " + seconds + " seconds and " + attempts +
                           " attempts to correctly type '" + WORD + "' " + TIMES + " times");
        System.out.println();
        printHeading("Come back for more pracice soon");
    }

}

1 Ответ

0 голосов
/ 04 октября 2019

При System.out.println("Correct"); необходимо увеличить переменную attempts, в противном случае она останется равной 0.

if(attempt.equals("type")) {

    System.out.println("Correct");
    attempts++; // <-- here
} 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...