hey im in AP computer science A (taking it on edhesive) and I'm confused on how to print a variable (double) but make it show up as an integer and i dont know what I'm doing wrong. (I'm using DrJava). Please help (I'll give brainliest if it works)!!
here is my code:
/*
* Lesson 6 Coding Activity Question 2
*
* Input a double value and print only the integer part.
*
* Sample run:

Please input a decimal number:
57.8934
Answer: 57

*/

import java.util.Scanner;
import java.lang.Math;

class Lesson_6_Activity_Two {
public static void main(String[] args) {

System.out.println("Please input a decimal number");
Scanner scan = new Scanner (System.in);

Double x = scan.nextDouble();

int b = scan.nextDouble();

System.out.println(b);


}
}

Respuesta :

Answer:

Your program will cause lossy conversion from double to int.

Explanation:

I have analyzed you code properly. You need to take the input as a double number and convert it to an integer. You are making two mistakes in your code.

1) You have not declared the main class. Therefore, the program is unable to find a main class and throwing an error of not loading the main class. Therefore, you have to replace  "class Lesson_6_Activity_Two" with "public class main".

2) You are assigning a double value to an integer. It cause a lossy conversion from double to int. Therefore, majority of compilers will throw this. To convert double to int, you have to typecast the double using "intValue();" function.

For more understanding, I have attached the correct code below.

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 System.out.println("Please input a decimal number");

       Scanner scan = new Scanner (System.in);

       Double x = scan.nextDouble();

       System.out.println("Output");

       int b = x.intValue();

       System.out.println(b);

}

}

Answer:

num = float(input("45.6")

if num>45.6

print("90")

else:

print("Not greater than 45.6")

Explanation:

im not sure