variable.nextInt() 在 Java 程序中不工作
variable.nextInt() not working in Java program
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.nextLine();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
**System.out.println("Please enter the name of the second subject");
String B = input.nextLine();
System.out.println("Please enter the marks of the subject "+ B );**
float b = input.nextFloat();
System.out.println("Please enter the name of the last subject");
String C = input.nextLine();
在此代码中,我希望该程序在获取第一个主题标记的值后询问第二个主题的名称,而是打印第二个主题的打印语句并忽略使用第二个主题名称。
如果主题名称只有一个词,您应该使用 input.next() 而不是 input.nextLine()
试试这个:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.next();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
System.out.println("Please enter the name of the second subject");
String B = input.next();
System.out.println("Please enter the marks of the subject "+ B );
float b = input.nextFloat();
System.out.println("Please enter the name of the last subject");
String C = input.next();
当您使用读取标记的扫描器函数(例如:next()、nextInt()、nextFloat() 等)时,扫描器会读取并 returns 下一个标记。当您读取整行时(即:readLine()),它会从当前位置读取到下一行的开头。
使用这个
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.nextLine();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
System.out.println("Please enter the name of the second subject");
String B = input.next();
System.out.println("Please enter the marks of the subject "+ B );
input.nextLine();
float b = input.nextFloat();
input.nextLine();
System.out.println("Please enter the name of the last subject");
String C = input.next();
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.nextLine();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
**System.out.println("Please enter the name of the second subject");
String B = input.nextLine();
System.out.println("Please enter the marks of the subject "+ B );**
float b = input.nextFloat();
System.out.println("Please enter the name of the last subject");
String C = input.nextLine();
在此代码中,我希望该程序在获取第一个主题标记的值后询问第二个主题的名称,而是打印第二个主题的打印语句并忽略使用第二个主题名称。
如果主题名称只有一个词,您应该使用 input.next() 而不是 input.nextLine()
试试这个:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.next();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
System.out.println("Please enter the name of the second subject");
String B = input.next();
System.out.println("Please enter the marks of the subject "+ B );
float b = input.nextFloat();
System.out.println("Please enter the name of the last subject");
String C = input.next();
当您使用读取标记的扫描器函数(例如:next()、nextInt()、nextFloat() 等)时,扫描器会读取并 returns 下一个标记。当您读取整行时(即:readLine()),它会从当前位置读取到下一行的开头。
使用这个
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.nextLine();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
System.out.println("Please enter the name of the second subject");
String B = input.next();
System.out.println("Please enter the marks of the subject "+ B );
input.nextLine();
float b = input.nextFloat();
input.nextLine();
System.out.println("Please enter the name of the last subject");
String C = input.next();