Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So, I\'m working on a fairly simple code that I keep running into hurdles with.

ID: 3548342 • Letter: S

Question

So, I'm working on a fairly simple code that I keep running into hurdles with. Im not sure what the hell im doing wrong here but Im running low on time. If anyone could give me a hand, I'd be incredibly greatful!


Here were my instructions:

Have your program pick 1 - 500. Make this variable constant.
Ask what type of quiz they want to take.
-if binary
prompt the number in binary and ask for the number.
Ask if they want to try another.
When no, give them their total score.

-else bin/octal/hex
randomly select bin/oct/hex
prompt number in bin/oct/hex and ask for the number.
Calculate their score when they decline.





package basequiz;

import java.util.Random;
import javax.swing.*;
import java.util.Scanner;
public class BaseQuiz
{
  
    
    
    public static void main(String[] args)
    {
    int rand, correct = 0, failed = 0;
    String data;
    String output = null;
        boolean flag = false;
    //Select Quiz Type Question
    Object[] options = {"Binary Quizzer?",
                    "Bin/Hex/Oct Quizzer?"};
int n = JOptionPane.showOptionDialog(null,
    "Select your trial!",
    "Base QUIIIIIIIIZZZZZZZZZZZERRRRRRRR!",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,     //do not use a custom Icon
    options,  //the titles of buttons
    options[0]); //default button title
       if(n == 0){    
        JOptionPane.showMessageDialog(null, "Welcome to Binary Quizzer!");
            //    int rand = this.Random();

                Random generator = new Random();
                int x = generator.nextInt(500) + 1;
              
                output = Integer.toBinaryString(x);}

                data = JOptionPane.showInputDialog(null, "Convert " + output);
                rand = Integer.parseInt(data);
                if (rand == x){
                JOptionPane.showMessageDialog(null, "Correct!");
        ++correct;
                      JOptionPane.showMessageDialog(null, "Your current score is: " + correct +
                                "right & " + failed + " wrong for a total average of " + correct/(correct+failed) "!");}
                else{ JOptionPane.showMessageDialog(null, "Wrong!");
            ++failed;
            JOptionPane.showMessageDialog(null, "Your current score is: " + correct +
                                "right & " + failed + " wrong for a total average of " + correct/(correct+failed) "!");
        }
        else
        {
        JOptionPane.showMessageDialog(null, "Welcome to Base Quizzer!");
                
                Random generator = new Random();
                int x = generator.nextInt(500) + 1;
                Random alt = new Random();
                int q = alt.nextInt(2);
                if(q == 0){
                output = Integer.toBinaryString(x);}
                else if(q == 1){
                output = Integer.toHexString(x);}
                else if(q == 2){
                output = Integer.toOctalString(x);}
                
                data = JOptionPane.showInputDialog(null, "Convert " + output);
                rand = Integer.parseInt(data);
                if (rand == x){
                JOptionPane.showMessageDialog(null, "Correct!");
                }
        }

    



    }
/*

}




Code on pastebin: http://pastebin.com/bFVBUhWW

Explanation / Answer

The Corrected Code : It is working fine now :)


Code :


import java.util.Random;


import javax.swing.JOptionPane;

public class BaseQuiz

{

public static void main(String[] args){

int rand, correct = 0, failed = 0;

String data;

String output = null;

//Select Quiz Type Question

Object[] options = {"Binary Quizzer?",

"Bin/Hex/Oct Quizzer?"};

int n = JOptionPane.showOptionDialog(null,

"Select your trial!",

"Base QUIIIIIIIIZZZZZZZZZZZERRRRRRRR!",

JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE,

null, //do not use a custom Icon

options, //the titles of buttons

options[0]); //default button title

  

if(n == 0){

JOptionPane.showMessageDialog(null, "Welcome to Binary Quizzer!");

// int rand = this.Random();

Random generator = new Random();

int x = generator.nextInt(500) + 1;

output = Integer.toBinaryString(x);

data = JOptionPane.showInputDialog(null, "Convert " + output);

rand = Integer.parseInt(data);

if (rand == x){

JOptionPane.showMessageDialog(null, "Correct!");

++correct;

JOptionPane.showMessageDialog(null, "Your current score is: " + correct +

"right & " + failed + " wrong for a total average of " + correct/(correct+failed) + "!");}

else{

JOptionPane.showMessageDialog(null, "Wrong!");

++failed;

JOptionPane.showMessageDialog(null, "Your current score is: " + correct +

"right & " + failed + " wrong for a total average of " + correct/(correct+failed) +"!");

}

} else

{

JOptionPane.showMessageDialog(null, "Welcome to Base Quizzer!");

Random generator = new Random();

int x = generator.nextInt(500) + 1;

Random alt = new Random();

int q = alt.nextInt(2);

if(q == 0){

output = Integer.toBinaryString(x);}

else if(q == 1){

output = Integer.toHexString(x);}

else if(q == 2){

output = Integer.toOctalString(x);}

data = JOptionPane.showInputDialog(null, "Convert " + output);

rand = Integer.parseInt(data);

if (rand == x){

JOptionPane.showMessageDialog(null, "Correct!");

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote