在我的Java程序中读取第二个用户输入不会';行不通

在我的Java程序中读取第二个用户输入不会';行不通,java,Java,我想做一个程序,让用户输入他们想买多少台笔记本电脑,然后问他们这些笔记本电脑是否有不同的价格。如果答案是“是”,它会根据笔记本电脑的数量重复这个问题。否则,只需询问一次,然后计算笔记本电脑的成本 我明白了: How many laptops do you want to buy? 3 Do they have different price (yes/no)? Enter laptop/s price: import java.util.Scanner; 公共类LaptopCostCalcul

我想做一个程序,让用户输入他们想买多少台笔记本电脑,然后问他们这些笔记本电脑是否有不同的价格。如果答案是“是”,它会根据笔记本电脑的数量重复这个问题。否则,只需询问一次,然后计算笔记本电脑的成本

我明白了:

How many laptops do you want to buy? 3
Do they have different price (yes/no)? Enter laptop/s price:
import java.util.Scanner;
公共类LaptopCostCalculatorV2{
公共静态void main(字符串[]args){
int-lapplice=0;
int-laptopQuantity;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“您想买多少台笔记本电脑?”);
laptopQuantity=scan.nextInt();
系统输出打印(“它们是否有不同的价格(是/否)?”;
String choose=scan.nextLine();
如果(选择.equals(“是”)){
对于(int i=0;i
为什么不让我输入第二个问题的答案?

import java.util.Scanner;
import java.util.Scanner;

public class LaptopCostCalculatorV2 {
    public static void main(String[] args) {
        int laptopsPrice = 0;
        int laptopQuantity;

        Scanner scan = new Scanner(System.in);

        System.out.print("How many laptops do you want to buy? ");
        laptopQuantity = scan.nextInt();
        scan.nextLine();
        System.out.print("Do they have different price (yes/no)? ");
        String choose = scan.nextLine();
        if ("yes".equals(choose)) {
            for (int i = 0; i < laptopQuantity; i++) {
                System.out.println("Enter laptop price");
                laptopsPrice += scan.nextInt();
                scan.nextLine();
            }
        } else {
            System.out.println("Enter laptop/s price: ");
            laptopsPrice += scan.nextInt() * laptopQuantity;
            scan.nextLine();
        }
        System.out.println("Total is: " + laptopsPrice);
    }
}
公共类LaptopCostCalculatorV2{ 公共静态void main(字符串[]args){ int-lapplice=0; int-laptopQuantity; 扫描仪扫描=新扫描仪(System.in); System.out.print(“您想买多少台笔记本电脑?”); laptopQuantity=scan.nextInt(); scan.nextLine(); 系统输出打印(“它们是否有不同的价格(是/否)?”; String choose=scan.nextLine(); 如果(“是”。等于(选择)){ 对于(int i=0;i

nextInt
读取缓冲区中的下一个整数,但不会自动“跳过”到下一行。此行为允许您通过连续调用
nextInt()
,在一行中读取多个整数,但要开始读取下一行,应随后调用
nextLine()
nextLine()。因为我们对读取其余的内容不感兴趣,所以我们只丢弃它的返回值。如果
12 whatever
是行,那么
12
将是
nextLine()
的结果,而
“whatever”
将是以下
nextLine()
的结果。即使用户只键入了
12
,也需要
nextLine()
,尽管它只会返回一个空字符串

您的问题是什么?你的代码中有什么不起作用?也许谢谢,你能告诉我这是什么意思吗?scan.nextLine();对下层选民说:我已经校对并纠正了错误的标识符和缺少的数量乘法;起初,我并没有真正检查我的代码是否编译且正确,因为我认为OP只是询问一些微妙的扫描仪解析行为,并演示
nextLine()
在这里如何工作就足够了。其余的问题很容易解决。
import java.util.Scanner;

public class LaptopCostCalculatorV2 {
    public static void main(String[] args) {
        int laptopsPrice = 0;
        int laptopQuantity;

        Scanner scan = new Scanner(System.in);

        System.out.print("How many laptops do you want to buy? ");
        laptopQuantity = scan.nextInt();
        scan.nextLine();
        System.out.print("Do they have different price (yes/no)? ");
        String choose = scan.nextLine();
        if ("yes".equals(choose)) {
            for (int i = 0; i < laptopQuantity; i++) {
                System.out.println("Enter laptop price");
                laptopsPrice += scan.nextInt();
                scan.nextLine();
            }
        } else {
            System.out.println("Enter laptop/s price: ");
            laptopsPrice += scan.nextInt() * laptopQuantity;
            scan.nextLine();
        }
        System.out.println("Total is: " + laptopsPrice);
    }
}