这里是作业: 编写一个读取两个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