我正在尝试打印"fields."的全部值 但它跳过了第一个输入,为什么?


import java.util.Scanner;
public class elseif {
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner s = new Scanner(System.in);
System.out.println("Enter name");
String a = s.next();
System.out.println("Enter roll number");
int b = s.nextInt();
System.out.println("Enter Filed of interest");
s.next();//To consume 'n' left by ENTER KEY, it is not consumed by nextInt()
String c = s.nextLine();
System.out.println("Hey, my name is "+a+" and my roll number is "+b+". 
My field of interest is "+c+".");
}
}

输出:

Enter name
Subhadip
Enter roll number
21
Enter Filed of interest
music, dance
Hey, my name is Subhadip and my roll number is 21. My field of interest is dance.

它不打印";音乐">

您不需要s.next();,可以使用Integer.parseInt(s.nextLine());获得b

Scanner s = new Scanner(System.in);
System.out.println("Enter name");
String a = s.nextLine();
System.out.println("Enter roll number");
int b = Integer.parseInt(s.nextLine());
System.out.println("Enter Filed of interest");
String c = s.nextLine();
System.out.println("Hey, my name is "+a+" and my roll number is "+b+". My field of interest is "+c+".");

相关内容

  • 没有找到相关文章

最新更新