Need help on this. Java 1- introduction. Will leave great rating. Code a while l
ID: 3799093 • Letter: N
Question
Need help on this. Java 1- introduction. Will leave great rating.
Code a while loop with if/else structures that determine an anniversary gift based on the number of years married.
i.Declare a loop-control variable initialized to ‘Y’.
ii.Declare a String variable that stores the gift.
iii.Declare an integer variable that stores the choice for the anniversary year.
iv.Code the while where you test the loop-control variable to ‘Y’ using Character.toUpperCase().
Have the user choose from the prompt:
ANNIVERSARY YEAR
1.50
2.55
3.60
4.None of the above.
Select the anniversary year:
Based on the yearChoice print “The anniversary gift is xxxxxxxxxx.” where the x’s represent the anniversary gift.
For yearChoice 1 or 50th anniversary, the gift is gold.
yearChoice 2 the gift is emerald.
yearChoice 3 the gift is diamond.
yearChoice 4 display this statement: go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.
Prompt to re-enter the loop or exit.
Refer to sample output below.
v.Assume the input variable for the Scanner class is already declared.
i.****SAMPLE OUTPUT*****
ANNIVERSARY YEAR
1. 50
2. 55
3. 60
4. None of the above.
Select the anniversary year: 1
The anniversary gift is gold.
Search for another anniversary gift? Enter 'Y' or 'N': y
ANNIVERSARY YEAR
1. 50
2. 55
3. 60
4. None of the above.
Select the anniversary year: 2
The anniversary gift is emerald.
Search for another anniversary gift? Enter 'Y' or 'N': y
ANNIVERSARY YEAR
1. 50
2. 55
3. 60
4. None of the above.
Select the anniversary year: 3
The anniversary gift is diamond.
Search for another anniversary gift? Enter 'Y' or 'N': y
ANNIVERSARY YEAR
1. 50
2. 55
3. 60
4. None of the above.
Select the anniversary year: 4
The anniversary gift is go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.
Search for another anniversary gift? Enter 'Y' or 'N': n
Explanation / Answer
YearChoiceTest.java
import java.util.Scanner;
public class YearChoiceTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("ANNIVERSARY YEAR");
int yearChoice ;
char ch = 'Y';
while(Character.toUpperCase(ch) == 'Y'){
System.out.println("1. 50 2. 55 3. 60 4. None of the above Select the anniversary year: ");
yearChoice = scan.nextInt();
if(yearChoice ==1){
System.out.println("The anniversary gift is gold.");
}
else if(yearChoice ==2){
System.out.println("The anniversary gift is emerald.");
}
else if(yearChoice ==3){
System.out.println("The anniversary gift is diamond.");
}
else{
System.out.println("The anniversary gift is go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");
}
System.out.println("Search for another anniversary gift? Enter 'Y' or 'N': ");
ch = scan.next().charAt(0);
}
}
}
Output:
ANNIVERSARY YEAR
1. 50
2. 55
3. 60
4. None of the above
Select the anniversary year:
1
The anniversary gift is gold.
Search for another anniversary gift? Enter 'Y' or 'N':
y
1. 50
2. 55
3. 60
4. None of the above
Select the anniversary year:
2
The anniversary gift is emerald.
Search for another anniversary gift? Enter 'Y' or 'N':
y
1. 50
2. 55
3. 60
4. None of the above
Select the anniversary year:
3
The anniversary gift is diamond.
Search for another anniversary gift? Enter 'Y' or 'N':
y
1. 50
2. 55
3. 60
4. None of the above
Select the anniversary year:
4
The anniversary gift is go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.
Search for another anniversary gift? Enter 'Y' or 'N':
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.