首页 > 编程语言 >Java: switch lambda-like syntax

Java: switch lambda-like syntax

时间:2023-04-09 17:44:31浏览次数:47  
标签:case Java Scanner System next switch result new syntax

 

The switch expression has an additional lambda-like syntax and it can be used not only as a statement, but also as an expression that evaluates to a single value.

 

With the new lambda-like syntax, if a label is matched, then only the expression or statement to the right of the arrow is executed; there is no fall through

package com.example.prom;

import java.util.Scanner;

public class D {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();
        byte result = switch (next) {
            case "A" -> 1;
            case "B" -> 2;
            default -> throw new IllegalStateException("Unexpected value: " + next);
        };
        System.out.println("result = " + result);
    }
}

 

package com.example.prom;

import java.util.Scanner;

public class D {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();
        int result;

        switch (next) {
            case "A" -> result = 1;
            case "B" -> result = 2;
            case "C" -> {
                result = 3;
                System.out.println("3!");
            }
            default -> {
                System.err.println("Unexpected value: " + next);
                result = -1;
            }
        }
        System.out.println("result = " + result);
    }
}

 

yield In the situation when a block is needed in a case, yield can be used to return a value from it

package com.example.prom;

import java.util.Scanner;

public class D {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();

        var result = switch (next) {
            case "A" -> 1;
            case "B" -> 2;
            case "C", "D", "E" -> {
                System.out.println("3!");
                yield 3;  // return
            }
            default -> throw new IllegalStateException("Unexpected value: " + next);
        };
        System.out.println("result = " + result);
    }
}

 

    protected double calculator(char operator, double x, double y) {
        return switch (operator) {
            case '+' -> x + y;
            case '-' -> x - y;
            case '*' -> x * y;
            case '/' -> {
                if (y == 0)
                    throw new IllegalArgumentException("Can't divide by 0");
                yield x / y;
            }
            default -> throw new IllegalArgumentException("Unknown operator `%s`".formatted(operator));
        };
    }

 

标签:case,Java,Scanner,System,next,switch,result,new,syntax
From: https://www.cnblogs.com/dissipate/p/17300678.html

相关文章

  • JavaWeb-24课-filter-2023-04-09
    Servlet类,没有乱码处理packagecom.feijian.servlet;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;public......
  • Golang与Java全方位对比总结
    本文针对Golang与Java的基础语法、结构体函数、异常处理、并发编程及垃圾回收、资源消耗等各方面的差异进行对比总结,有不准确、不到位的地方还请大家不吝赐教。一、基础语法Golang:编码风格及可见域规则严格且简单;Java:来说层次接口清晰、规范,主要表现有以下这些。1、变量......
  • leetcode56.合并区间-java
    1classSolution{2publicint[][]merge(int[][]intervals){3/*4思路:左区间排序,若intervals[i][0]>=intervals[i-1][1];则重叠5将重叠区间新建放入res数组里,没重叠则放入原数组6*/7List<int[]>......
  • javaEE进阶小结与回顾(三)
    集合概述集合是Java中存储引用数据类型的一种容器特点大小不固定,长度可以动态变化,适合做数据的增删如果集合不声明泛型,可以存入任意引用数据类型的数据(不推荐)声明集合时,通过泛型可以指定集合中应该存储什么类型的元素(推荐)注意:需要存储基本数据类型时,使用其......
  • JavaWeb-JSP-JSTL c foreach -2023-04-09
    <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%><%@pageimport="java.util.ArrayList"%><%@pagecontentType="text/html;charset=UTF-8"language="java"%><html>&l......
  • JavaWeb-JSP JSTL标签 -2023-04-09
    <%--CreatedbyIntelliJIDEA.User:AdministratorDate:2023/4/9Time:15:10TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java"%>&l......
  • 【Java 并发】【十】【JUC数据结构】【三】LinkedBlockingQueue阻塞队列原理
    1 前言这节我们就来看看LinkedBlockingQueue内部实现的原理。2 LinkedBlockingQueue的使用在看原理之前我们先来用一用LinkedBlockingQueue,来体验一下:2.1  插入数据publicclassLinkedBlockingQueueTest{publicstaticvoidmain(String[]args)throwsInter......
  • java基础-异常
    1.简介如果某个方法不能按照正常的途径完成任务,在这种情况下会抛出一个封装了错误信息的对象,此时这个方法会立刻退出同时不返回任何值,调用这个方法的其他代码也无法继续执行,异常处理机制会将代码执行交给异常处理器自定义异常的话是继承一个异常类,通常是RumtimeException或者E......
  • 【Java】FileInputStream和FileOutputStream基本使用
    文章目录InputStream-字节输入流1.FileInputStream介绍2.FileOutputStream介绍3.文件的拷贝IO流-体系图文件VS流InputStream-字节输入流InputStream抽象类是所有类字节输入流的超类InputStream常用的子类FileInputStream:文件输入流BufferedInputStream:缓冲字节输入流O......
  • 【Java】FileReader和FileWrite基本使用
    文章目录1.FileReader和FileWriter是字符流,即按照字符来操作IO2.FileWriter常用方法3.案例演示1.FileReader和FileWriter是字符流,即按照字符来操作IOFileReader相关方法:newFileReader(File/String)read:每次读取单个字符,返回该字符,如果到文件末尾返回-13)read(char[):批量......