Java:使用.charAt(0)将String转换为char不起作用,为什么?

时间:2017-10-30 11:27:09

标签: java

所以我收集信息,即2个数字和一个字符串,稍后在我的代码中处理它们。数字工作正常,只要我能输出信输出用户输入的字母,一切都会正常工作。任何帮助非常感谢。为了清楚起见,当我说'不工作'时,我的意思是字母是空白的,最后一行代码输出:

这封信是

   Scanner sc = new Scanner(System.in);

    System.out.println("Enter two integers and a Letter ");

    int num1 = sc.nextInt();  

    int num2 = sc.nextInt(); 

    String s = sc.nextLine(); 




   System.out.println("s is of type " +  s.getClass().getName());   // s is a String

   System.out.println(s); //s has value of this

    char letter = s.charAt(0);  //Why isn't this working? 

    System.out.println("The letter is " + letter); //letter isn't showing up

0 个答案:

没有答案