我知道这已经被问死了,但我还没有搜索并找到一个和我很相似的案例,所以我想我会问......我这里有这个小代码......
System.out.print("What would you like the name of your new recipe to be? ");
Recipe tempRecipe = new Recipe(name);
name = scan.nextLine();
scan.nextLine();
tempRecipe.setName(name);
System.out.print("How many ingredients does this recipe have? ");
ingredientCount = scan.nextInt();
scan.nextLine();
现在很明显我遇到了 println 语句在同一行并且不允许输入的问题,所以我投入了那个 scan.nextLine() 来解决这个问题。但现在的问题是,当我读到其中的内容时,因为 nextLine() 太多而让我空白!如果我将其更改为 name = scan.next() 我只能读一个单词,并且如果需要,我需要能够不分青红皂白地读 1 个或 2 个单词,这也可能有助于知道遵循此代码是这些线条
Ingredient tempIngredient = new Ingredient(name, quantity, null);
System.out.print("Enter the name of ingredient number " + (i+1) + ": ");
name = scan.nextLine();
tempIngredient.setName(name);
System.out.print("Enter the quantity of ingredient number " + (i+1) + ": ");
quantity = scan.nextDouble();
scan.nextLine();
tempIngredient.setQuantity(quantity);
System.out.print("Enter the unit of measurement of ingredient number " + (i+1) + ": ");
unit = scan.nextLine();
tempIngredient.setUnit(UnitOfMeasurement.valueOf(unit));
如果需要,tempRecipes 名称和 tempIngredients 名称都需要能够容纳 1 或 2 个单词,我该如何在解决 nextLine() 问题的同时做到这一点!?