samedi 17 mars 2018

Conditional While-Loop program wont end on command

The program I'm trying to make is supposed to act as a test, where users give their name to the program, and then choose a letter (A through F) for each two 'questions', where each letter has a different value. The user is then given a 'grade' that depends on the value of the letter choices. The program will loop until the user chooses to quit and end the program.
If the user gives an invalid input, (any other character or letter not A-F), the program will loop, asking for valid input until it's given. After the program runs all the way through, and the user gets their results, the user clears the screen by hitting enter and the program starts again from the beginning.

Here's the code I have so far:

import java.util.Scanner;
import java.lang.*;
import java.io.IOException;
/**
 * Write a description of class CNFusingExam here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
  public class CNFusingExam
{
    public static void main (String[] args) throws IOException
    {
        String userName, choiceOne, choiceTwo, quizGrade;  

        int questionOne = 1, questionTwo = 1;

        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter your name. (q to Quit):" );
        userName = scan.nextLine();

        if ((!userName.equals("q")))
        {
            System.out.println("Please enter two letters for questions one and two, with a space between each letter (Choices A, B, C, D, E, F): " );
            choiceOne = scan.next();
            choiceTwo = scan.next();

        while ((!choiceOne.equals("a") && !choiceOne.equals("b") && !choiceOne.equals("c") && !choiceOne.equals("d") && !choiceOne.equals("e") && !choiceOne.equals("f"))  || (!choiceTwo.equals("a") && !choiceTwo.equals("b") && !choiceTwo.equals("c") && !choiceTwo.equals("d") && !choiceTwo.equals("e") && !choiceTwo.equals("f")))
        {
            System.out.println("Please enter two valid inputs");
            System.out.println("Please enter two letters for questions one and two, with a space between each letter (Choices A, B, C, D, E, F): " );
            choiceOne = scan.next();
            choiceTwo = scan.next();
        }

if (choiceOne.equals("a"))
    questionOne = 8;
else if (choiceOne.equals("b"))
    questionOne = 5;
else if (choiceOne.equals("c"))
    questionOne = 5;
else if (choiceOne.equals("d"))
    questionOne = 10;
else if (choiceOne.equals("e"))
    questionOne = 7;


else  {
    questionOne = 0;
}

    switch (choiceTwo)
{   
case "a":
    questionTwo = 5;
    break;
case "b":
    questionTwo = 5;
    break;
case "c":
    questionTwo = 7;
    break;
case "d":
    questionTwo = 8;
    break;
case "e":
    questionTwo = 10;
    break;
default:
    questionTwo = 0;
    break;
    }
int pointTotal = questionOne + questionTwo; 
        if ((pointTotal == 20) && (pointTotal >= 18)) 
            quizGrade = "A";
        else if ((pointTotal <= 17) && (pointTotal >= 16)) 
            quizGrade = "B";
        else if ((pointTotal <= 15) && (pointTotal >= 14)) 
            quizGrade = "C";
    else if ((pointTotal <= 13) && (pointTotal >= 10))
        quizGrade = "D";
    else {
        quizGrade = "F";
}

System.out.println("Your name is " + userName + " your choices are " + choiceOne.toUpperCase() + " and " + choiceTwo.toUpperCase() + " your total is " + pointTotal + " and your grade is " + quizGrade);
System.out.print("Press Enter key to continue: ");
char c = (char) System.in.read(); 
System.out.print("\f");       
    }
        else {
            System.out.println("Program Ended");
            return;
        }

    }
}

The program works fine as is, where the user will input the two correct letters, will be prompted for again for two new ones if they aren't the letters a-f, the value of their choices will be calculated, and the user will be given a grade in the end. The program also quits if the user inputs 'q' instead of their name too. Now It's getting the entire code to loop again after it finishes. If I put the while-loop after where the user inputs their name (userName = scan.Next();), the program will loop, but it won't show the prompt for the username again or the choice to quit anymore. It'll begin at the prompt where it says: Please print out two letters. How and where do I put the while and break parts of the code so it will function properly?

Also, neither while (true) or do-while worked either when I tried it. It makes it so the program won't quit on command anymore.

Aucun commentaire:

Enregistrer un commentaire