英文:
How to use nextLine for scanner in java
问题
我使用了正确的数据类型,但是找不到错误。以下是我的代码:
Scanner input = new Scanner(System.in);
// 读取
System.out.printf("输入加油站:");
String s = input.nextLine();
System.out.printf("输入升数:");
double q = input.nextDouble();
input.nextLine(); // 读取换行符
System.out.printf("输入汽油类型:");
String t = input.nextLine();
System.out.printf("输入汽油价格:");
double p = input.nextDouble();
System.out.printf("输入折扣:");
int d = input.nextInt();
当我运行我的程序时,它不会进入下一行以输入值。对于汽油类型,我想要输入类似于 "Super 99" 这样的内容,所以我需要使用 input.nextLine()
,但它没有起作用。
英文:
I used correct data type but I cant find my errors. Below is my code;
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();
when I run my program, It does not go to next line to input values. For petrol type, I want to input something like "Super 99" so I need to use input.nextLine() but its not working.
答案1
得分: 0
尝试以下代码。
在 double q = input.nextDouble();
这一行之后再加一行 input.nextLine();
。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取
System.out.printf("输入加油站:");
String s = input.nextLine();
System.out.printf("输入升数:");
double q = input.nextDouble();
input.nextLine();
System.out.printf("输入油品类型:");
String t = input.nextLine();
System.out.printf("输入油价:");
double p = input.nextDouble();
System.out.printf("输入折扣:");
int d = input.nextInt();
}
英文:
Try the below code.
Use another input.nextLine() right after the double q = input.nextDouble(); line.
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();
}
答案2
得分: 0
/*
*****这将对您有效
*/
import java.util.*;
class Tester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.printf("输入加油站:");
String s = input.nextLine();
System.out.printf("输入升数:");
double q = input.nextDouble();
input.nextLine();
System.out.printf("输入汽油类型:");
String t = input.nextLine();
System.out.printf("输入汽油价格:");
double p = input.nextDouble();
System.out.printf("输入折扣:");
int d = input.nextInt();
}
}
英文:
/*
*****This will work for you dear
*/
import java.util.*;
class Tester
{
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();
}
}
答案3
得分: -1
你只需要使用:
System.out.println("输入汽油类型");
它将像这样工作:
输入汽油类型
Super99 //根据您的选择在此输入,光标将自动移到下一行
英文:
You just need to use:
System.out.println("Enter the type of petrol");
It will work like this :
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
答案4
得分: -1
要打印包括换行符在内的一行内容:
```java
System.out.println("输入汽油价格:");
// -OR-
System.out.printf("输入汽油价格:\n");
获取单个标记(单词、数字、整数等):
scanner.nextInt(); // 整数
scanner.next(); // 单词
获取整行内容:
scanner.useDelimiter("\r?\n");
// 现在扫描器处于“整行”模式
scanner.nextInt(); // 仍然有效
scanner.next(); // 获取一行的内容
要返回到“每个空白字符一个标记”的模式,在此模式下,输入例如 "a b c"(按Enter键)将导致有3个 .next()
调用的数据可用:
scanner.reset();
// -OR-
scanner.useDelimiter("\\s+");
提示:不要混合使用 nextLine()
和任何其他 next
方法。
<details>
<summary>英文:</summary>
To print a line _including_ a newline:
System.out.println("Enter price of petrol: ");
// -OR-
System.out.printf("Enter price of petrol: \n");
To obtain a single token (a single word, number, integer, etc):
scanner.nextInt(); // integer
scanner.next(); // word
To obtain an entire line:
scanner.useDelimiter("\r?\n");
// now the scanner is in 'entire line' mode
scanner.nextInt(); // still works
scanner.next(); // gets one line's worth
To go back to 'one token per whitespace' mode, where entering e.g. "a b c" (enter) will cause 3 `.next()` calls of data to be available:
scanner.reset();
// -OR-
scanner.useDelimiter("\s+");
TIP: Don't mix `nextLine()` and any other `next` method.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论