forked from erenuygur/EfficientHouseJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2.java
More file actions
70 lines (56 loc) · 2.17 KB
/
Q2.java
File metadata and controls
70 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package homeworks.chapter2;
/*
The video game machines at your local arcade output coupons according to
how well you play the game. You can redeem 10 coupons for a candy bar or 3
coupons for a gumball. You prefer candy bars to gumballs. Write a program that
defines a variable initially assigned to the number of coupons you win. Next,
the program should output how many candy bars and gumballs you can get if
you spend all of your coupons on candy bars first, and any remaining coupons
on gumballs.
*/
public class Q2 {
public static void main(String[] args)
{
java.util.Scanner kb = new java.util.Scanner(System.in);
System.out.print("Coupon count:");
int couponCount = Integer.parseInt(kb.nextLine());
couponStore(couponCount);
}
public static void couponStore(int coupons)
{
int candyCost = 10;
int gumballCost = 3;
int candy;
int gumball;
if (coupons % candyCost == 0) {
System.out.printf("%d = %d Candy%n", coupons, coupons / candyCost);
return;
}
if (coupons % gumballCost == 0) {
System.out.printf("Gumball = %d || Coupon remaining: %d%n", coupons / gumballCost, coupons % gumballCost);
return;
}
candy = coupons / candyCost;
System.out.printf("Candy: %d || ", candy);
coupons %= candyCost;
gumball = coupons / gumballCost;
System.out.printf("Gumball = %d || ", gumball);
coupons %= gumballCost;
System.out.printf("Coupon remaining: %d%n", coupons);
if (candy >= 1 && coupons == 2) {
--candy;
gumball += 4;
coupons -= 2;
System.out.println("============ OR ============");
System.out.printf("Candy: %d || Gumball = %d || Coupon remaining: %d%n", candy, gumball, coupons);
return;
}
if (candy >= 2 && coupons == 1) {
candy -= 2;
gumball += 7;
--coupons;
System.out.println("============ OR ============");
System.out.printf("Candy: %d || Gumball = %d || Coupon remaining: %d%n", candy, gumball, coupons);
}
}
}