嗨,我只是想问一下,当您要在条件下使用变量时,如何正确初始化变量?这是我到目前为止制作的代码..
import java.io.*;
public class Bwiset{
public static void main(String[]args){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=0;
double v=0;
String vt,tt;
double tc;
try{
System.out.print("==================================\nVehicle Type \tCharge per km \n Car \t\t Php0.50\n Light Truck \t Php0.75");
System.out.print("\n Bus \t\t Php1.00");
System.out.print("\n Heavy Truck \t Php1.25\n");
System.out.print("==================================\n");
System.out.print("Km Travelled \t Ticket Color\n");
System.out.print(" 15 \t \t Yellow\n");
System.out.print(" 25 \t \t Blue\n");
System.out.print(" 50 \t \t Red\n");
System.out.print(" 75 \t \t Orange\n");
System.out.println("Welcome! Please enter your vehicle type: ");
vt=br.readLine();
System.out.println("Enter ticket type: ");
tt=br.readLine();
if (vt=="car"||vt=="Car"||vt=="CAR"||vt=="cAr"||vt=="caR"||vt=="CAr"||vt=="cAR"){
v=0.50;
}else if (vt=="light truck"||vt=="Light Truck"||vt=="LIGHT TRUCK"){
v=0.75;
}else if (vt=="bus"||vt=="Bus"||vt=="BUS"){
v=1.00;
}else if (vt=="heavy truck"||vt=="Heavy Truck"||vt=="HEAVY TRUCK"){
v=1.25;
}
if (tt=="Yellow"||tt=="yellow"||tt=="YELLOW"){
t=15;
}else if (tt=="BLUE"||tt=="blue"||tt=="Blue"){
t=25;
}else if (tt=="red"||tt=="RED"||tt=="Red"){
t=50;
}else if (tt=="orange"||tt=="ORANGE"||tt=="Orange"){
t=75;
}
tc=v*t;
System.out.println("Vehicle Type: " + vt);
System.out.println("Ticket Type:" + tt);
System.out.println("Charge According to Vehicle: " + v);
System.out.println("Kilometers traveled according to ticket: " + t);
System.out.print("Total Toll Charge: " + tc);
}catch(IOException e){
}
}
}
简而言之,程序会运行,但是当您输入车辆类型和票证类型时,它仍然返回不应该发生的0——因为我使用了为每种车辆类型和票证类型分配特定值的条件(也显示在程序附带的表格中)。我试图搜索与此相关的正确初始化,但我找不到任何东西,我很困惑。请帮忙。抱歉成为这样的菜鸟