使用三根刺的处理方法

  • 本文关键字:处理 方法 三根 java
  • 更新时间 :
  • 英文 :


根据参数x的值,编写一个Processing方法返回三个字符串中的一个。如果x是偶数,则该方法应返回"偶数"。如果x可被三整除,则该方法应返回"被三整整除"。如果x既不是偶数也不能被三整除,则该方法应返回"Just odd"。方法的签名应该是String evenOdd(int x(

您在注释中留下的代码的问题是,他们要求数字可以被三整除,即0的余数。您的代码正在尝试查找3的余数。

所以,与其写if (x % 3 == 3),不如写if (x % 3 == 0)

基本上,你的完整代码如下所示:

string evenOdd(int x)
{
string theResponse = "";
if (x % 2 == 0) // if x is divisible by 2 with no remainders
{
theResponse = "Even!";
}
else if (x % 3 == 0) // if x is divisible by 3 with no remainders
{
theResponse = "By three!";
}
else
{
theResponse = "Odd";
}
return theResponse;
}
String evenOdd(int x)
{
if (x%2 == 0) //number is even when it is divisible by 2
return "Even";
else if(x%3 == 0) //if remainder is 0 then it is divisible by three
return "By three";
else
return "Just Odd";
}

最新更新