Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

java - Multiplying float values "possible lossy conversion from double to float"

I have this problem wherein I have to convert kilometers into miles. I'm a novice programmer so bear with me.

Here's my code so far:

import java.util.Scanner;

public class problem1 {
    public static void main (String args[]) {
        float m;
        float km;

        Scanner input=new Scanner(System.in);

        System.out.print("Please enter a distance in kilometers:");
        km=input.nextFloat();
        m=km*0.621371;
        System.out.println("This is equal to: "+m);
    }
}

It gives me an error saying:

Incompatible types:possible lossy conversion from double to float.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are trying to set a double to a float variable

To fix, change this line

m=km*0.621371;

to

m=km*0.621371f;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...