Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Lopping Problem.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nitrix




PostPosted: Sun Nov 20, 2005 7:20 pm   Post subject: Lopping Problem.

Hello, getting the program to loop is becoming quite a problem, the program does not go back to the beggning once the condition quit is met. I am using standard java.

code:

import java.io.*; //Required for Input

class Quiz
{
        public static void main(String[] args) throws IOException
        {
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
               
                String answer="";
                String quit="";
                double average=0.0;
                double correct=0.0;
                                               
                        while(quit.equals("n"));
                        {
                                System.out.println("Welcome to the Biology Quiz game, you will be asked 10 biology related questions");
                                System.out.println("1. What is a bacteriaphage?");
                                System.out.println("a) Virii that attack and kill bacteria");
                                System.out.println("b) The bacterial DNA stored in the nucleoid");
                                System.out.println("c) an Agent that is designed to attack and break down bacteria");
                                System.out.println("d) The cycle in which  bacteria prepare for binary fission");       
                                answer=stdin.readLine();
                               
                                        if(answer.equals("a"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was a)");
                                        }
                               
                                System.out.println(answer);
                                System.out.println("2. Mendel's Law of Segregation states that");
                                System.out.println("a) Individuals with contrasting traits are crossed, the offspring will express only the dominant trait");
                                System.out.println("b) Genes of contrasting traits will seperate from the chromatid");
                                System.out.println("c) inherited traits are determined by the nucleotide");
                                System.out.println("d) inherited traits are determined by the genes"); 
                                answer=stdin.readLine();

                                        if(answer.equals("d"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was d)");
                                        }

                                System.out.println("3. What are the stages of Mitosis?");
                                System.out.println("a) Prophase,Anaphase,Telophase,Metaphase");
                                System.out.println("b) Prophase,Anaphase,Metaphase,Telophase");
                                System.out.println("c) Prophase,Metaphase,Anaphase,Telophase");
                                System.out.println("d) Prophase,Telophase.Metaphase,Anaphase"); 
                                answer=stdin.readLine();
                                       
                                        if(answer.equals("c"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was c)");
                                        }

                                System.out.println("4. What is a protien?");
                                System.out.println("a) Anabolic Steroid");
                                System.out.println("b) Macromolecule");
                                System.out.println("c) Lipid");
                                System.out.println("d) Saccharide");   
                                answer=stdin.readLine();

                                        if(answer.equals("b"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was b)");
                                        }

                                System.out.println("5. What does a Plantcell have that an Animal cell doesnt?");
                                System.out.println("a) Cell Wall and Chloroplasts");
                                System.out.println("b) Cell Wall,Cholorplasts,and Cytotrides");
                                System.out.println("c) Vacuole,Cell Wall,Chloroplasts");
                                System.out.println("d) Glucogens,Cell Wall,Chloroplasts");     
                                answer=stdin.readLine();

                                        if(answer.equals("a"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was a)");
                                        }

                                System.out.println("6. What are the processes of Mitosis?");
                                System.out.println("a) Prophase,Anaphase,Metaphase,Telophase");
                                System.out.println("b) Prophase,Telophase,Metaphase,Anaphase");
                                System.out.println("c) Prophase,Metaphase,Anaphase,Telophase");
                                System.out.println("d) Metaphase,Prophase,Anaphase,Telophase"); 
                                answer=stdin.readLine();

                                        if(answer.equals("c"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was c)");
                                        }
                                       
                                System.out.println("7. What is the unit used to measure cellular energy?");
                                System.out.println("a) CCP");
                                System.out.println("b) PCTP");
                                System.out.println("c) ACP");
                                System.out.println("d) ATP");   
                                answer=stdin.readLine();

                                                        if(answer.equals("d"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was d)");
                                        }
                                System.out.println("8. What are the products of photosynthesis?");
                                System.out.println("a) Oxygen and Sugar");
                                System.out.println("b) Water, Oxygen, and Sugar");
                                System.out.println("c) Oxygen,Carbon Dioxide");
                                System.out.println("d) Oxygen and Cellulose"); 
                                answer=stdin.readLine();

                                                        if(answer.equals("a"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was a)");
                                        }
                               
                                System.out.println("9. What are two reproduction methods Viruses use to reproduce?");
                                System.out.println("a) Lytic and Lysogenic Cycle");
                                System.out.println("b) Virokinesis and Binary Fission");
                                System.out.println("c) Lytic and and Binary Fission");
                                System.out.println("d) Mitosis and Meiosis");   
                                answer=stdin.readLine();

                                                        if(answer.equals("a"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was a)");
                                        }
                               
                                System.out.println("10. How many chromosomes in a Human somatic cell");
                                System.out.println("a) 23N");
                                System.out.println("b) 46");
                                System.out.println("c) 96");
                                System.out.println("d) 46N");   
                                answer=stdin.readLine();

                                                        if(answer.equals("b"))
                                        {       
                                        correct=correct+1;
                                        }
                                        else
                                        {
                                        System.out.println("Sorry, the correct answer was b)");
                                        }
                               
                                average=(correct)/(10)*(100);
                               
                                System.out.println("You answered:"+correct+" out of 10 correct");
                                System.out.println("Your Score is:"+average+"%");
                                System.out.println("Would you like to quit(y/n)?");
                                quit=stdin.readLine();

                                        if(quit.equals("y"))
                                        {       
                                        correct=0;
                                        average=0.0;
                                        }
                                        else
                                        {
                                        System.out.println("Restarting..");
                                        correct=0;
                                        average=0.0;
                                        }
                        }                            

        }
}
                                       
                               



                                        
Sponsor
Sponsor
Sponsor
sponsor
Nitrix




PostPosted: Sun Nov 20, 2005 9:02 pm   Post subject: (No subject)

anybody at all?
wtd




PostPosted: Sun Nov 20, 2005 9:09 pm   Post subject: (No subject)

http://www.compsci.ca/v2/viewtopic.php?p=98192#98192
DanShadow




PostPosted: Tue Nov 22, 2005 1:54 pm   Post subject: (No subject)

When you say "the beginning", your not being very specific at all... because technically the beginning could be taken as your import statement, right after you declared your class, right in your main void, etc. Your program essentially (as I see it) enters a loop, which will continue while quit="n". If 'y' is not typed at the end, it restarts the score values, and the while loop should restart, if 'y' is typed, the while loop should exit, therefore exitting the program.
Quote:

while(quit.equals("n"));

(Note:Smile If any use presses any button (not just "n"), the program will exit. I believe this is the problem your trying to describe.
The solution is simple.
In the end, where you ask the user whether or not he/she wishes to quite, put THAT in a while statement.
code:

while (!quit.equals("n") && !quite.equals("y")) {

Make it so the user cannot exit the loop, until they have answered whether or not they wish to leave or continue the game. (So, the program requires a 'n' or 'y' to be entered to exit the while loop, and therefore enter their response).
Implement that, and you shouldn't have any problems keeping your loop running as you wish it to.

Hope that helps.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: