In: Computer Science
When calories are 1200 through 1800: “Diet is on target.”
When calories are 2000 through 2550: “Calorie intake ok if active.”
When calories are any other value: “Calorie intake is either insufficient or too much!”
while(cruise)
{
System.out.printf(“%nChoose a number from 1 through 4 to find out “
+ “which cruise you have won: “);
choice = input.nextInt();
if(choice == 1)
{
destination = “Bahamas”;
}
else
{ if(choice == 2)
{
destination = “British Isles”;
}
else
{ if(choice == 3)
{
destination = “Far East”;
}
else
{ if(choice == 4)
{
destination = “Amazon River”;
}
else
{
System.out.printf(“%nInvalid choice! Enter “
+ “5 to continue or 0 to exit: ”);
choice = input.nextInt();
}//END if choice = 4 else NOT = 4
}//END if choice = 3 else NOT = 3
}//END if choice = 2 else NOT = 2
}//END if choice = 1 else NOT = 1
if(choice >= 0 && choice < 5)
{
cruise = false;
}//END if choice from 1-4
}//END while cruise is true
System.out.printf(“%nYou have won a cruise to the %s!”, destination);
please check out the solution... please do a comment for any doubt... thank you...
1.a> Shares will be awarded if the employee works for >=30 years...
using ternary operator, it can be written as ....
preferredShares = (yearsEmployed>=30)? 100: 0;
1.b>
do{
System.out.println("GOTTA FIND MY CAR KEYS!"); //printing the given massage
foundKeys=scnr.nex().charAt(0); //taking input for foundKeys, here scnr is the object of Scanner class
}while(foundKeys!='n'); //loop will continue until foundKeys not-equals to n
1.c>
while(foundKeys != 'n'){ //i.e. until key is found
System.out.println("GOTTA FIND MY CAR KEYS!"); //printing the given massage
foundKeys=scnr.next().charAt(0); //taking input for foundKeys, here scnr is the object of Scanner class
} //end of while
1.d>
in this case, there is a problem... for switch case calories is divided by 100. so the number of digits in calorie decreases by 2 digits. now if the input is 2560 calories... it should come under "Calorie intake is either insufficient or too much!" case... but after divided by 100, new calories becomes 25.... in switch case it comes under “Calorie intake ok if active.” case... this condition happens because switch cannot use float value...
switch(calories/100){ // calories is divided by 100
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
System.out.println("Diet is on target.");
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
System.out.println("Calorie intake ok if active.");
break;
default:
System.out.println("Calorie intake is either insufficient or too much!");
}//end of switch
1.e>
if(calories>=1200 && calories<==1800) //When calories are 1200 through 1800
System.out.println("Diet is on target.");
else if(calories>=2000 && calories<==2550) //When calories are 2000 through 2550
System.out.println("Calorie intake ok if active.");
else //When calories are any other value
System.out.println("Calorie intake is either insufficient or too much!");
1.f>
for(int i=1;i<=3;i++){ //loop will continue for 3 times
if(i==3) //if it is the last attempt
System.out.print("No more attempts left!");
calories=scnr.nextInt(); // taking input... scnr is the object of scanner class
switch(calories/100){ // calories is divided by 100
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
System.out.println("Diet is on target.");
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
System.out.println("Calorie intake ok if active.");
break;
default:
System.out.println("Calorie intake is either insufficient or too much!");
}//end of switch
}//end of for
1.g> updated the given code to do while
Note: in the print statement, there was "%n", it will be "\n" i.e newline... which is been updated...
do //updated to do while loop
{
System.out.printf("\nChoose a number from 1 through 4 to find out "
+ "which cruise you have won: ");
choice = input.nextInt();
if(choice == 1)
{
destination = "Bahamas";
}
else
{ if(choice == 2)
{
destination = "British Isles";
}
else
{ if(choice == 3)
{
destination = "Far East";
}
else
{ if(choice == 4)
{
destination = "Amazon River";
}
else
{
System.out.printf("\nInvalid choice! Enter "
+ "5 to continue or 0 to exit: ");
choice = input.nextInt();
}//END if choice = 4 else NOT = 4
}//END if choice = 3 else NOT = 3
}//END if choice = 2 else NOT = 2
}//END if choice = 1 else NOT = 1
if(choice >= 0 && choice < 5)
{
cruise = false;
}//END if choice from 1-4
}while(cruise); //END do-while if cruise is true
System.out.printf("\nYou have won a cruise to the %s!", destination);