Java if statement to change a variable value -
i've started java, done 5-6 hours have no idea how approach task.
i'm doing tax system hotel.
standard rate of tax used (20%).
however, program asks user if want change rate asking user input "yes" or "no", if yes, proceed enter new value of variable tax
, if no, keeps standard rate of (20%).
there's lot more task. however, issue i'm struggling with.
can tell me how write such if statement? have absolutely no idea how such specific user input.
any sort of or suggestions appreciate can play around it.
perhaps can find solution within following example. please note it's quick , dirty example without errorhandling or gui.
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class demo { private int rate; public static void main(string[] args) { demo demo = new demo(); demo.work(); } private void work() { rate = 20; system.out.println("would use default tax?"); if (!getinput().equalsignorecase("yes")) { system.out.println("please set new rate"); rate = integer.parseint(getinput()); } system.out.println("the rate "+rate); } private string getinput() { string result = null; bufferedreader reader = new bufferedreader(new inputstreamreader(system.in)); try { result = reader.readline(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return result; } }
Comments
Post a Comment