如何在 java 中将 nextLine 用于扫描仪
How to use nextLine for scanner in java
我使用了正确的数据类型,但找不到我的错误。下面是我的代码;
Scanner input = new Scanner (System.in);
//Read
System.out.printf ("Enter the station: ");
String s = input.nextLine();
System.out.printf ("Enter quantity in liter: ");
double q = input.nextDouble();
System.out.printf ("Enter type of petrol: ");
String t = input.nextLine();
System.out.printf ("Enter price of petrol: ");
double p = input.nextDouble();
System.out.printf ("Enter discount: ");
int d = input.nextInt();
当我 运行 我的程序时,它不会转到下一行来输入值。对于汽油类型,我想输入类似“Super 99”的内容,所以我需要使用 input.nextLine() 但它不起作用。
你只需要使用:
System.out.println("Enter the type of petrol");
它将像这样工作:
Enter the type of petrol
Super99 //its what you will enter as per your choice here the cursor will automatically take you to the next line
试试下面的代码。
在双 q = input.nextDouble() 之后使用另一个 input.nextLine();线.
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
//Read
System.out.printf ("Enter the station: ");
String s = input.nextLine();
System.out.printf ("Enter quantity in liter: ");
double q = input.nextDouble();
input.nextLine();
System.out.printf ("Enter type of petrol: ");
String t = input.nextLine();
System.out.printf ("Enter price of petrol: ");
double p = input.nextDouble();
System.out.printf ("Enter discount: ");
int d = input.nextInt();
}
打印一行包括一个换行符:
System.out.println("Enter price of petrol: ");
// -OR-
System.out.printf("Enter price of petrol: \n");
获取单个标记(单个单词、数字、整数等):
scanner.nextInt(); // integer
scanner.next(); // word
获取整行:
scanner.useDelimiter("\r?\n");
// now the scanner is in 'entire line' mode
scanner.nextInt(); // still works
scanner.next(); // gets one line's worth
返回 'one token per whitespace' 模式,输入例如“a b c”(输入)将导致 3 .next()
次数据调用可用:
scanner.reset();
// -OR-
scanner.useDelimiter("\s+");
提示:不要混合使用 nextLine()
和任何其他 next
方法。
/*
*****亲爱的,这对你有用
*/
进口java.util.*;
class 测试人员
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.printf ("Enter the station: ");
String s = input.nextLine();
System.out.printf ("Enter quantity in liter: ");
double q = input.nextDouble();
input.nextLine();
System.out.printf ("Enter type of petrol: ");
String t = input.nextLine();
System.out.printf ("Enter price of petrol: ");
double p = input.nextDouble();
System.out.printf ("Enter discount: ");
int d = input.nextInt();
}
}
我使用了正确的数据类型,但找不到我的错误。下面是我的代码;
Scanner input = new Scanner (System.in);
//Read
System.out.printf ("Enter the station: ");
String s = input.nextLine();
System.out.printf ("Enter quantity in liter: ");
double q = input.nextDouble();
System.out.printf ("Enter type of petrol: ");
String t = input.nextLine();
System.out.printf ("Enter price of petrol: ");
double p = input.nextDouble();
System.out.printf ("Enter discount: ");
int d = input.nextInt();
当我 运行 我的程序时,它不会转到下一行来输入值。对于汽油类型,我想输入类似“Super 99”的内容,所以我需要使用 input.nextLine() 但它不起作用。
你只需要使用:
System.out.println("Enter the type of petrol");
它将像这样工作:
Enter the type of petrol
Super99 //its what you will enter as per your choice here the cursor will automatically take you to the next line
试试下面的代码。
在双 q = input.nextDouble() 之后使用另一个 input.nextLine();线.
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
//Read
System.out.printf ("Enter the station: ");
String s = input.nextLine();
System.out.printf ("Enter quantity in liter: ");
double q = input.nextDouble();
input.nextLine();
System.out.printf ("Enter type of petrol: ");
String t = input.nextLine();
System.out.printf ("Enter price of petrol: ");
double p = input.nextDouble();
System.out.printf ("Enter discount: ");
int d = input.nextInt();
}
打印一行包括一个换行符:
System.out.println("Enter price of petrol: ");
// -OR-
System.out.printf("Enter price of petrol: \n");
获取单个标记(单个单词、数字、整数等):
scanner.nextInt(); // integer
scanner.next(); // word
获取整行:
scanner.useDelimiter("\r?\n");
// now the scanner is in 'entire line' mode
scanner.nextInt(); // still works
scanner.next(); // gets one line's worth
返回 'one token per whitespace' 模式,输入例如“a b c”(输入)将导致 3 .next()
次数据调用可用:
scanner.reset();
// -OR-
scanner.useDelimiter("\s+");
提示:不要混合使用 nextLine()
和任何其他 next
方法。
/* *****亲爱的,这对你有用 */
进口java.util.*; class 测试人员 {
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.printf ("Enter the station: ");
String s = input.nextLine();
System.out.printf ("Enter quantity in liter: ");
double q = input.nextDouble();
input.nextLine();
System.out.printf ("Enter type of petrol: ");
String t = input.nextLine();
System.out.printf ("Enter price of petrol: ");
double p = input.nextDouble();
System.out.printf ("Enter discount: ");
int d = input.nextInt();
}
}