java - Why dosen't my code continue when y is enter? -
i'm new programming (java) , stack overflow. code works except when run , press y keeps repeating "do want continue" , stuck in loop (everything else works). please tell me why happening , should fix it.
import java.util.scanner; import java.text.numberformat; import java.text.numberformat; import java.util.scanner; public class ch03_ex3_downloadtime { public static void main(string[] args) { int dspeed = 0; int fsize = 0; scanner sc = new scanner(system.in); { // input user system.out.println("welcome download time estimator"); system.out.print("enter file size (mb): "); fsize = sc.nextint(); system.out.print("enter download speed (mb/sec): "); dspeed = sc.nextint(); } int dtime = fsize / dspeed; int hours; int remainder; remainder = dtime % 3600; hours = dtime / 3600; int minutes; int seconds = remainder % 60; minutes = remainder / 60; string message = "\n" + "this download take approximately " + hours + " hours " + minutes + " minutes " + seconds + " seconds \n"; system.out.println(message); boolean iscontinuing = true; boolean inputisinvalid = true; while (inputisinvalid) { system.out.print("continue? (y/n): "); string choice = sc.next(); if ("y".equalsignorecase(choice)) { inputisinvalid = false; } else if ("n".equalsignorecase(choice)) { inputisinvalid = false; iscontinuing = false; } else { system.err.print("error: valid answers y/n. "); } } } }
import java.util.scanner; import java.text.numberformat; import java.text.numberformat; import java.util.scanner; public class ch03_ex3_downloadtime { public static void main(string[] args) { test t=new test(); t.chk(); } } class test{ public void chk(){ int dspeed = 0; int fsize = 0; scanner sc = new scanner(system.in); { // input user system.out.println ("welcome download time estimator"); system.out.print("enter file size (mb): "); fsize = sc.nextint(); system.out.print("enter download speed (mb/sec): "); dspeed = sc.nextint(); } int dtime = fsize / dspeed; int hours; int remainder; remainder= dtime % 3600; hours = dtime / 3600; int minutes; int seconds= remainder % 60; minutes = remainder / 60; string message = "\n" + "this download take approximately " + hours + " hours " + minutes + " minutes " + seconds + " seconds \n"; system.out.println(message); system.out.println("do want retry"); system.out.println("press 1 if yes"); system.out.println("press 2 if no"); int a=sc.nextint(); if(a==1) { chk(); } else { system.out.println("thanks"); } } }
Comments
Post a Comment