這裏是作業: 編寫一個讀取兩個double值作爲輸入的java程序。您的程序需要使用Scanner類來讀取這兩個值。有了這兩個值,您的程序需要輸出以下內容: 該產品作爲一個雙倍產品,將第一個值乘以第二個產品的第二個 的商(作爲雙倍),第一個值除以第二個產品INT,由第二 商第一值乘以作爲int,由第二雙和詮釋博士。 java
這裏將第一值的的是我的代碼:
import java.util.Scanner;
public class Question_12 {
public static void main(String[] args) {
double ProductofDouble;
double QuotientofDouble;
int ProductofInt;
int QuotientofInt;
double ValueA;
double ValueB;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Value A: ");
ValueA = keyboard.nextDouble();
System.out.println("Enter Value B: ");
ValueB = keyboard.nextDouble();
//calc product as double
ProductofDouble = ValueA * ValueB;
//calc quotient as double
QuotientofDouble = ValueA/ValueB;
//calc product as int
ProductofInt = ValueA * ValueB;
//calc quotient as int
QuotientofInt = ValueA/ValueB;
System.out.println("The product of Value A and Value B as a double is: " + ProductofDouble);
System.out.println("The quotient of Value A and Value B as a double is: " + QuotientofDouble);
System.out.println("The product of Value A and Value B as an int is: " + ProductofInt);
System.out.println("The quotient of Value A and Value B as an int is: " + QuotientofInt);
} }
The Error:
2 errors found:
File: /Users/gcaruso/Documents/CISS 110/Module 3/Question_12.java [line: 26]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Question_12.java:26: possible loss of precision
found : double
required: int
File: /Users/gcaruso/Documents/CISS 110/Module 3/Question_12.java [line: 28]
Error: /Users/gcaruso/Documents/CISS 110/Module 3/Question_12.java:28: possible loss of precision
found : double
required: int
我知道你不應該在int中使用double值,但我不知道有任何其他方式來完成這個任務。
謝謝!
您可以通過鑄造雙爲int規避錯誤:''INT整數=(INT)originalDouble''。這會將其舍入到最接近的整數。它不會爲您自動執行,因爲這是一個經常犯的錯誤,所以需要確定程序員是否想要將其捨棄。另請注意,Java中的慣例是讓變量名以小寫字母開頭。 – Ghostkeeper 2014-09-18 23:46:20