如何在Java中设置if-else来解析数组元素
我正试图设置我的Java代码来解析数组中的一个元素是否参加了迪斯尼乐园或环球影城。我的阵列有7个元素,可容纳7名与会者。参与者会收到特定于他们参加的公园的折扣,我创建了两个for循环来表示这两个公园 如何在main方法中设置代码以确定哪些与会者去了哪里如何在Java中设置if-else来解析数组元素,java,arrays,for-loop,Java,Arrays,For Loop,我正试图设置我的Java代码来解析数组中的一个元素是否参加了迪斯尼乐园或环球影城。我的阵列有7个元素,可容纳7名与会者。参与者会收到特定于他们参加的公园的折扣,我创建了两个for循环来表示这两个公园 如何在main方法中设置代码以确定哪些与会者去了哪里 public class Visitor { public static void main( String args[] ) { double totalAttendeeCost = 0.0; //Using 1 array
public class Visitor
{
public static void main( String args[] )
{
double totalAttendeeCost = 0.0;
//Using 1 array
VisitorPackage[] attendee = new VisitorPackage[7];
attendee[0] = new VisitorPackage("Mickey", 'S', "weekday",
"Disneyland", 75.0);
attendee[1] = new VisitorPackage("Donald", 'C', "weekday",
"Disneyland", 75.0);
attendee[2] = new VisitorPackage("Minnie", 'E', "weekend",
"Disneyland", 75.0);
attendee[3] = new VisitorPackage("Goofie", 'E', "weekend",
"Disneyland", 75.0);
attendee[4] = new VisitorPackage("Harry", 'C', "weekend", "Universal
Studios", 60.0);
attendee[5] = new VisitorPackage("Hermoine", 'S', "weekend",
"Universal Studios", 60.0);
attendee[6] = new VisitorPackage("Ron", 'E', "weekday", "Universal
Studios", 60.0);
//This is where I am looking for help
//if attendee == disneyland
for(int i=0; i < attendee.length; i++)
{
{
attendee[i].applyDiscount(attendee[i], 5.0, 10.0);
attendee[i].applyWeekdayRate(attendee[i], 15.0);
attendee[i].printInfo(attendee[i]);
totalAttendeeCost += attendee[i].getTotalCost();
}
}
//else if attendee == universal
for(int i=0; i < attendee.length; i++)
{
attendee[i].applyDiscount(attendee[i], 10.0, 15.0);
attendee[i].applyWeekdayRate(attendee[i], 20.0);
attendee[i].printInfo(attendee[i]);
totalAttendeeCost += attendee[i].getTotalCost();
}
//System.out.println("Total: $" + totalCostDisney + "\n");
System.out.println("--------------");
System.out.printf("Total: $%.2f\n", totalAttendeeCost);
}
}
请记住,与会者数组的每个元素都是VisitorPackage的一个特定实例。因此,在VisitorPackage类中使用您的Getter方法,但首先要去除该方法的参数,并将其简化为:
public String getDestinationPark() {
return this.destination;
}
public void applyDiscount(double childRate, double seniorRate) {
if (visitorType == 'C') {
totalCost -= ((totalCost * childRate) / 100);
}
else if (visitorType == 'S')
totalCost -= ((totalCost * seniorRate) / 100);
}
public void applyWeekdayRate(double weekdayDiscount) {
if (visitDay.equalsIgnoreCase("weekday")) {
totalCost -= weekdayDiscount;
}
}
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.print("$");
System.out.printf("%.2f", totalCost);
if (visitorType == 'E') {
System.out.print(" ***** Discount cannot be applied to " + visitorName);
}
System.out.println();
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
if (dest.equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
else if (dest.equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
switch (dest.toLowerCase()) {
case "disneyland":
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
case "universal studios": // if the string has a space
case "universalstudios": // OR if the string has no space
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
}
}
对VisitorPackage类中的所有其他方法执行相同的操作,在类方法中去掉VisitorPackage arrayInstance参数及其任何关系,如下所示:
public String getDestinationPark() {
return this.destination;
}
public void applyDiscount(double childRate, double seniorRate) {
if (visitorType == 'C') {
totalCost -= ((totalCost * childRate) / 100);
}
else if (visitorType == 'S')
totalCost -= ((totalCost * seniorRate) / 100);
}
public void applyWeekdayRate(double weekdayDiscount) {
if (visitDay.equalsIgnoreCase("weekday")) {
totalCost -= weekdayDiscount;
}
}
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.print("$");
System.out.printf("%.2f", totalCost);
if (visitorType == 'E') {
System.out.print(" ***** Discount cannot be applied to " + visitorName);
}
System.out.println();
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
if (dest.equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
else if (dest.equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
switch (dest.toLowerCase()) {
case "disneyland":
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
case "universal studios": // if the string has a space
case "universalstudios": // OR if the string has no space
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
}
}
因为attendee数组的每个元素都是VisitorPackage类的实例,所以不需要将该实例发送到类方法。当您提供索引号时,它已经知道您指的是哪个实例(米奇、唐纳德、罗恩等)。由于Minnie位于attendee数组的索引2,因此对VisitorPackage类的任何调用,如attendee[2]。applyWeekdayRate(15.0)
将仅适用于Minnie
现在,当您想要处理与会者的数组时:
// iterate through each attendee and
// apply necessary discounts, etc.
for (int i = 0; i < attendee.length; i++) {
// Disneyland
if (attendee[i].getDestinationPark().equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
// Universal Studios
else if (attendee[i].getDestinationPark().equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
//遍历每个与会者并
//实行必要的折扣等。
对于(int i=0;i
或者你可以这样做:
public String getDestinationPark() {
return this.destination;
}
public void applyDiscount(double childRate, double seniorRate) {
if (visitorType == 'C') {
totalCost -= ((totalCost * childRate) / 100);
}
else if (visitorType == 'S')
totalCost -= ((totalCost * seniorRate) / 100);
}
public void applyWeekdayRate(double weekdayDiscount) {
if (visitDay.equalsIgnoreCase("weekday")) {
totalCost -= weekdayDiscount;
}
}
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.print("$");
System.out.printf("%.2f", totalCost);
if (visitorType == 'E') {
System.out.print(" ***** Discount cannot be applied to " + visitorName);
}
System.out.println();
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
if (dest.equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
else if (dest.equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
switch (dest.toLowerCase()) {
case "disneyland":
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
case "universal studios": // if the string has a space
case "universalstudios": // OR if the string has no space
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
}
}
for(inti=0;i
或者你可以这样做:
public String getDestinationPark() {
return this.destination;
}
public void applyDiscount(double childRate, double seniorRate) {
if (visitorType == 'C') {
totalCost -= ((totalCost * childRate) / 100);
}
else if (visitorType == 'S')
totalCost -= ((totalCost * seniorRate) / 100);
}
public void applyWeekdayRate(double weekdayDiscount) {
if (visitDay.equalsIgnoreCase("weekday")) {
totalCost -= weekdayDiscount;
}
}
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.print("$");
System.out.printf("%.2f", totalCost);
if (visitorType == 'E') {
System.out.print(" ***** Discount cannot be applied to " + visitorName);
}
System.out.println();
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
if (dest.equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
else if (dest.equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
switch (dest.toLowerCase()) {
case "disneyland":
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
case "universal studios": // if the string has a space
case "universalstudios": // OR if the string has no space
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
}
}
for(inti=0;i
除了DevilsHnd指出的代码问题之外,我还想引导您走向另一个方向
- 使用streams获取您想要的报告,以及
- 使用枚举来表示这些概念,而不是使用字符串来表示目的地、访问者类型和访问日期,可以大大简化代码,并非常清楚折扣方案的工作原理
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingDouble;
public class Visitor {
public enum VisitorType {
Child, Senior, Other
}
public enum VisitType {
Weekday,Weekend
}
// represents each Destination as well as all related discount rates.
public enum Destination {
Disneyland {
{
visitorDiscount.put(VisitorType.Child, 5.0);
visitorDiscount.put(VisitorType.Senior, 10.0);
visitTypeDiscount.put(VisitType.Weekday, 15.0);
}
}
, UniversalStudios {
{
visitorDiscount.put(VisitorType.Child, 10.0);
visitorDiscount.put(VisitorType.Senior, 15.0);
visitTypeDiscount.put(VisitType.Weekday, 20.0);
}
};
protected Map<VisitorType,Double> visitorDiscount= new HashMap();
protected Map<VisitType,Double> visitTypeDiscount= new HashMap();
public double getVisitorTypeDiscount(VisitorType visitorType) {
Double discount = visitorDiscount.get(visitorType);
return discount == null ? 0.0 : discount;
}
public double getVisitTypeDiscount(VisitType visitType) {
Double discount = visitTypeDiscount.get(visitType);
return discount == null ? 0.0 : discount;
}
};
public static class VisitorPackage {
private final String visitorName;
private final VisitorType visitorType;
private final VisitType visitDay;
private final Destination destination;
private final double totalCost;
public VisitorPackage(String newVisitorName, VisitorType newVisitorType,
VisitType newVisitDay, Destination newDestination, double newTotalCost) {
visitorName = newVisitorName;
visitorType = newVisitorType;
visitDay = newVisitDay;
totalCost = newTotalCost;
destination = newDestination;
}
public String getDestinationPark() {
return destination.toString();
}
public double getTotalCost() {
return totalCost;
}
public double getDiscountedCost() {
double visitorTypeDiscount = totalCost * destination.getVisitorTypeDiscount(visitorType)/100;
return totalCost - visitorTypeDiscount - destination.getVisitTypeDiscount(visitDay);
}
public Destination getDestination() { return destination; }
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.printf("$%.2f -> ", getTotalCost());
System.out.print("$");
System.out.printf("%.2f", getDiscountedCost());
if (visitorType == VisitorType.Other) {
System.out.print(" ***** Discount cannot be applied to "
+ visitorName);
}
System.out.println();
}
}
public static void main(String args[]) {
double totalAttendeeCost = 0.0;
//Using 1 array
VisitorPackage[] attendee = new VisitorPackage[7];
attendee[0] = new VisitorPackage("Mickey", VisitorType.Senior, VisitType.Weekday,
Destination.Disneyland, 75.0);
attendee[1] = new VisitorPackage("Donald", VisitorType.Child, VisitType.Weekday,
Destination.Disneyland, 75.0);
attendee[2] = new VisitorPackage("Minnie", VisitorType.Other, VisitType.Weekend,
Destination.Disneyland, 75.0);
attendee[3] = new VisitorPackage("Goofie", VisitorType.Other, VisitType.Weekend,
Destination.Disneyland, 75.0);
attendee[4] = new VisitorPackage("Harry", VisitorType.Child, VisitType.Weekend, Destination.UniversalStudios, 60.0);
attendee[5] = new VisitorPackage("Hermoine", VisitorType.Senior, VisitType.Weekend,
Destination.UniversalStudios, 60.0);
attendee[6] = new VisitorPackage("Ron", VisitorType.Other, VisitType.Weekday, Destination.UniversalStudios, 60.0);
// Print a report grouped by Destination showing all VisitoerPackages and their discounted costs with subtotals
Arrays.stream(attendee)
.collect(groupingBy(VisitorPackage::getDestination))
.entrySet().stream()
.forEach(e->{
System.out.println("Summary for "+e.getKey());
e.getValue().stream().forEach(VisitorPackage::printInfo);
Double total = e.getValue().stream().collect(summingDouble(VisitorPackage::getDiscountedCost));
System.out.printf("Total Discounted Cost for %s = $%.2f\n",e.getKey(),total);
System.out.println("------------------------------------------------------------");
});
// Here's a way to reduce the dataset to map of sub-totals keyed by destination.
Map<Destination,Double> discountedCostByDest = Arrays.stream(attendee)
.collect(groupingBy(
VisitorPackage::getDestination,
summingDouble(VisitorPackage::getDiscountedCost)));
System.out.println(discountedCostByDest);
// compute and display the total cost.
Double totalDiscountedCost = Arrays.stream(attendee)
.collect(summingDouble(VisitorPackage::getDiscountedCost));
System.out.printf("Grand Total = $%.2f\n", totalDiscountedCost);
}
}
我还建议使用
enum
s,但我会更进一步,将折扣添加为实际enum
的一员。这样,您就不需要维护映射
、查询方法等。