Задача: Sushi Time
Здравейте,
Judge ми дава 0/100
Резултата ми винаги е "Sushi Zones is invalid restaurant!"
не мога да разбира къде бъркам.
Ако някои помогне, бих била много благодарна.
import java.util.Scanner;
public class SeaTrip {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String sushiType = scanner.nextLine();
String restorant = scanner.nextLine();
int portions = Integer.parseInt(scanner.nextLine());
String online = scanner.nextLine();
double total = 0;
double price = 0;
switch (restorant)
{
case "Sushi Zone":
switch (sushiType)
{
case "sashimi":
price = 4.99;
break;
case "maki":
price = 5.29;
break;
case "uramaki":
price = 5.99;
break;
case "temaki":
price = 4.29;
break;
default:
break;
}
break;
case "Sushi Time":
switch (sushiType)
{
case "sashimi":
price = 5.49;
break;
case "maki":
price = 4.69;
break;
case "uramaki":
price = 4.49;
break;
case "temaki":
price = 5.19;
break;
default:
break;
}
break;
case "Sushi Bar":
switch (sushiType)
{
case "sashimi":
price = 5.25;
break;
case "maki":
price = 5.55;
break;
case "uramaki":
price = 6.25;
break;
case "temaki":
price = 4.75;
break;
default:
break;
}
break;
case "Asian Pub":
switch (sushiType)
{
case "sashimi":
price = 4.50;
break;
case "maki":
price = 4.80;
break;
case "uramaki":
price = 5.50;
break;
case "temaki":
price = 5.50;
break;
default:
break;
}
break;
default:
break;
}
if (restorant == "Sushi Zone" || restorant == "Sushi Time" || restorant == "Sushi Bar" || restorant == "Asian Pub")
{
if (sushiType == "sashimi")
{
total = portions * price;
}
else if (sushiType == "maki")
{
total = portions * price;
}
else if (sushiType == "uramaki")
{
total = portions * price;
}
else if (sushiType == "temaki")
{
total = portions * price;
}
if (online == "Y")
{
total = total * 0.20 + total;
}
System.out.printf("Total price: %f lv.", total);
}
else
{
System.out.printf("%s is invalid restaurant!", restorant);
}
}
}
Благодаря Ви!
Споделям решението на цялата зада, този път правилно, ако на някои му потрябва.
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); String sushiType=scanner.nextLine(); String restorant=scanner.nextLine(); int portions=Integer.parseInt(scanner.nextLine()); String online=scanner.nextLine(); double total=0; double price=0; switch(restorant) { case"Sushi Zone": switch(sushiType) { case"sashimi": price=4.99; break; case"maki": price=5.29; break; case"uramaki": price=5.99; break; case"temaki": price=4.29; break; default: break; } break; case"Sushi Time": switch(sushiType) { case"sashimi": price=5.49; break; case"maki": price=4.69; break; case"uramaki": price=4.49; break; case"temaki": price=5.19; break; default: break; } break; case"Sushi Bar": switch(sushiType) { case"sashimi": price=5.25; break; case"maki": price=5.55; break; case"uramaki": price=6.25; break; case"temaki": price=4.75; break; default: break; } break; case"Asian Pub": switch(sushiType) { case"sashimi": price=4.50; break; case"maki": price=4.80; break; case"uramaki": price=5.50; break; case"temaki": price=5.50; break; default: break; } break; default: break; } if ( restorant.equalsIgnoreCase("Sushi Zone") || restorant.equalsIgnoreCase("Sushi Time") || restorant.equalsIgnoreCase("Sushi Bar")|| restorant.equalsIgnoreCase("Asian Pub")) { if(sushiType.equalsIgnoreCase("sashimi")) { total=portions*price; } else if(sushiType.equalsIgnoreCase("maki")) { total=portions*price; } else if(sushiType.equalsIgnoreCase("uramaki")) { total=portions*price; } else if(sushiType.equalsIgnoreCase("temaki")) { total=portions*price; } if(online.equalsIgnoreCase("Y")) { total =total*0.20+total; } System.out.printf("Total price: %.2f lv.",total); } else { System.out.printf("%s is invalid restaurant!", restorant); } } }Решението работи, но за да избегнеш излишно повтаряне на
if (restorant == "Sushi Zone" || restorant == "Sushi Time" || restorant == "Sushi Bar" || restorant == "Asian Pub")
можеш default стойността на switch конструкцията за restorant да я зададеш като
default: System.out.printf("%s is invalid restaurant!",restorant); invalid=true; break;където invalid е boolean променлива. След това директно можеш да продължиш с кратък IF:
if (!invalid){ if (online.equalsIgnoreCase("Y")){ total=portions*price; total=Math.ceil(total*(1+0.20)); System.out.printf("Total price: %.0f lv.",total); }else{ total=Math.ceil(total*portions); System.out.printf("Total price: %.0f lv.",total); } }