无法输入字符串

时间:2016-10-31 18:15:06

标签: java

试图找出我的String userString ="&#34 ;;不被承认。我的所有其他变量类型都显示为不同的颜色。

由于

import 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 userChar = 'a';
  String userString = "";

  System.out.println("Enter integer: ");
  userInt = scnr.nextInt();

  System.out.println("Enter double: ");
  userDouble = scnr.nextDouble();

  System.out.println("Enter character: ");
  userChar = scnr.next().charAt(0);

  System.out.println("Enter string: ");
  userString = scnr.nextLine();

  System.out.println(userInt + " " + userDouble + " " + userChar + " " + userString);

  return;
  }
}

1 个答案:

答案 0 :(得分:0)

运行代码这是输出:

Enter integer: 
1
Enter double: 
22
Enter character: 
b
Enter string: 
1 22.0 b //Skips the userString = scnr.nextLine(); and jumps to the last println

出于某种原因,当我运行程序时,使用不同的.nextLine... .nextInt .next,它只使用.nextLine跳过读取最后一行,然后使用方法检查String是否为double integerchar可以解决此问题