Created
November 18, 2019 09:06
-
-
Save 51enra/d36a1073b55d6d1a89528fd5eeafb440 to your computer and use it in GitHub Desktop.
Java Quest 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CandyCount { | |
/* | |
Translate the following instructions in compilable Java code: | |
real money ← 12.4; | |
real price ← 1.2 | |
candies integer ← 0 | |
If money > 0 AND price > 0 | |
As long as (money - price) >= 0 | |
candies ← candies + 1 | |
money ← money - price | |
End As long as | |
End If | |
display candies | |
*/ | |
public static void main(String[] args) { | |
// Counts number of candies that can be bought, given price and money | |
double money = 12.4; | |
double price = 1.2; | |
int candies = 0; | |
if ((money > 0) && (price > 0)) { | |
while ((money - price) >= 0) { | |
candies += 1; | |
money -= price; | |
} | |
} | |
System.out.println(candies); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment