Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让扫描仪读取我的布尔结果?_Java - Fatal编程技术网

Java 如何让扫描仪读取我的布尔结果?

Java 如何让扫描仪读取我的布尔结果?,java,Java,我正在制作一个包含大量布尔真或假语句的程序,我想让扫描仪读取布尔值的输出,并将其作为某种输出,有没有办法做到这一点 例: 基本上,我要求用户输入一个数字,它会将其转换为实际月份的名称,并通过转换数字告诉他们的生日,但当我这样做时,我只是将其打印出来。我想让扫描器先读取输入1,然后将其写为“您的生日是1月1日”您可以使用switch语句,根据输入的数字设置响应字符串,并在打印语句中使用 String response; switch(day){ case 1: response = "fir

我正在制作一个包含大量布尔真或假语句的程序,我想让扫描仪读取布尔值的输出,并将其作为某种输出,有没有办法做到这一点

例:


基本上,我要求用户输入一个数字,它会将其转换为实际月份的名称,并通过转换数字告诉他们的生日,但当我这样做时,我只是将其打印出来。我想让扫描器先读取输入1,然后将其写为“您的生日是1月1日”

您可以使用switch语句,根据输入的数字设置响应字符串,并在打印语句中使用

String response;

switch(day){
  case 1: response = "first";
    break;
  case 2: response = "second";
    break;
  /*
  And so on...
  */
}

尝试以下方法:

public static void main(String[] args) {
    final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    Scanner sc = new Scanner(System.in);
    System.out.println("Type month: ");

    int month = 0;
    while(sc.hasNextInt()) {
        month = sc.nextInt();
        // get month from 1 to 12
        if (month > 0 && month < 12) {
            break;
        } else {
            System.out.println("Type valid month: ");
            continue;
        }
    }

    System.out.println("Type day: ");
    while (sc.hasNextInt()) {
        int day = sc.nextInt();
        int numDays = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31; break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30; break;
            case 2: numDays = 28; break;
        }
        // get day taking under consideration amount of days in the month
        if (day > 0 && day <= numDays) {
            System.out.println("Your birthday is " + day + " " + months[month-1]);
            return;
        } else {
            System.out.println("Type valid day: ");
            continue;
        }
    }
}
publicstaticvoidmain(字符串[]args){
最终字符串[]月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
扫描仪sc=新的扫描仪(System.in);
System.out.println(“类型月份:”);
整月=0;
while(sc.hasnetint()){
月份=sc.nextInt();
//从1月到12月
如果(月>0和月<12){
打破
}否则{
System.out.println(“键入有效月份:”);
继续;
}
}
System.out.println(“键入日期:”);
while(sc.hasnetint()){
int day=sc.nextInt();
int numDays=0;
开关(月){
案例1:
案例3:
案例5:
案例7:
案例8:
案例10:
案例12:
numDays=31;中断;
案例4:
案例6:
案例9:
案例11:
numDays=30;中断;
案例2:numDays=28;中断;
}
//考虑到月内的天数,获取天数
如果(天>0&&D天)
public static void main(String[] args) {
    final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    Scanner sc = new Scanner(System.in);
    System.out.println("Type month: ");

    int month = 0;
    while(sc.hasNextInt()) {
        month = sc.nextInt();
        // get month from 1 to 12
        if (month > 0 && month < 12) {
            break;
        } else {
            System.out.println("Type valid month: ");
            continue;
        }
    }

    System.out.println("Type day: ");
    while (sc.hasNextInt()) {
        int day = sc.nextInt();
        int numDays = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31; break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30; break;
            case 2: numDays = 28; break;
        }
        // get day taking under consideration amount of days in the month
        if (day > 0 && day <= numDays) {
            System.out.println("Your birthday is " + day + " " + months[month-1]);
            return;
        } else {
            System.out.println("Type valid day: ");
            continue;
        }
    }
}