这是我的代码:
导入java.util.Scanner;

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

  Scanner scnr = new Scanner(System.in);
  int userInt = 0;
  double userDouble = 0.0;
  char userLetter = 'z';

  userInt = scnr.nextInt();
  userDouble = scnr.nextDouble();
  userLetter = scnr.nextLine();

  System.out.println("Enter integer: " + userInt);
  System.out.println("Enter double: " + userDouble);
  System.out.println("Enter letter: " + userLetter);


当我用字母输入“ z”运行它时,它给了我这个错误

    BasicInput.java:13: error: incompatible types
userLetter = scnr.nextLine();
                            ^


必填:char

找到:字符串

1个错误

我该怎么做才能读取和输入字母?

最佳答案

这试图将String的值存储到char中-不适合

尝试

String userLetter = scnr.nextLine();


然后只用第一个字符

userletter.charAt (0);

10-07 12:00