可以有多个扫描仪吗? Java的

时间:2017-11-21 23:22:00

标签: java

为什么我必须删除/取消input.close();以允许下一次扫描参与?第二个结束函数inputtwo.close();是否都关闭了?

对不起我的坏话。我真的很擅长这个东西。

import java.util.Scanner;
public class FieldTesting {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in); 
    System.out.println("Enter the month number (1 - 12): ");
    int month = input.nextInt();

    String monthString;

    switch (month) {
    case 1:
        monthString = "January";
        break;
    case 2:
        monthString = "February";
        break;
                //etc...
    default:
        monthString = "Invalid month";
        break;
    }

    System.out.println(monthString);
    /* Why must I delete this close statement?
    input.close();
    */
    Scanner inputtwo = new Scanner(System.in);

    System.out.println("Start or Stop? ");
    String command = inputtwo.nextLine();

    switch (command) {
    case "start":
        System.out.println("Machine Started");
        break;
    case "stop":
        System.out.println("Machine Stopped");
        break;
    default:
        System.out.println("Not a command.");
        break;
    }
    input2.close();
}
}

0 个答案:

没有答案