我正在編寫一個非常簡單的程序,用戶試圖找出他是否改變了煎餅的直徑(新舊直徑都是用戶輸入的),給定數量(用戶輸入),可以製作多少個具有新直徑的薄煎餅。現在我的計算與測試數據一起工作:10箇舊直徑,8箇舊數量,4個新直徑。這導致了50個新的煎餅是正確的,但是當我輸入5箇舊的直徑,10箇舊的數量,10個新的直徑時。這導致1.59999新煎餅不正確,答案應該是2.5。這裏是我的代碼,任何幫助,將不勝感激:Java程序對某些數據集計算不正確,但對其他數據集正確計算
import java.util.Scanner;
public class pancakes {
public static void main(String[] args) {
int diameterOld = 0; // used to save diameter of original pancakes
int quantityOld = 0; // used to save quantity of original pancakes
int diameterNew = 0; // used to save diameter of new pancakes
double quantityNew = 0; // used to save quantity of new pancakes
double areaNew = 0; // used to store area of new pancakes
double areaOld = 0; // used to store area of original pancakes
double totalSurfaceArea = 0; // total amount of batter used
Scanner input = new Scanner(System.in); // allows keyboard input
System.out.println ("Mohammad's java Pancakes");
System.out.print ("Diameter of original pancakes (inches): "); // Prints statement asking for diameter of original pancakes
diameterOld = input.nextInt(); // saves input into diameterOld
System.out.print ("Quantity of original pancakes: "); // Prints statement asking for quantity of original pancakes
quantityOld = input.nextInt(); // saves input into quantityOld
System.out.print ("Diameter of new pancakes (inches): "); // Prints statement asking for diameter of new pancakes
diameterNew = input.nextInt(); // saves input into diameterNew
areaOld = ((diameterOld/2)*(diameterOld/2)*Math.PI); // calculates area of original pancakes
totalSurfaceArea = (areaOld * quantityOld); // calculates total amount of batter needed
areaNew = ((diameterNew/2)*(diameterNew/2)*Math.PI); // calculates area of new pancakes
quantityNew = (totalSurfaceArea/areaNew); // calculates quantity of new pancakes
System.out.println ("Quantity of new pancakes: " + quantityNew); // Prints statement stating quantity of new pancakes and amount of new pancakes
input.close(); // Closes scanner input
}
}
困惑......不會老5直徑,10歲數量,5個新的直徑= 0新的煎餅? – 2014-09-12 19:49:20
我只得到1.59999999,如果我輸入5,10,10。 – rgettman 2014-09-12 19:50:52