给定一个字符串,确定它是否是整数。例如,字符串“123”是整数,但字符串“hello”不是。 (Codehs) [英] Given a string, determine if it is an integer. For example the string “123” is an integer, but the string “hello” is not. (Codehs)

查看:119
本文介绍了给定一个字符串,确定它是否是整数。例如,字符串“123”是整数,但字符串“hello”不是。 (Codehs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用名为codehs的网站学习Java,但我坚持这个问题。它希望我创建一个名为isInteger的方法来确定字符串是否为整数,如果是,则返回true,否则返回false。知道我怎么能这样做吗?我已经尝试了很多东西,但似乎没有任何工作,我不能只做



System.out.println(Character.isDigit('1') );



因为它要我创建一个名为isInteger的方法来执行这个任务,任何想法?



我尝试过:



I've recently started to learn Java using a website called codehs but I'm stuck on this problem. It wants me to create a method called isInteger to determine if a string is an integer or not and return true if it is or false if it's not. Any idea how I can do this? I've tried a lot of stuff but nothing seems to work and I can't just do

System.out.println(Character.isDigit('1'));

because it wants me to create a method called isInteger to perform this task, any ideas?

What I have tried:

public class Scratchpad extends ConsoleProgram
{
    public void run()
    {
        // Add your own tests here
        System.out.println(isInteger(str));
    }

    // Copy and paste your Unit Test method here
    public boolean isInteger(String str)
    {
    
        if(Character.isDigit('1'))
        {
            return true;
        }
        else
        {
            return false;   
        }
    }

}

推荐答案

检查值是否为整数



To check value is Integer or not

public boolean isInteger(String number ){
    try{
        Integer.parseInt(number);
    }catch(Exception e ){
        return false;
    }
    return true;
}


我运行的代码除了其中一种情况外都适用。事实证明,他们测试的输入之一输入不正确。其中一个应该是1O1(其中一个是数字而另一个是大写字母O),但是它们将其键入为101,它们全部为零,并且不会通过它们的分级窗口。您可以在窗口中看到它们的输入方式的区别,并知道它应该是什么。

我的代码适用于其他一切:



public boolean isInteger(String str)// char is String

{

for(int i = 0; i< str.length(); i ++)

{

if(Character) .isDigit(str.charAt(i)))

返回true;

}

返回false;

}



对于这个人来说有点晚了但是当他们遇到它时可能会帮助别人。
I ran a code that worked for all but one of their situations. It turns out that one of the inputs they test is typed incorrectly. One of them should be "1O1" (the ones are numbers and the other is a capital O) but they typed it up as "101" where they are all zeros and it doesn't pass their grading window. You can see the difference in the window at how they are typed and know what it should be.
My code that worked for everything else is:

public boolean isInteger(String str) //char was String
{
for(int i = 0; i < str.length(); i++)
{
if(Character.isDigit(str.charAt(i)))
return true;
}
return false;
}

A little late for this individual but may help others as they come across it.


你的代码正常工作。 codeHS检查码功能是不是让你通过?如果是这样,他们在描述中要求的任何规格是什么?
Your code works correctly. Is codeHS checkcode function not letting you pass? If so, are their any specifications they request for in the description?


这篇关于给定一个字符串,确定它是否是整数。例如,字符串“123”是整数,但字符串“hello”不是。 (Codehs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆