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 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);

{
}

{
System.out.println("What are the answers that the students put");
Correct++;
else
Incorrect++;
}

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

}
}``````

## All 5 Replies

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%.

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();

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;
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();

While(!=Quizzes.length)
{
{
}

{
System.out.println("What are the answers that the students put");
Correct++;
else
Incorrect++;
}

double Percent = Correct / Questions;
System.out.println("There are " +Correct + " correct answers and " +Incorrect
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 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 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;
{
System.out.println("What are the answers that the students put");
Correct++;
else
Incorrect++;
}

double Percent = Correct / Questions;
System.out.println("There are " +Correct + " correct answers and " +Incorrect
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();
}

}
}``````
Be a part of the DaniWeb community

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