the program asks the user to input the number of users who wants to play.
i couldn't get the result of players.length individually. each players result should be number of correct answers from the number of questions asked. please help!.

import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.*;


public class coll
{
    static int cr, choice, answer, xAnswer;
    static double count, players;

    public static void main (String[] args)
    {
        
       String strUsers = JOptionPane.showInputDialog("How many players?");
       int num = Integer.parseInt(strUsers);
       int players[] = new int[num];
    
           
       
           
       do
       {
           for (int index = 0; index < num; index++)
           {
           boolean invalidNumber = true;
           
               while (invalidNumber)
               {
               
                
           
                String menu = JOptionPane.showInputDialog("Player " +(index+1) +
                                            "\n1.Practice addition" +
                                            "\n2.Practice subtraction" +
                                            "\n3.Practice multiplication" +
                                            "\n4.Quit the program" +
                                            "\n " +
                                            "\n " +
                                            "Enter your choice");
                      try
                      {
                          choice = Integer.parseInt(menu);
                          invalidNumber = false;
                      }
                      catch (NumberFormatException e)
                      {
                      }
               }
                        
                        

                                                       
            Random randomNumbers = new Random();
            int number1 = randomNumbers.nextInt(9);
            int number2 = randomNumbers.nextInt(9);
            
            switch(choice)
            {
               case 1 : add (number1, number2);
                        count++;
                        break;
               case 2 : deduct (number1, number2);
                        count++;
                        break;
               case 3 : multiply (number1, number2);
                        count++;
                        break;
               case 4:
                {

                    for ( index = 0; index < players.length; index++)
                    {
                    double total = cr/count*100;
                    DecimalFormat newFormat = new DecimalFormat("0");
                    JOptionPane.showMessageDialog(null, "Player " + players[index] + " got " + 
                                newFormat.format(total) + "% right!!");
                    System.exit(0);
                }
                }
                break;
            
            } 
         
           }
       }
       while(choice != 4);
                
    
}
    

        
    public static void add (int number1, int number2)
    {
        boolean invalidNumber = true;
             while (invalidNumber)
             {
        
                  String str1 = JOptionPane.showInputDialog("what is "  + number1 + 
                                            " plus " + number2 + "?");
                            
                        try
                        {      
                            answer = Integer.parseInt(str1);
                            invalidNumber = false;
                        }
                        catch (NumberFormatException e)
                        {
                            
                        }
                        
              }
              
                         xAnswer = number1 + number2;
                         
                         if (answer == xAnswer)
                         correctAnswer();
                         else
                         incorrectAnswer();
                    
                    
                
    }
                    
    public static void deduct (int number1, int number2)
    {
        boolean invalidNumber = true;
                    
        int x, y;
        if (number1 > number2)
                        x = number1;
                    else
                        x = number2;
                    if (number1 < number2)
                        y = number1;
                    else
                        y = number2;
                        
            while (invalidNumber)
            {            
                                
                  String str2 = JOptionPane.showInputDialog("what is "  + x + 
                                            " minus " + y + "?");
                    
                        try
                        {      
                            answer = Integer.parseInt(str2);
                            invalidNumber = false;
                        }
                        catch (NumberFormatException e)
                        {
                            
                        }
                        
            }
            
                         xAnswer = x - y;
                        
                        
                         if (answer == xAnswer)
                         correctAnswer();
                         else
                         incorrectAnswer();
                     
    }
    
    
    
    public static void multiply(int number1, int number2)
    {
        boolean invalidNumber = true;

            while (invalidNumber)
            {  
        
                  String str3 = JOptionPane.showInputDialog("what is "  + number1 + 
                                            " times " + number2 + "?");
                                            
                                            
                        try
                        {      
                            answer = Integer.parseInt(str3);
                            invalidNumber = false;
                        }
                        catch (NumberFormatException e)
                        {
                            
                        }
                        
            }                 
            
                         xAnswer = (number1 * number2);
                        
                        
                         if (answer == xAnswer)
                         correctAnswer();
                         else
                         incorrectAnswer();
                     
                     
    } 
    
    public static void correctAnswer()
    {
        cr++;   
    String remarks[] = {"Very good!", "Excellent!", "You are hot!", "Keep it up!"};
            
            Random rem = new Random();
            int rnd = rem.nextInt(remarks.length);
                       
            JOptionPane.showMessageDialog(null, remarks[rnd]);           
                    
            
    }
    
    
    public static void incorrectAnswer()
    {
        
        
    String remarks[] = {"Wrong answer!", "Try again!", "Incorrect!", "Work harder!!"};
        

            Random rem = new Random();
            int rnd = rem.nextInt(remarks.length);
                        
            JOptionPane.showMessageDialog(null, remarks[rnd]);
    }
}

Recommended Answers

All 5 Replies

not urgent

ok, not urgent.. can you please help then?

no doubt it's too late now.

you seriously feel like being mean? look jwenting... if u arnt willing to help!! just dont place ur head in here

solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.