Do While 问题:即使满足 do while 条件,程序也不会停止
Do While Issue : Program does not stop even when do while's condition is fulfilled
This program is for finding out the difference between the Compound Interest and Simple Interest.
当我将时间输入为 0(零)时,此代码应该终止并且代码应该 运行 至少一次。 并且当我编译代码时,编译器说:
Class compiled - no syntax errors
但是当我 运行 代码时,第二个条件 "run atleast once" 得到满足并且一切正常但是在接受值之后,如果时间输入为 [=34= 它不会停止]0(零)
(根据标题应该是) 但它给了我错误的答案。
示例:
p=3000
r=10
t=2
对于上述值,输出应该是 30.0
但是当我 运行 程序时它给我的输出是 -600.0
(一个消极的和错误的区别)。
import java.io.*;
class p6
{
InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);
// calculating the CI and SI
public void calc()throws IOException
{ int p,t,r,si;double a, ci;char k;
do
{
// 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
a = (double)p*(Math.pow((1+r/100),t));
// calculating CI
ci = a - p;
// printing Difference
System.out.println("Difference between CI and SI = "+(ci-si));
}while(t!=0);//end of DOWhile
}// end of calc
}//end of p6
我正在使用 BlueJ 编写和 运行 我的代码。
我是初学者,java一年前才开始学习。
请尽快帮我解决这个问题。
感谢任何帮助,
阿努拉格。
你在你的程序中调用了时间r和费率t,这不是交换了吗?
不应该是
System.out.println("Enter Rate in decimal :");
r = Integer.parseInt(br.readLine());
// getting Time
System.out.println("Enter Time :");
t = Integer.parseInt(br.readLine());
您在本应使用双精度数的地方使用了整数。
p
、t
、r
和 si
是整数。
所以 (1+r)/100
在你的情况下是 11/100
(你说 r
是 10
)。由于这是整数除法,结果是 0。因此,您将零提高到任何幂。还是零。
从零中减去本金,得到一个负数。
如果您的变量被声明为 double
,您将得到一个非零结果 (0.11),您可以将其与本金相乘。
尝试使用扫描器class。替换
InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);
来自
扫描仪扫描=新扫描仪(System.in);
此外,
si = (p*t*r)/100;
这就是你的问题所在。 p、t 和 r 是整数,所以这个操作会得到错误的值。这可以通过将整数类型更改为 float 或 double 或通过转换
来解决
si = ((double) (p*t*r))/100;
此外,即使您提供给 with 0,循环也会继续执行其中的所有行。要解决此问题,请将此语句放在 t 值之后:
if(t==0)
break;
而不是检查与利息计算相关的变量。对于利息计算,不仅 T 甚至 P 或 R 都不应该为 0,因此不要检查 t!=0,而是检查 SI!=0。如上所述,无论任何其他值如何,将 0 与任何值相乘都会得到 0。它避免了您对值
进行多变量检查
试试这个:
import java.io.*;
class p6
{
InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);
// calculating the CI and SI
public void calc()throws IOException
{ double p,t,r, a, si, ci;char k;//Changed all to double.
do
{
// getting principle
System.out.println("Enter Principle in decimal:");
p = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
// getting Rate
System.out.println("Enter Rate in decimal :");
r = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
// getting Time
System.out.println("Enter Time :");
t = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
//calculating SI
si = (p*t*r)/100;
// calculating amount
a = (double)p*(Math.pow((1+r/100),t));
// calculating CI
ci = a - p;
// printing Difference
System.out.println("Difference between CI and SI = "+(ci-si));
}while(t!=0);//end of DOWhile
}// end of calc
}//end of p6
This program is for finding out the difference between the Compound Interest and Simple Interest.
当我将时间输入为 0(零)时,此代码应该终止并且代码应该 运行 至少一次。 并且当我编译代码时,编译器说:
Class compiled - no syntax errors
但是当我 运行 代码时,第二个条件 "run atleast once" 得到满足并且一切正常但是在接受值之后,如果时间输入为 [=34= 它不会停止]0(零) (根据标题应该是) 但它给了我错误的答案。
示例:
p=3000
r=10
t=2
对于上述值,输出应该是 30.0
但是当我 运行 程序时它给我的输出是 -600.0
(一个消极的和错误的区别)。
import java.io.*;
class p6
{
InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);
// calculating the CI and SI
public void calc()throws IOException
{ int p,t,r,si;double a, ci;char k;
do
{
// 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
a = (double)p*(Math.pow((1+r/100),t));
// calculating CI
ci = a - p;
// printing Difference
System.out.println("Difference between CI and SI = "+(ci-si));
}while(t!=0);//end of DOWhile
}// end of calc
}//end of p6
我正在使用 BlueJ 编写和 运行 我的代码。
我是初学者,java一年前才开始学习。
请尽快帮我解决这个问题。
感谢任何帮助,
阿努拉格。
你在你的程序中调用了时间r和费率t,这不是交换了吗?
不应该是
System.out.println("Enter Rate in decimal :");
r = Integer.parseInt(br.readLine());
// getting Time
System.out.println("Enter Time :");
t = Integer.parseInt(br.readLine());
您在本应使用双精度数的地方使用了整数。
p
、t
、r
和 si
是整数。
所以 (1+r)/100
在你的情况下是 11/100
(你说 r
是 10
)。由于这是整数除法,结果是 0。因此,您将零提高到任何幂。还是零。
从零中减去本金,得到一个负数。
如果您的变量被声明为 double
,您将得到一个非零结果 (0.11),您可以将其与本金相乘。
尝试使用扫描器class。替换
InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);
来自 扫描仪扫描=新扫描仪(System.in);
此外,
si = (p*t*r)/100;
这就是你的问题所在。 p、t 和 r 是整数,所以这个操作会得到错误的值。这可以通过将整数类型更改为 float 或 double 或通过转换
来解决si = ((double) (p*t*r))/100;
此外,即使您提供给 with 0,循环也会继续执行其中的所有行。要解决此问题,请将此语句放在 t 值之后:
if(t==0)
break;
而不是检查与利息计算相关的变量。对于利息计算,不仅 T 甚至 P 或 R 都不应该为 0,因此不要检查 t!=0,而是检查 SI!=0。如上所述,无论任何其他值如何,将 0 与任何值相乘都会得到 0。它避免了您对值
进行多变量检查试试这个:
import java.io.*;
class p6
{
InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);
// calculating the CI and SI
public void calc()throws IOException
{ double p,t,r, a, si, ci;char k;//Changed all to double.
do
{
// getting principle
System.out.println("Enter Principle in decimal:");
p = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
// getting Rate
System.out.println("Enter Rate in decimal :");
r = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
// getting Time
System.out.println("Enter Time :");
t = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
//calculating SI
si = (p*t*r)/100;
// calculating amount
a = (double)p*(Math.pow((1+r/100),t));
// calculating CI
ci = a - p;
// printing Difference
System.out.println("Difference between CI and SI = "+(ci-si));
}while(t!=0);//end of DOWhile
}// end of calc
}//end of p6