[英]Java program only outputting one line of code
我的班級Java程序出現問題。 我必須創建一個程序來提示用戶輸入輸出文件的路徑和名稱,該文件將包含我的程序將采用的方程式的系數行,並使用二次公式計算解。 到目前為止,我認為除了我的輸出文件之外,其他一切都正確。 假設我有一個包含3行系數的輸入文件,我的程序將在控制台流中顯示解決方案,但是在我的輸出文件上僅顯示1行解決方案。
while (input.hasNext()) {
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
discriminant = Math.pow(b, 2) - 4 * a * c;
///There will be no solutions if discriminant<0
if (discriminant < 0){
System.out.println("There are no solutions.");
output.println("There are no solutions.");
}
///As with the above, if coefficent a = 0 no solutions
else if (a == 0){
System.out.println("There are no solutions.");
output.println("There are no solutions.");
}
else if (discriminant == 0){
solutionOne = (-b + Math.sqrt(discriminant)) / (2 * a);
if (b < 0) {
System.out.printf("%3.0fx^2 %3.0fx + %3.0f, has one solution:%5.3f%n",a,b,c,solutionOne);
output.printf("%3.0fx^2 %3.0fx + %3.0f has one solution:%5.3f%n",a,b,c,solutionOne);
}
else{
System.out.printf("%3.0fx^2 %3.0fx + %3.0f has one solution:%5.3f%n",a,b,c,solutionOne);
output.printf("%3.0fx^2 %3.0fx + %3.0f has one solution:%5.3f%n",a,b,c,solutionOne);
}
}
else if(discriminant>0){
solutionOne=(-b + Math.sqrt(discriminant))/(2*a);
twoSolutions=(-b - Math.sqrt(discriminant))/(2*a);
if(b<0){
System.out.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions: %5.3f %5.3f%n",a,b,c,solutionOne,twoSolutions);
output.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions:5.3f %5.3f%n",a,b,c,solutionOne,twoSolutions);
}
else{
System.out.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions:%5.3f %5.3f%n",a,b,c,solutionOne,twoSolutions);
output.printf("%3.0fx^2 %3.0fx + %3.0f has two solutions: %5.3f%5.3f%n",a,b,c,solutionOne,twoSolutions);
}
}
output.close();
如果我正確閱讀了括號,則問題是您在打電話
output.close();
在循環的每次迭代結束時。 完成所有輸出的編寫后,需要在循環外調用該函數 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.