java - how to initialize a destructive variable? -
this question has answer here:
- how compare strings in java? 23 answers
hi want ask how initialize variable when you're gonna use in condition? here's code made far..
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 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 vehicle: " + v); system.out.println("kilometers traveled according ticket: " + t); system.out.print("total toll charge: " + tc); }catch(ioexception e){ } } }
to make short, program runs when input vehicle type , ticket type, still returns 0 isn't supposed happen-- because used conditions assigns value each vehicle type , ticket types (also shown in table comes program). i've tried search proper initializations related can't find , confused. please help. sorry bein such noob
don't use ==
compare string
values; in java, objects, ==
compares object references determine if refer same object.
use string#equals
compare string
values.
if (vt.equals("car") || vt.equals("car"))
in fact, string#equalsignorecase
better in case, cut down on number of conditions in each if
statement:
if (vt.equalsignorecase("car"))
Comments
Post a Comment