java - Do While Issue : Program does not stop even when do while's condition is fulfilled -


this program finding out difference between compound interest , simple interest.

this code should terminate when enter time 0(zero) , code should run @ least once. , when compile code, compiler says:

class compiled - no syntax errors

but when run code,the second condition "run atleast once" fulfilled , every thing works fine after accepting values, not stop if time entered 0(zero) (which according title should) , gives me wrong answer.

example :

p=3000 r=10 t=2 

for above values, output should 30.0 when run program gievs me output -600.0 (a negetive , wrong difference).

import java.io.*; class p6  {   inputstreamreader i1 =new inputstreamreader(system.in); bufferedreader br=new bufferedreader(i1);   // calculating ci , si public void calc()throws ioexception {   int p,t,r,si;double a, ci;char k;         {       // getting principle       system.out.println("enter principle in decimal:");                  p = integer.parseint(br.readline());       // getting rate       system.out.println("enter rate in decimal    :");        t = integer.parseint(br.readline());       // getting time       system.out.println("enter time               :");        r = integer.parseint(br.readline());       //calculating si       si = (p*t*r)/100;       // calculating amount       = (double)p*(math.pow((1+r/100),t));         // calculating ci       ci = - p;       // printing difference       system.out.println("difference between ci , si = "+(ci-si));        }while(t!=0);//end of dowhile   }// end of calc }//end of p6 

i'm using bluej write , run code.

i'm beginner , started learning java year ago.

please me resolve issue possible.

any appreciated,

anurag.

try out this:

import java.io.*; class p6  {   inputstreamreader i1 =new inputstreamreader(system.in); bufferedreader br=new bufferedreader(i1);   // calculating ci , si public void calc()throws ioexception {   double p,t,r, a, si, ci;char k;//changed double.         {       // getting principle       system.out.println("enter principle in decimal:");                  p = double.parsedouble(br.readline());// changed double.parsedouble store double value.       // getting rate       system.out.println("enter rate in decimal    :");        r = double.parsedouble(br.readline());// changed double.parsedouble store double value.       // getting time       system.out.println("enter time               :");        t = double.parsedouble(br.readline());// changed double.parsedouble store double value.       //calculating si       si = (p*t*r)/100;       // calculating amount       = (double)p*(math.pow((1+r/100),t));         // calculating ci       ci = - p;       // printing difference       system.out.println("difference between ci , si = "+(ci-si));        }while(t!=0);//end of dowhile   }// end of calc }//end of p6 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -