Can someone please explain why this division is not working. This is very simple division but for some reason it won't work.
double Percent = Correct / Questions;

//****************************************************************
// Quizzes.java
//
// Calculates quizz scores.
// 
//
// ****************************************************************
import java.util.Scanner;

public class Quizzes
{
    public static void main(String[] args)
    {
	final int SALESPEOPLE;
	int sum;
        int Questions;
        int answers;
        int Correct = 0;
        int Incorrect = 0;
        //double Percent;

	Scanner scan = new Scanner(System.in);

        System.out.println("How many questions are in the quizz?");
        Questions = scan.nextInt();
        System.out.println("The number of questions will be " +Questions);

        int[] canswers = new int[Questions];

	for (int i=0; i<canswers.length; i++)
	    {
		System.out.println("Please give the correct answers " + (i+1) + ": ");
		canswers[i] = scan.nextInt();
	    }

        for (int i=0; i<canswers.length; i++)
        {
            System.out.println("What are the answers that the students put");
            answers = scan.nextInt();
            if (answers == canswers[i])
                Correct++;
            else
                Incorrect++;
        }

        double Percent = Correct / Questions;
        System.out.println("There are " +Correct + " correct answers and " +Incorrect
                +" answers");
        System.out.println(Correct);
        System.out.println(Incorrect);
        System.out.println(Questions);
        System.out.println(Percent);
        System.out.println("The percentage correct is" +Percent);



    }
}

You need to cast at least one of:
Correct
Questions

into a double before it will work.

You basically have

int x = 3/4; // for example, since its an int/int
So x is 0.
Then this is cast to a double, to get 0.0.

But doing 3.0 / 4 or 3 / 4.0 will yield the desired result;


I would take it a little further and do this:
int percentage = (int)( (100.0 * correct) / questions + 0.000001 );

The reason I added 0.000001 is java is terrible at math, you may get the answer...
99.99999999999% or 100.000000000001%
So adding a tiny amount on top, then (int)-ing, will give you 100% in this example, instead of rounding down to 99%.

Edited 7 Years Ago by cms271828: n/a

Got the part above. Now I'm having problems with this part.
System.out.println("How many quizzes would you like to grade");
Quizzes = scan.nextInt();

int[] canswers = new int[Questions];

While(!=Quizzes.length)

I would like to grade however many quizzes I input above, so I would think I would need some kind of while loop. What kind of loop would I need to make that work? Here is my full code now.

//****************************************************************
// Quizzes.java
//
// Calculates quizz scores.
// 
//
// ****************************************************************
import java.util.Scanner;

public class Quizzes
{
    public static void main(String[] args)
    {
	final int SALESPEOPLE;
	int sum;
        int Questions;
        int answers;
        double Correct = 0;
        double Incorrect = 0;
        int Quizzes;

	Scanner scan = new Scanner(System.in);

        System.out.println("How many questions are in the quizz?");
        Questions = scan.nextInt();
        System.out.println("The number of questions will be " +Questions);

        System.out.println("How many quizzes would you like to grade");
        Quizzes = scan.nextInt();

        int[] canswers = new int[Questions];
        
        While(!=Quizzes.length)
        {
	for (int i=0; i<canswers.length; i++)
	    {
		System.out.println("Please give the correct answers " + (i+1) + ": ");
		canswers[i] = scan.nextInt();
	    }

        for (int i=0; i<canswers.length; i++)
        {
            System.out.println("What are the answers that the students put");
            answers = scan.nextInt();
            if (answers == canswers[i])
                Correct++;
            else
                Incorrect++;
        }

        double Percent = Correct / Questions;
        System.out.println("There are " +Correct + " correct answers and " +Incorrect
                +" answers");
        System.out.println(Correct);
        System.out.println(Incorrect);
        System.out.println(Questions);
        System.out.println(Percent);
        System.out.println("The percentage correct is " +Percent*100 +"%");



        }
    }
}

I think you would need a for loop
That performs quizzes.length loops

int num = quizzes.length;
for(int quiz=1;quiz<=num;quiz++)
{
...
}

And then quiz is your quiz number which you can use inside loop if you want, to display the number of the quiz.

I think you would need a for loop
That performs quizzes.length loops

int num = quizzes.length;
for(int quiz=1;quiz<=num;quiz++)
{
...
}

And then quiz is your quiz number which you can use inside loop if you want, to display the number of the quiz.

I can't get that to work no matter how I try it :(.

I figured out the problem.
double Correct = 0;
double Incorrect = 0;
On that part I needed to move it down farther in the code so it reinitialized to 0 to reset the count.

Here's the full working code if anyone is curious.

//****************************************************************
// Quizzes.java
//
// Calculates quizz scores.
// 
//
// ****************************************************************
import java.util.Scanner;

public class Quizzes
{
    public static void main(String[] args)
    {
	final int SALESPEOPLE;
	int sum;
        int Questions;
        int answers;
        int Quizzes;
        String phrase= "";    // a string of characters

	Scanner scan = new Scanner(System.in);

        System.out.println("How many questions are in the quizz?");
        Questions = scan.nextInt();
        System.out.println("The number of questions will be " +Questions);

        //System.out.println("How many quizzes would you like to grade");
        //Quizzes = scan.nextInt();

        int[] canswers = new int[Questions];
        for (int i=0; i<canswers.length; i++)
	    {
		System.out.println("Please give the correct answers " + (i+1) + ": ");
		canswers[i] = scan.nextInt();
	    }


        //int num = Quizzes.length;
        //for(int quiz=1;quiz<=num;quiz++)
        //while(!Quizzes)
        while (!phrase.equals("n"))
        {
        //for(int quiz=1;quiz<=Quizzes;quiz++)
        //{

            double Correct = 0;
            double Incorrect = 0;
            for (int i=0; i<canswers.length; i++)
            {
            System.out.println("What are the answers that the students put");
            answers = scan.nextInt();
            if (answers == canswers[i])
                Correct++;
            else
                Incorrect++;
            }

            double Percent = Correct / Questions;
            System.out.println("There are " +Correct + " correct answers and " +Incorrect
                +" answers");
            System.out.println(Correct);
            System.out.println(Incorrect);
            System.out.println(Questions);
            System.out.println(Percent);
            System.out.println("The percentage correct is " +Percent*100 +"%");
            phrase = scan.nextLine();
            System.out.println("Would you like to grade another quiz y/n");
            phrase = scan.nextLine();
        }


    }
}
This article has been dead for over six months. Start a new discussion instead.