题解 | #有容乃大#

有容乃大

http://www.nowcoder.com/practice/4712159bbc15417086d40d6c6ff94fee

通过封装一个printByte函数来实现输出不同的基本数据类型对应的字节大小

public class Main {
    public static void main(String[] args) {
        printByte("short");
        printByte("int");
        printByte("long");
        printByte("long long");
    }
    
    public static void printByte(String s) {
        int i = 0;
        switch (s){
            case "byte": case "boolean":
                i=1;
                break;
            case "char": case "short":
                i=2;
                break;
            case "int": case "float":
                i=4;
                break;
            case "long": case "long"+" "+"long": case "double":
                i=8;
                break;
        }
        System.out.printf("The size of %s is %d bytes.%n",s,i);
    }
}
全部评论

相关推荐

评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务