gpt4 book ai didi

java - 错误: incompatible types possible lossy conversion from float to char

转载 作者:太空宇宙 更新时间:2023-11-04 11:59:15 25 4
gpt4 key购买 nike

我在这个项目上工作了一个小时,但仍然找不到解决方案。我总是收到相同的错误(EnigmaEncrypt.java:44: 错误:不兼容的类型:可能从 float 到 char 的有损转换 char e = exampleCharInput; ^1 个错误)这是我的代码:

 import java.util.Scanner;



// Created by ab38 on 2015-12-01.

public class EnigmaEncrypt {

// Static Scanner field

private static Scanner scan;

/**
* Get a char from the command line.
*
* @return the first char of the current line, typed by the user
* on the command line.
*/
private static char getChar() {
return scan.next().charAt(0);
}

/**
* Get an integer from the command line.
*
* @return the integer on the current line, typed by the user
* on the command line.
*/
private static int getInt() {
return scan.nextInt();
}

public static void main(String[] args) {
scan = new Scanner(System.in);

// Example: How to get an integer from the command line:
System.out.println("Enter an integer:");
int exampleIntInput = getInt();
System.out.println("Rotorposition: " + exampleIntInput);


// Example: How to get a single char from the command line:
System.out.println("Enter a single character:");
float exampleCharInput = getChar() + exampleIntInput;
char e = exampleCharInput;

System.out.println("Char from command line: " + e);


}
}

最佳答案

char 存储在 2 个字节中,而 float 存储在 4 个字节中。您试图将 float 放入字符中而不进行转换。试试这个吧

char e = (char) exampleCharInput;

关于java - 错误: incompatible types possible lossy conversion from float to char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41091076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com