How do I split an indice of an array into two different pices, I.E to validate that neither number is odd/even?

huangapple go评论80阅读模式
英文:

How do I split an indice of an array into two different pices, I.E to validate that neither number is odd/even?

问题

我必须检查 roomNumber[i],如果这两个数字(假设是两位数)都不是偶数,也不是奇数,则才能接受这个数字。还需要判断第二位数字是否不小于第一位数字的两倍。我猜我需要将它们分割成两个不同的整数(a、b),但我不太确定。

英文:

I have to check, roomNumber[i], if BOTH numbers (assuming it's two-digit) are not even, and not odd. Only then can I accept the number. Also need to determine if the second digit is not less than double the first. I assume that I need to some how split these into two different ints (a,b), but I'm not sure.

答案1

得分: 1

如果你假设只有两位数,可以按照以下方式处理:

int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
  int second = num % 10;
  int first = (num - second) / 10;

  System.out.println("first: " + first + " second: " + second);
}

你可以使用取模(%)运算符来获取第二个数字。要获取第一个数字,你需要减去第二个数字然后除以 10。

现在你可以进一步处理这些数字。

英文:

If you assume that you have only two-digit numbers, you can go with it this way:

int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
  int second = num % 10;
  int first = (num - second) / 10;

  System.out.println("first: " + first + " second: " + second);
}

You take the second number using the modulo (%) operator. To get the first number, you need to subtract the second number and divide it by 10.

Now you can process those numbers further.

答案2

得分: 0

你的问题与数组无关。我们来重新表述一下:

如何检测一个数字是否为偶数?

你可以搜索一下,很容易就能找到答案。提示:使用取模运算符(%)。

如何判断第二位数字是否不小于第一位数字的两倍?

将其分解成较小的部分:首先,你需要将第一位和第二位数字分开。如果你不知道怎么做,同样可以搜索一下(提示:这与第一个问题非常相似。你可以使用取模运算符和除法(/))。

然后,你需要根据规则进行比较。尝试自己写点代码,看看你能想出什么来。

英文:

Your questions have nothing to do with arrays. Let's reword them:

> How do I test if a number is even?

You can google this and easily find the answer. Hint: use the modulus operator (%).

> How do I test if the second digit is not less than double the first digit

Break this into smaller pieces: First you need to separate out the first and second digit. Again, you can google this if you don't know. (Hint: it's very similar to the first question. You can use the % operator and division (/).

Then you have to compare these according to the rule. Try to write something on your own and see what you can come up with.

huangapple
  • 本文由 发表于 2020年10月12日 13:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64312141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定