简体   繁体   中英

Double and Int dr. java

Here is the assignment: Write a java program that reads two double values as input. Your program will need to use the Scanner class to read the two values. With these two values your program needs to output the following: the product, as a double, of multiplying the first value by the second the quotient, as a double, of dividing the first value by the second the product, as an int, of multiplying the first value by the second the quotient, as an int, of dividing the first value by the second

Here is my code:

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

I know that you are not supposed to use double values in an int, but I don't know of any other way to do this assignment.

Thank you!

You will need to use a type of operation called a cast, to explicitly narrow the type from double to int :

ProductofInt = (int)(ValueA * ValueB);

In addition, looking at your code, I will suggest a few more points of improvement:

  • Class names should be PascalCase, not Captialized_And_Underscored. So, your class name would be more viable as Question12 .
  • Variable names should be camelCase , for example productAsDouble . Also, the word "as" makes a bit more sense than "of"--you're taking the product and representing it as a double/int, instead of taking a product of specifically ints or doubles (your factors/divisor/dividend are always doubles)
  • You should declare variables as late as possible for readability. Instead of declaring ProductOfDouble at the top and then setting it later on, declare and set it at the same time:

     double ProductofDouble = ValueA * ValueB; 

This is java detecting what you are doing and thinking it may be a mistake on your part - int s are less specific than double s.

To fix it, use a cast:

ProductofInt = (int)(ValueA * ValueB);
QuotientofInt = (int)(ValueA / ValueB);

You used the Double to calculate an int result , the double is more precise than the int so you lost precision . what you needed to do is to cast the variables from double to int by doing the following double a; a=(int) b;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM