首页 > 解决方案 > 给定一个 int 变量状态,编写一个 switch 语句,根据状态单独打印出上述列表中的适当标签

问题描述

HTTP 是管理 Web 服务器和 Web 客户端(即浏览器)之间通信的协议。协议的一部分包括服务器返回的状态代码,用于告诉浏览器其最近的页面请求的状态。下面列出了一些代码及其含义:

200,OK(已完成) 403,禁止 404,未找到 500,服务器错误

给定一个 int 变量状态,编写一个 switch 语句,根据状态单独打印出上述列表中的适当标签。

这就是我的代码,但它仍然无法正常工作,我不确定为什么。

switch ( status ){
case 200: System.out.println("OK(fulfilled)");
break;
case 403: System.out.println("forbidden");
break;
case 404: System.out.println("not found");
break;
case 500: System.out.println("server error");
break;

}

我得到的错误是“_stdout 的值不正确”。

标签: java

解决方案


你能发布你的整个源代码吗?您是否尝试过使用如下所示的默认情况?

public void run() {

    //enter a test case here to see if your console prints the output:
    int status = readInt("Enter case here: ");


    switch ( status ){
    case 200: System.out.println("OK(fulfilled)");
    break;
    case 403: System.out.println("forbidden");
    break;
    case 404: System.out.println("not found");
    break;
    case 500: System.out.println("server error");
    break;

//Try the default case here:
    default: 
        break;
    }

}

}

推荐阅读