本文介绍了如果语句运算符被忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在以下方法中,当输入窗口出现时,它不会正确添加SolarPanel。 当我输入Electric后跟Yes / yes时,我得到以下基本价格:20000 电动型号:2000 总计:22000 但当我做电+是/是时我得到 基本价格:20000 电动型号:2000 太阳能电池板:5000 总计:27000 怎么来的? public static int CalculateCost() { String typeOfCarCost = askCarType(); String SolarPanelCost = askSolarPanel(); int basicPrice = 20000; int ElectricModel = 2000; int SolarPanel = 5000; int total = 0; if(typeOfCarCost.equals(Electric)||(typeOfCarCost.equals(electric)&&(SolarPanelCost.equals(No)||(SolarPanelCost.equals(没有))))) { total = basicPrice + ElectricModel; System.out.println(基本价格:+ basicPrice); System.out.println(Electric Model:+ ElectricModel); System.out.println(总计:+总计); } else if(typeOfCarCost.equals(Electric)||(typeOfCarCost.equals(electric)&&(SolarPanelCost.equals(Yes)|| (SolarPanelCost.equals(是))))) { total = basicPrice + ElectricModel + SolarPanel; System.out.println(基本价格:+ basicPrice); System.out.println(Electric Model:+ ElectricModel); System.out.println(太阳能电池板:+ SolarPanel); System.out.println(总计:+总计); } 其他 {总计+ = basicPrice; System.out.println(基本价格:+ basicPrice); System.out.println(总计:+总计); } 总回报; } //结束CalculateCost 例如,它询问:电或混合动力(我选择电动,这应该增加2000总成本)电动 太阳能电池板(是或否,我选择是。这应该增加5000总成本)是 控制台窗口会显示此信息,但它不会为太阳能电池板添加。 基本价格:20000 电动型号:2000 总计:22000 此外,我如何计算折扣-500选择电动模型和太阳能电池板时的总数。 整个代码发布在下面。 有什么建议吗? import javax.swing。*; 公共类short7 { / ** * @param args * / public static void main(String [] args){ // TODO自动生成的方法stub PrintOptions(); } //结束主 public static String askCarType() { String typeOfCar; typeOfCar = JOptionPane.showInputDialog(Electric还是Hybrid?); if(!typeOfCar.equals(Electric)&&(!typeOfCar.equals(electric)&&(!typeOfCar.equals(Hybrid)&&(!typeOfCar .equals(hybrid))))) { JOptionPane.showMessageDialog(null,你必须选择电动或混合型汽车。); typeOfCar = JOptionPane.showInputDialog(Electric还是Hybrid?); } 返回typeOfCar; } //结束askCarType public static String askSolarPanel() { String wantSolarPanel; wantSolarPanel = JOptionPane.showInputDialog(你想要太阳能电池板吗?); if(!wantSolarPanel.equals(Yes)&&(!wantSolarPanel.equals(yes)&&(!wantSolarPanel.equals(No)&&(!wantSolarPanel .equals(no))))) { JOptionPane.showMessageDialog(null,你必须输入是或否); wantSolarPanel = JOptionPane.showInputDialog(你想要太阳能电池板吗?); } 返回wantSolarPanel; } //结束askSolarPanel public static int calculateDiscount() { String typeOfCarSelected = askCarType(); String SolarPanelSelected = askSolarPanel(); int Discount = 0; if(typeOfCarSelected.equals(Electric)&&(typeOfCarSelected.equals(electric)&&(typeOfCarSelected.equals(Hybrid)&&(typeOfCarSelected.equals( hybrid)& SolarPanelSelected.equals(Yes)|| SolarPanelSelected.equals(yes))))) { Discount = 500; } 其他 {折扣= 0; } 退货折扣; } //结束calculateDiscount public static int CalculateCost() { String typeOfCarCost = askCarType(); String SolarPanelCost = askSolarPanel(); int basicPrice = 20000; int ElectricModel = 2000; int SolarPanel = 5000; int total = 0; if(typeOfCarCost.equals(Electric)||(typeOfCarCost.equals(electric)&(SolarPanelCost.equals(No)||(SolarPanelCost.equals(no) ))))) { total = basicPrice + ElectricModel; System.out.println(基本价格:+ basicPrice); System.out.println(Electric Model:+ ElectricModel); System.out.println(总计:+总计); } else if(typeOfCarCost.equals(Electric)||(typeOfCarCost.equals(electric)&(SolarPanelCost.equals(Yes)||(SolarPanelCost) .equals(是))))) { total = basicPrice + ElectricModel + SolarPanel; System.out.println(基本价格:+ basicPrice); System.out.println(Electric Model:+ ElectricModel); System.out.println(太阳能电池板:+ SolarPanel); System.out.println(总计:+总计); } 其他 {总计+ = basicPrice; System.out.println(基本价格:+ basicPrice); System.out.println(总计:+总计); } 总回报; } //结束CalculateCost public static void PrintOptions() { int printTotal = CalculateCost(); } //结束PrintOptions } //结束类short7 解决方案您需要使用双括号和'&&',如下所示: - if((typeOfCarCost.equals(Electric)|| typeOfCarCost.equals(electric))&&((SolarPanelCost.equals( 否)|| SolarPanelCost.equals(no))) { total = basicPrice + ElectricModel; System.out.println(Basic Price:+ basicPrice); System.out.println(Electric Model:+ ElectricModel); System.out.println(Total:+ total); } else if((typeOfCarCost.equals(Electric)|| typeOfCarCost.equals(electric))&&(SolarPanel Cost.equals(是)|| SolarPanelCost.equals(是))) { total = basicPrice + ElectricModel + SolarPanel; System.out.println(基本价格:+ basicPrice); System.out.println(Electric Model:+ ElectricModel); System.out.println(太阳能电池板:+ SolarPanel); System.out.println(总计:+总计); } In the following method, it doesn't do the addition for the SolarPanel correctly when the input window appears. When I type in "Electric" followed by "Yes/yes" i get the followingBasic Price:20000Electric Model:2000Total:22000but when I do "electric" + "yes/Yes" I getBasic Price:20000Electric Model:2000Solar Panel:5000Total:27000how come?public static int CalculateCost() { String typeOfCarCost = askCarType(); String SolarPanelCost = askSolarPanel(); int basicPrice = 20000; int ElectricModel = 2000; int SolarPanel = 5000; int total = 0; if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") && (SolarPanelCost.equals("No") || (SolarPanelCost.equals("no"))))) { total = basicPrice + ElectricModel; System.out.println("Basic Price:" + basicPrice); System.out.println("Electric Model:" + ElectricModel); System.out.println("Total:" + total); } else if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") && (SolarPanelCost.equals("Yes") || (SolarPanelCost.equals("yes"))))) { total = basicPrice + ElectricModel + SolarPanel; System.out.println("Basic Price:" + basicPrice); System.out.println("Electric Model:" + ElectricModel); System.out.println("Solar Panel:" + SolarPanel); System.out.println("Total:" + total); } else { total += basicPrice; System.out.println("Basic Price:" + basicPrice); System.out.println("Total:" + total); } return total; }//ends CalculateCostFor example, it asks:Electric or Hybrid ( I chose Electric, this should add 2000 to the total cost)ElectricSolar Panel (Yes or No, I chose yes. This should add 5000 to the total cost)YesThe console window displays this however, it doesn't do the addition for the Solar Panel.Basic Price:20000Electric Model:2000Total:22000Additionally, how can I factor in the discount of -500 on the total when both an Electric Model and Solar Panel are chosen. The whole code is posted below.Any advice?import javax.swing.*;public class short7 {/** * @param args */public static void main(String[] args) { // TODO Auto-generated method stub PrintOptions();}//ends main public static String askCarType() { String typeOfCar; typeOfCar = JOptionPane.showInputDialog("Electric or Hybrid?"); if (!typeOfCar.equals("Electric") && (!typeOfCar.equals("electric") && (!typeOfCar.equals("Hybrid") && (!typeOfCar.equals("hybrid"))))) { JOptionPane.showMessageDialog(null, "You have to choose either an Electric or Hybrid type of car."); typeOfCar = JOptionPane.showInputDialog("Electric or Hybrid?"); } return typeOfCar; }//ends askCarType public static String askSolarPanel() { String wantSolarPanel; wantSolarPanel = JOptionPane.showInputDialog("Do you want a Solar Panel?"); if (!wantSolarPanel.equals("Yes") && (!wantSolarPanel.equals("yes") && (!wantSolarPanel.equals("No") && (!wantSolarPanel.equals("no"))))) { JOptionPane.showMessageDialog(null, "You have to enter either Yes or No"); wantSolarPanel = JOptionPane.showInputDialog("Do you want a Solar Panel?"); } return wantSolarPanel; }//ends askSolarPanel public static int calculateDiscount() { String typeOfCarSelected = askCarType(); String SolarPanelSelected = askSolarPanel(); int Discount = 0; if (typeOfCarSelected.equals("Electric") && (typeOfCarSelected.equals("electric") && (typeOfCarSelected.equals("Hybrid") && (typeOfCarSelected.equals("hybrid") & SolarPanelSelected.equals("Yes") || SolarPanelSelected.equals("yes"))))) { Discount = 500; } else { Discount = 0; } return Discount; }//ends calculateDiscount public static int CalculateCost() { String typeOfCarCost = askCarType(); String SolarPanelCost = askSolarPanel(); int basicPrice = 20000; int ElectricModel = 2000; int SolarPanel = 5000; int total = 0; if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") & (SolarPanelCost.equals("No") || (SolarPanelCost.equals("no"))))) { total = basicPrice + ElectricModel; System.out.println("Basic Price:" + basicPrice); System.out.println("Electric Model:" + ElectricModel); System.out.println("Total:" + total); } else if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") & (SolarPanelCost.equals("Yes") || (SolarPanelCost.equals("yes"))))) { total = basicPrice + ElectricModel + SolarPanel; System.out.println("Basic Price:" + basicPrice); System.out.println("Electric Model:" + ElectricModel); System.out.println("Solar Panel:" + SolarPanel); System.out.println("Total:" + total); } else { total += basicPrice; System.out.println("Basic Price:" + basicPrice); System.out.println("Total:" + total); } return total; }//ends CalculateCost public static void PrintOptions() { int printTotal = CalculateCost(); }//ends PrintOptions}//ends class short7 解决方案 You need to use double bracket and '&&' as below:-if ((typeOfCarCost.equals("Electric") || typeOfCarCost.equals("electric")) && ((SolarPanelCost.equals("No") || SolarPanelCost.equals("no"))) { total = basicPrice + ElectricModel; System.out.println("Basic Price:" + basicPrice); System.out.println("Electric Model:" + ElectricModel); System.out.println("Total:" + total); }else if ((typeOfCarCost.equals("Electric") || typeOfCarCost.equals("electric")) && (SolarPanelCost.equals("Yes") || SolarPanelCost.equals("yes"))) { total = basicPrice + ElectricModel + SolarPanel; System.out.println("Basic Price:" + basicPrice); System.out.println("Electric Model:" + ElectricModel); System.out.println("Solar Panel:" + SolarPanel); System.out.println("Total:" + total); } 这篇关于如果语句运算符被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 14:30