本文介绍了检查,看看一个字符串是一个整数 - 的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have found several links related to my question, but they were not exactly what I was looking for. I would prefer to use a simple boolean than to learn to use the try/catch thing. Anyways, here is my question:

I want to set a boolean to true or false depending on if a string is an integer >= 10 and <= 100. So my objective is to set the boolean "answer":

String junk;
if (junk is an integer between 10 and 100){
return true;}
else{
return false;}


if junk = 50, its true.
if junk = fifty, its false.
if junk = 50:-p, its false.
if junk = 5, its false.
if junk = 500, its false.

Thanks!

解决方案
 static boolean isIntegerBetween10And100(String junk){
     try{
        int x = Integer.parseInt(junk);
        return x >= 10 && x <= 100;
     }
     catch(NumberFormatException e){
        return false;
     }
  }

这篇关于检查,看看一个字符串是一个整数 - 的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:29