我是Java的初学者我可以用另一种方式编写这段代码吗? [英] I Am A Beginner In Java Can I Write This Code In Another Way.?

查看:40
本文介绍了我是Java的初学者我可以用另一种方式编写这段代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if (x% 100  ==  11  || x% 100  ==  22  || x % 100  ==  33  || x% 100  ==  44  
|| x% 100 == 55 || x% 100 == 66 || x%< span class =code-digit> 100 == 77
|| x% 100 == 88 || x% 100 == 99 || x% 100 == 00 ){

return true;
}

解决方案

(我不懂Java。)



怎么样:



<前lang =java> 返回(x%< span class =code-digit> 100 == 11 || x% 100 == 22 || x% 100 == 33 || x% 100 == 44
|| x% 100 == 55 || x% 100 == 66 || x% 100 == 77
|| x% 100 == 88 || x% 100 == 99 || x% 100 == 00 );





您可以使用本地变量来保存 x%100 并保存一些打字。

我想你可以使用开关或某种查找表。



另外,考虑一下:



  int  x =  455 ; 
int x1 = x% 10 ;
int x10 = x / 10 10 ;

bool b = x1 == x10;





所以,把它们放在一起:



  public   boolean  doSomeCheck( int  x){ return (x% 10 )==(x /  10  10 ); } 


  int  tempINT = x% 100 ; 
if (tempINT% 11 == 0
|| tempINT == 0
{
return true ;
}


除了其他答案,你想知道最后两位数可以被11整除:



  boolean  lastTwoEleven( int  x) 
{
// mod 100给出最后两位数字& mod 11检查是否为divisble
return ((x% 100 )%< span class =code-digit> 11 )== 0 ;
}


if (x % 100 == 11 || x % 100 == 22 || x % 100 == 33 || x % 100 == 44
				|| x % 100 == 55 || x % 100 == 66 || x % 100 == 77
				|| x % 100 == 88 || x % 100 == 99 || x % 100 == 00) {

			return true;
		}

解决方案

(I don't know Java.)

How about:

return (x % 100 == 11 || x % 100 == 22 || x % 100 == 33 || x % 100 == 44
  || x % 100 == 55 || x % 100 == 66 || x % 100 == 77
  || x % 100 == 88 || x % 100 == 99 || x % 100 == 00) ;



You could use a local variable to hold x % 100 and save some typing.
I suppose you could use a switch or some sort of lookup table.

Also, consider this:

int x = 455 ;
int x1 = x % 10 ;
int x10 = x / 10 % 10 ;

bool b = x1 == x10 ;



So, to put that all together:

public boolean doSomeCheck(int x){ return ( x % 10 ) == ( x / 10 % 10 ) ; }


int tempINT = x % 100;
            if (tempINT % 11 == 0
                || tempINT == 0)
            {
                return true;
            }


Further to other answers, you want to know that the last two digits are divisible by 11:

boolean lastTwoEleven(int x)
{
    // mod 100 gives last two digits & mod 11 checks if divisble
    return ((x % 100) % 11) == 0;
}


这篇关于我是Java的初学者我可以用另一种方式编写这段代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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