在Java中,我应该为不同类型的输入使用不同的Scanner实例吗?

我需要从用户那里获得输入.他们开始创造一朵新花.用户告诉我花的名字(String),花的颜色(String),花上的刺数(int)和花的气味(String).我使用单个Scanner命名输入来获取所有这些.但是,它无法正常工作.获得荆棘数后,程序会询问用户花的味道是什么,但不会让我输入答案.但是,我创建了第二个名为input2的Scanner来获取荆棘数,现在它正常工作.这是代码:

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

public class AssignmentTwo {

    static ArrayList<FlowerClass> flowerPack = new ArrayList<FlowerClass>();


    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        while(true){
            System.out.println("1. Add flower to flowerpack.");
            System.out.println("2. Remove flower from the flowerpack.");
            System.out.println("3. Search for a flower in the flowerpack.");
            System.out.println("4. Display the flowers in the flowerpack.");

            int userChoice = input.nextInt();

            switch(userChoice){
            case 1:
                addFlower();
                break;
            case 2:
                //removeFlower();
                break;
            case 3:
                //searchFlower();
                break;
            case 4:
                displayFlowers();
                break;
            case 5:
                System.out.println("Goodbye!");
                System.exit(0);
            }
        }
    }

    public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower's color?");
        String desiredColor = input.nextLine();
        System.out.println("How many thorns does it have?");
        Scanner input2 = new Scanner(System.in);
        int desiredThorns = input2.nextInt();
        System.out.println("What does it smell like?");
        String desiredSmell = input.nextLine();
        flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
    }

    public static void displayFlowers(){

        for (FlowerClass flower: flowerPack){
            System.out.println(flower.getName());
        }
        System.out.println("Number of flowers in pack: " + FlowerClass.numberFlowers());

    }
}

如果您查看我的addFlower()函数,您将看到我创建Scanner input2的部分,并使用input2获取新花所具有的荆棘数的int.之前,我正在使用函数中的第一个Scanner实例来获取荆棘数的输入.但是,它无法正常工作.旧功能看起来像这样:

public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower's color?");
        String desiredColor = input.nextLine();
        System.out.println("How many thorns does it have?");
        int desiredThorns = input.nextInt();
        System.out.println("What does it smell like?");
        String desiredSmell = input.nextLine();
        flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
    }

有没有理由为什么第一个版本的功能不起作用?我设法通过使用新的扫描仪修复它,但我不明白为什么第一个版本不起作用.当您尝试将单个扫描仪用于不同类型的输入时,扫描仪是否会感到困惑?我正在查看The Java Tutorials docs中的Scanning教程,但我没有看到答案.谢谢你的时间!

最佳答案 我认为挂断来自这样一个事实:当你在nextInt()之后调用nextLine()时,未在nextInt()中解析的换行被计为行的结尾,因此扫描器假定你已经完成了.缓解这种情况的一种方法是始终调用nextLine,但将结果String解析为Int.

在大多数情况下,我不认为这样做是个好主意,但在你看来这似乎是有益的.

public static void addFlower(){
    Scanner input = new Scanner(System.in);
    ...
    int desiredThorns = Integer.parseInt(input.nextLine());
    ...
    flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
}
点赞