Dragon4c3_
Dragon4c3_

Reputation: 57

How to i make it so that every time it reaches 10 it plays a sound, so 10, 20, 30, 40

if (score == 10) {
    Sound.soundScore1();

I am a newbie programmer, so if this is a stupid question, sorry but if you don't ask you don't know. What i want the code to do is when you reach the score of 10, 20, 30, 40... it plays a different sound to the regular ping noise. That there is my current code, is there any way of doing what i said??

Upvotes: 2

Views: 1205

Answers (3)

Angelo Tricarico
Angelo Tricarico

Reputation: 1363

Although modulus % solution is fine, it costs a lot. It could be faster to check if the last digit is a 0. An example below. You can avoid creating a boolean variable. Also if you already have a string, you can avoid first conversion from int to String (this often happens in videogames world when you want to display a score on some GUI labels).

int iNumber = 665;
String sNumber = String.valueOf(iNumber);
boolean bResult = (sNumber.substring(sNumber.length() - 1)).equals("0");
System.out.println(bResult);

Upvotes: 1

Keshav Pradeep Ramanath
Keshav Pradeep Ramanath

Reputation: 1687

The Remainder operator(%) is the appropriate way to carry out the functionality. It is also called the Modulo operator.

It is a binary operator.

Please find the below link for more information.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

The below code would help you achieve it.

if(score%10 == 0){
Sound.soundScore1();
}

Upvotes: 2

Zilicon
Zilicon

Reputation: 3860

if (score % 10 == 0) {
    Sound.soundScore1();
}

meaning, if the remainder of the division by 10 (aka modulus operator %) is 0.

Upvotes: 7

Related Questions