Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
<pre>
import java.io.*;
import java.util.*;
public class GameGuess{
	
	int x;
	int ctr;
	public static void main(String [] args){
		new GameGuess();
	}
	public GameGuess(){
		GameGuess1();
	}
	public void GameGuess1(){
		Scanner con = new Scanner(System.in);
		String[] z= new String[5];
		String[] a= new String[5];
		String[] b= new String[5];
		String[] c= new String[5];
		String[] q= new String[5];
		String[] answer= new String[5];
		
		String[] validLetters= new String[3];
		int ctr = 0;
		
		validLetters[0] = "a";
		validLetters[1] = "b";
		validLetters[2] = "c";
		
		//questions
		q[0] = "capital city of philippines?";
		q[1] = "1 + 2?";
		q[2] = "9+1?";
		q[3] = "5+5?";
		q[4] = "3+5?";
		//choices0
		a[0] = "manila";
		b[0] = "davao";
		c[0] = "gensan";
		//choices 1
		a[1] = "3";
		b[1] = "6";
		c[1] = "8";
			//choices 2
		a[2] = "11";
		b[2] = "3";
		c[2] = "11";
			//choices 3
		a[3] = "20";
		b[3] = "10";
		c[3] = "6";
			//choices 4
		a[4] = "3";
		b[4] = "8";
		c[4] = "9";
		
		System.out.println("Quiz Bee");
		for (int x=0; x<10; x++){
			
			try{
			System.out.println(x+1 + " . " + q[x]);
			System.out.println("A." + a[x]);
			System.out.println("B." + b[x]);
			System.out.println("C." + c[x]);
			System.out.println("Enter your answer:");
			String myans =con.next();
			System.out.println(" ");
			
			if(myans.equalsIgnoreCase("a") || myans.equalsIgnoreCase("b") || myans.equalsIgnoreCase("c")){
				if (myans.equalsIgnoreCase(answer[x])){
					
					break;
				}
			}else if(myans.equalsIgnoreCase(" ") || myans.equalsIgnoreCase(" ")){
				throw new BlankAnsException();
			}
			else if (myans.equalsIgnoreCase("0") || myans.equalsIgnoreCase("1") || myans.equalsIgnoreCase("2") || myans.equalsIgnoreCase(".")){
				throw new Exception("number or Special characters are not allowed");
			}
			else{
				throw new InvalidLetterException();
			} 
	      }catch(InvalidLetterException ide){
	      	System.out.println(ide.getMessage());
	      	--x;
	      }
	      catch(BlankAnsException dae){
	      	System.out.println(dae.getMessage());
	      	--x;
	      }
	      catch (Exception e){
	      	System.out.println(e.getMessage());
	      	--x;
	      }
		}
		System.out.println("Your score is:" + ctr + "/10.");
	}public class InvalidLetterException extends Exception{
		public InvalidLetterException(){
			super("An invalid letter.. Enter only A, B and C only.\n");
		}
	}
	public class BlankAnsException extends Exception{
		public BlankAnsException(){
			super("Blank.. You have no Answer!\n");
		}
	}
}


What I have tried:

My code can't stop looping, I can't get the result.
Posted
Updated 24-Apr-21 22:40pm

Your for loop overruns the number of the quiz proposed questions. I've fixed that (and other bugs, adding remarks on modified lines of code). Try
(Please note, using Java facilities, there are far better ways to write such a code).
Java
import java.io.*;
import java.util.*;
public class GameGuess{

  int x;
  int ctr;
  public static void main(String [] args){
    new GameGuess();
  }
  public GameGuess(){
    GameGuess1();
  }
  public void GameGuess1(){
    Scanner con = new Scanner(System.in);
    String[] z= new String[5];
    String[] a= new String[5];
    String[] b= new String[5];
    String[] c= new String[5];
    String[] q= new String[5];
    String[] answer= new String[5];

    String[] validLetters= new String[3];
    int ctr = 0;

    validLetters[0] = "a";
    validLetters[1] = "b";
    validLetters[2] = "c";

    //questions
    q[0] = "capital city of philippines?";
    q[1] = "1 + 2?";
    q[2] = "9+1?";
    q[3] = "5+5?";
    q[4] = "3+5?";
    //choices0
    a[0] = "manila";
    b[0] = "davao";
    c[0] = "gensan";
    answer[0] = "a"; // right answer for quiz 0
    //choices 1
    a[1] = "3";
    b[1] = "6";
    c[1] = "8";
    answer[1] = "a"; // right answer for quiz 1
      //choices 2
    a[2] = "11";
    b[2] = "3";
    c[2] = "10";
    answer[2] = "c"; // right answer for quiz 2, note, there wasn't a valid solution among the proposed answers
      //choices 3
    a[3] = "20";
    b[3] = "10";
    c[3] = "6";
    answer[3] = "b"; // right answer for quiz 3
      //choices 4
    a[4] = "3";
    b[4] = "8";
    c[4] = "9";
    answer[4] = "b"; // right answer for quiz 4

    System.out.println("Quiz Bee");
    for (int x=0; x<5; x++){ // loop limit is the number of quiz questions

      try{
      System.out.println(x+1 + " . " + q[x]);
      System.out.println("A." + a[x]);
      System.out.println("B." + b[x]);
      System.out.println("C." + c[x]);
      System.out.println("Enter your answer:");
      String myans =con.next();
      System.out.println(" ");

      if(myans.equalsIgnoreCase("a") || myans.equalsIgnoreCase("b") || myans.equalsIgnoreCase("c")){
        if (myans.equalsIgnoreCase(answer[x])){
          ctr++; // valid answer, increment the points  
        }
      }else if(myans.equalsIgnoreCase(" ") || myans.equalsIgnoreCase(" ")){
        throw new BlankAnsException();
      }
      else if (myans.equalsIgnoreCase("0") || myans.equalsIgnoreCase("1") || myans.equalsIgnoreCase("2") || myans.equalsIgnoreCase(".")){
        throw new Exception("number or Special characters are not allowed");
      }
      else{
        throw new InvalidLetterException();
      }
        }catch(InvalidLetterException ide){
          System.out.println(ide.getMessage());
          --x;
        }
        catch(BlankAnsException dae){
          System.out.println(dae.getMessage());
          --x;
        }
        catch (Exception e){
          System.out.println(e.getMessage());
          --x;
        }
    }
    System.out.println("Your score is:" + ctr + "/5."); // use the number of quiz questions, here
  }public class InvalidLetterException extends Exception{
    public InvalidLetterException(){
      super("An invalid letter.. Enter only A, B and C only.\n");
    }
  }
 
Share this answer
 
v2
We have no idea what you are entering, so we can't run it under the same conditions you can. That makes working out what went wrong - and you don't really tell us what you have noticed - pretty difficult.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900