JUSTHISS 2021-12-12 11:14 采纳率: 57.1%
浏览 152
已结题

Java有关判断的问题

  1. 向用户请求输入。
  2. 确定输入的数据类型是byte、short、int、long、float和/或double。
  3. 程序应该一直等待用户输入,直到用户输入“退出”(忽略大小写)。

    img

  • 写回答

2条回答 默认 最新

  • soar3033 2021-12-12 19:38
    关注

    img

    
    import java.util.Scanner;
    
    import System;
    
    public class test {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();
            sc.close();
            String[] strs = str.split(" ");
            for (int i = 0; i < strs.length; i++) {
                try {
                    Integer tmp = Integer.valueOf(strs[i]);
                    if (tmp >= 0 && tmp <= 255) {
                        java.lang.System.out.println(strs[i] + " can be converted into a type byte!");
                    } else {
                        java.lang.System.out.println(strs[i] + " cannot be converted into a type byte!");
                    }
                } catch (Exception e) {
                    java.lang.System.out.println(strs[i] + " cannot be converted into a type byte!");
                }
    
                try {
                    Short tmp = Short.parseShort(strs[i]);
                    java.lang.System.out.println(strs[i] + " can be converted into a type short!");
                } catch (Exception e) {
                    java.lang.System.out.println(strs[i] + " cannot be converted into a type short!");
                }
    
                try {
                    Integer tmp = Integer.valueOf(strs[i]);
                    java.lang.System.out.println(strs[i] + " can be converted into a type int!");
                } catch (Exception e) {
                    java.lang.System.out.println(strs[i] + " cannot be converted into a type int!");
                }
    
                try {
                    Float tmp = Float.parseFloat(strs[i]);
                    java.lang.System.out.println(strs[i] + " can be converted into a type float!");
                } catch (Exception e) {
                    java.lang.System.out.println(strs[i] + " cannot be converted into a type float!");
                }
    
                try {
                    Double tmp = Double.valueOf(strs[i]);
                    java.lang.System.out.println(strs[i] + " can be converted into a type double!");
                } catch (Exception e) {
                    java.lang.System.out.println(strs[i] + " cannot be converted into a type double!");
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 12月12日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,