java - 扫描仪多输入问题

标签 java eclipse

我是爱尔兰本地理工学院的一年级学生。正如您所猜测的那样,我们正在处理 java,目前我们正在处理扫描仪。我已在底部添加了我的代码,但有点困惑。

当我运行该程序时,它工作正常,直到我进入“输入国家/地区”和“输入类(class)”,并且无论我将扫描仪更改为 f.nextLine 还是 f.next,这两行都会一起打印。

任何人都可以帮助我解决这个问题,因为我错过了一些东西,而且我不知道它是什么。

非常感谢

马修

public static void main(String[] args) 
{
    String name; //declaring string name
    String town; //declaring string town
    double dist; //declaring double distance from AIT the person lives
    int age; //declaring their age 
    double height; //declaring their height
    double weight; //declaring their weight
    String country; //declaring their country that they are from
    String course; //declaring their course that they are taking 
    int years; //declaring the amount of years they are in the course
    String sNum; //declaring there email address string

    Scanner f = new Scanner(System.in);
    //Scanner s = new Scanner(System.in);
    // below is the prompt for all the information and there keyboard input with scanner
    System.out.print("Enter your name here & press enter: "); 
    name = f.nextLine();

    System.out.print("Enter the name of the town or city you live in & press enter: ");
    town = f.nextLine();

    System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
    dist = f.nextDouble();

    System.out.print("Enter your age & press enter: ");
    age = f.nextInt();

    System.out.print("Enter your height & press enter: ");
    height = f.nextDouble();

    System.out.print("Enter your weight & press enter: ");
    weight = f.nextDouble();

    System.out.print("Enter the name of country that you are from & press enter: ");
    country = f.nextLine();

    System.out.print("Enter the name of course you are taking & press enter: ");
    course = f.nextLine();

    System.out.print("Enter the number of years the course is & press enter: ");
    years = f.nextInt();

    System.out.print("Enter your student number & press enter: ");
    sNum = f.next();

    //all the outputs of the information collected
    System.out.println("Your name: "+name+" Town: "+town);
    System.out.println(town+" is "+dist+" away from AIT.");
    System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
    System.out.println("Your studying "+course+" for "+years+" years at AIT.");
    System.out.println("Your student number is "+sNum+" & your student email address is         "+sNum+"@student.ait.ie");
    System.out.println("Please review the information.");
}

最佳答案

nextDouble() 扫描下一个标记并将其解析为 double 型。这就是您面临这个问题的原因。您只需将 nextLine() 移至 nextDouble() 上方即可。这是解决问题的临时方案。

尝试下面的代码。我刚刚编辑了你的代码。

public static void main(String[] args) 
{
    String name; //declaring string name
    String town; //declaring string town
    double dist; //declaring double distance from AIT the person lives
    int age; //declaring their age 
    double height; //declaring their height
    double weight; //declaring their weight
    String country; //declaring their country that they are from
    String course; //declaring their course that they are taking 
    int years; //declaring the amount of years they are in the course
    String sNum; //declaring there email address string

    Scanner f = new Scanner(System.in);
    //Scanner s = new Scanner(System.in);
    // below is the prompt for all the information and there keyboard input with scanner
    System.out.print("Enter your name here & press enter: "); 
    name = f.nextLine();

    System.out.print("Enter the name of the town or city you live in & press enter: ");
    town = f.nextLine();

        System.out.print("Enter the name of country that you are from & press enter: ");
    country = f.nextLine();

    System.out.print("Enter the name of course you are taking & press enter: ");
    course = f.nextLine();

    System.out.print("Enter the number of years the course is & press enter: ");
    years = f.nextInt();

    System.out.print("Enter your student number & press enter: ");
    sNum = f.next();

    System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
    dist = f.nextDouble();

    System.out.print("Enter your age & press enter: ");
    age = f.nextInt();

    System.out.print("Enter your height & press enter: ");
    height = f.nextDouble();

    System.out.print("Enter your weight & press enter: ");
    weight = f.nextDouble();



    //all the outputs of the information collected
    System.out.println("Your name: "+name+" Town: "+town);
    System.out.println(town+" is "+dist+" away from AIT.");
    System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
    System.out.println("Your studying "+course+" for "+years+" years at AIT.");
    System.out.println("Your student number is "+sNum+" & your student email address is         "+sNum+"@student.ait.ie");
    System.out.println("Please review the information.");

    }
    }

关于java - 扫描仪多输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26060395/

相关文章:

java - Paypal Sandbox 支付状态未决

java - 正则表达式替换一个字符或仅替换一个重复的字符

java - 按子类断点过滤?

eclipse - 使用嵌入式 jetty 的 Spring 应用程序启动速度慢

java - 在数组内部使用循环时节点不是交集?

java - 如何在 Java 中生成 min 和 max 之间的随机整数?

java - 在eclipse中编辑xml时如何让Ctrl-F11构建项目?

php - Eclipse 缩进 PHP

eclipse - 使用 jsp 和 session 创建一个简单的登录页面

java - 过载 : why List<String> and List<Integer> make ambiguous declaration?