lundi 19 mars 2018

Java math x+y=z game

Hi I am currently trying to make a game where you will have a formula of linear form. Ex: 2+5 = ? or 2*6 = ? or 6-2= ? The user will have shown 4 answers and he has to choose the good one. All the numbers and operators are chosen randomly and it gives to the user how many on /15 he guessed. I will show what I did so far, but I have some problems and need help to fix them: 1)in my AllQuestion I wanna make sure that the good answer is always the D answers which is the good one but shuffled in the code. 2) I need to be able to run this in console mode but I have no idea how. 3) when I have negative answer (z), I have exception error saying:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Random.java:388) at projetfinal.Reponse.(Reponse.java:33) at projetfinal.ProjetFinal.main(ProjetFinal.java:19) C:\Users\Ali Alp\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

Here's the code I did so far, this one is to generate the formula to ask:

public class Equation {

    int x, y, z;

    //x,y,z sont ce qui permettent de faire la formule a poser
    public Equation() {
        //la classe qui crée la formule linéaire avec x,y,z et qui génère l'opérateur au hasard
        Random r = new Random();
        x = r.nextInt(50) + 1;
        y = r.nextInt(50) + 1;
        z = 0;
        char operator = '?';

        switch (r.nextInt(3)) {
            case 0:
                operator = '+';
                z = x + y;
                break;
            case 1:
                operator = '-';
                z = x - y;
                ;
                break;
            case 2:
                operator = '*';
                z = x * y;
                ;
                break;
            default:
                operator = '?';
        }

        System.out.print(x);
        System.out.print(" ");
        System.out.print(operator);
        System.out.print(" ");
        System.out.print(y);
        System.out.print(" = ");
        System.out.println("?");
        //System.out.println(z);
    }

}

This one is to generate the 4 answers and also calculate how many good answers the person got:

    public class Reponse {
    int a, b, c, d;
    char operator = '?';



    public Reponse(Equation equ) {
        Random r = new Random();
        switch (r.nextInt(4)) {

            case 0:
                a = r.nextInt(2 * equ.z);
                break;

            case 1:
                b = r.nextInt(2 * equ.z);
                break;

            case 2:
                c = r.nextInt(2 * equ.z);
                break;
            case 3:
                d = equ.z;
                break;
            default:
                operator = '?';
        }
    }
}
class Question {

    String question;
    String correctOption;
    //int correct = 0;

    public Question(String question, String correctOption) {
        this.question = question;
        this.correctOption = correctOption;


    }

}

class AllQuestions {
    List<Question> allQuestions = new ArrayList<Question>();
         public AllQuestions(){   
             String a,b,c,d = null;
             int correct = 0;
             Scanner input = new Scanner(System.in);

             allQuestions.add(new Question("Trouvez la bonne reponse" +"\nA-"+ "\nB-"+ "\nC-"+ "\nD-" ,d));
            Collections.shuffle(allQuestions); 

            for (int i=0; i < allQuestions.size(); i++) {
            Question curQuest = allQuestions.get(i);
            System.out.println(curQuest.question);
            String ans = input.next();
            if(ans.equalsIgnoreCase(curQuest.correctOption)) {
                System.out.println("Correct");
                correct++;
            } else {
                System.out.println("incorrect");
            }
            }
            System.out.println("Vous avez "+ correct++ +"/15"); 
        }

}




Aucun commentaire:

Enregistrer un commentaire