# How to Find Percentage of a Quiz On Processing

**URL:** <https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 28, 2019, 3:57pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910 "2019-11-28T15:57:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [November 28, 2019, 3:57pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/1 "2019-11-28T15:57:18Z")

</div>

Hi. I am making a quiz using processing. Our teacher instructed us to create the function findPercent, which will find the percent of the quiz using the variable totalCorrect (represents the answers the user got correct on the quiz) and the total number of questions. Since the teacher is away and very busy, I have no clue what to do.[  
[[https://editor.p5js.org/seerhallow12247/sketches/x7a5hTh5P](https://editor.p5js.org/seerhallow12247/sketches/x7a5hTh5P)]

boolean findPercent(){  
divide =totalCorrect/numberOfquestions;  
percent = ((float)divide);

}  
Sorry if I messed up on the formatting, I am very new to Processing.  
Thanks

---

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [November 28, 2019, 3:59pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/2 "2019-11-28T15:59:20Z")

</div>

The part of the code with the boolean is the part of the code I am having trouble with. Sorry if I have been confusing.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 28, 2019, 5:05pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/3 "2019-11-28T17:05:27Z")

</div>

> [@seerhallow12247](#):
>
> boolean findPercent(){

When the function should **return** a float or integer number (percentage) then return type is float or int. In your case float.

Now the **variable type** before the _function name_ is the **return type**. This is often

- void (this function doesn’t return anything, like `void draw()` for example) or
- here boolean (which returns a boolean, example would be: `boolean isTheMouseOverMyButton() {` ).

**The function findPercent()**

So in the line `boolean findPercent() {`

- the _function name_ is findPercent
- the **return type** is boolean (which is not correct)

So replace boolean with float or int please.

**Remark**

And at the end of the function write  
`return percent;`

Chrisir

---

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [December 2, 2019, 3:12pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/4 "2019-12-02T15:12:32Z")

</div>

When I tried to put float or int , it said unexpected token. What should I do now?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 2, 2019, 3:26pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/5 "2019-12-02T15:26:33Z")

</div>

oops

did i see this wrong?

you link to the p5.js online editor,  
with your sketch ( same user name )  
but the code is JAVA.

-a- where that code come from? your teacher? in form of a file?  
you make that account on p5.js and pasted that code?

-b- you want learn processing?

- JAVA use the PDE [https://processing.org/download/](https://processing.org/download/)

- p5.js write javascript code

* * *

the error when you try to run the code

```auto
SyntaxError: unexpected token: identifier (sketch: line 1)

```

because already the first line is wrong ( language )

sorry that you lost 4 days ? for doing your homework?  
but somehow that mistake slipped even 34 views on this topic??

---

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [December 29, 2019, 4:22am UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/6 "2019-12-29T04:22:24Z")

</div>

Sorry for the late answer. I had no idea that the type of code that was supposed to be put in p5.js was not JAVA. My teacher wrote the first line as part of the example code. Here’s the line:  
`String myText ;`  
`

---

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [December 29, 2019, 4:28am UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/7 "2019-12-29T04:28:45Z")

</div>

The part of the code that I wanted to find the answer was this part:

```auto
else{
   text("You are done!", 50,250,700,250);
   text("You got " + totalCorrect + " correct answers.", 50,300,700,250);
text("You got"+totalCorrect%10, 50,500,700,250);
   
  }
  
  
  //---------------------------------------------------------------------------
}

```

Sorry, I copied and pasted the code not knowing that the editor is coding in Javascript, not JAVA. I am coding in JAVA.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2019, 11:37am UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/8 "2019-12-29T11:37:19Z")

</div>

this is JAVA

```auto

int numOfQuestionsTotal = 12; // number Of Questions in Total (e.g. 12)
int totalCorrect = 4; // number of correct answers (e.g. 9)

void setup() {
  size(300, 300);
  background(0);

  float percent = findPercent( totalCorrect, numOfQuestionsTotal) ;

  println(percent + " %");
}

void draw() {
  //
}

// ----------------------------------------------------------------------------------

float findPercent( int currentValue, int BaseValue ) {

  // The percent value is computed by multiplying the numeric value of the ratio by 100.
  // https://en.wikipedia.org/wiki/Percentage#Calculations

  float ratio = (float) currentValue / (float) BaseValue; 
  float percent = ratio * 100.0; 

  return percent;
}
//

```

---

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [December 31, 2019, 5:43pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/9 "2019-12-31T17:43:32Z")

</div>

Sorry, but I didn’t make it clear that the percentage is calculated based on user input, not based on preprogrammed numbers. These variables:  
`int totalCorrect = 0; int totalWrong = 0;`  
have to start at 0.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 1, 2020, 3:24am UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/10 "2020-01-01T03:24:23Z")

</div>

only with

```auto
int totalCorrect = 0;
int totalWrong = 0;

```

the percent calc would be

```auto
int percent = totalCorrect * 100 / total;

```

and we would assume

```auto
int total = totalCorrect + totalWrong;

```

but when both are 0  
that division does not work.

if you calculate easy way or use @Chrisir good function  
anyhow you need

```auto
numOfQuestionsTotal

```

to do it.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 1, 2020, 9:36am UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/11 "2020-01-01T09:36:48Z")

</div>

> [@seerhallow12247](#):
>
> that the percentage is calculated based on user input, not based on preprogrammed numbers.

I just provide function/ you have to insert and apply to your sketch

---

<div class="post-metadata">

**Author:** ![seerhallow12247](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@seerhallow12247](https://discourse.processing.org/u/seerhallow12247)\
**Post date:** [January 2, 2020, 4:45pm UTC](https://discourse.processing.org/t/how-to-find-percentage-of-a-quiz-on-processing/15910/12 "2020-01-02T16:45:23Z")

</div>

Thank you. I’ll try to figure it out
