英文:
Checking if two three digit numbers have any of there digits that are the same in Java
问题
我正在尝试用Java编写一个程序,检查两个数字中至少有一个数字的一位与另一个数字的一位匹配。例如,如果这两个数字分别是238和345,它应该返回一个特定的字符串。
到目前为止,这是我拥有的代码,但它只对最右边的数字有效:
if ((computerGuess/10) == (userGuess/10) || (computerGuess%10) == (userGuess%10) || (computerGuess%10) == (userGuess/10) || (userGuess%10) == (computerGuess/10)) {
System.out.println("You have won S1000");
break;
}
英文:
I'm trying to write a program in Java that checks if at least one digit of one of the numbers, matches one digit of the other number. For example, if the two numbers are 238 and 345 it should return a certain string. <br>So far this is what I have, but it only works properly for the right most digit:
if ((computerGuess/10) == (userGuess/10) || (computerGuess%10) == (userGuess%10) || (computerGuess%10) == (userGuess/10) || (userGuess%10) == (computerGuess/10)) {
System.out.println("You have won S1000");
break;
}
答案1
得分: 2
Sure, here is the translated code:
一个干净的解决方案是将每个数字转换为一组数字,然后检查是否有重叠:
public Set<Integer> getDigits(int input) {
Set<Integer> digits = new HashSet<>();
while (input > 0) {
digits.add(input % 10);
input /= 10;
}
return digits;
}
int computerGuess = 238;
int userGuess = 345;
Set<Integer> computerDigits = getDigits(computerGuess);
Set<Integer> userDigits = getDigits(userGuess);
computerDigits.retainAll(userDigits);
if (computerDigits.size() > 0) {
System.out.println("存在重叠: " + computerDigits.toString());
}
英文:
One clean solution would be to convert each number into a set of digits, and then check for overlap:
public Set<Integer> getDigits (int input) {
Set<Integer> digits = new HashSet<>();
while (input > 0) {
digits.add(input % 10);
input /= 10;
}
return digits;
}
int computerGuess = 238;
int userGuess = 345;
Set<Integer> computerDigits = getDigits(computerGuess);
Set<Integer> userDigits = getDigits(userGuess);
computerDigits.retainAll(userDigits);
if (computerDigits.size() > 0) {
System.out.println("There is an overlap: " + computerDigits.toString());
}
答案2
得分: 0
原因始终是您在使用"10",无论是'/10'还是'%10'。
最佳解决方案是将它们转换为字符串,然后检查子字符串。
String computerGuessStr = new String(computerGuess);
String userGuess = new String(userGuess);
for (int i = 0; i < computerGuessStr.length(); i++) {
char computerChar = computerGuessStr.charAt(i);
if (userGuess.contains(String.valueOf(computerChar))) {
//做一些操作
} else {
//做一些操作
}
}
英文:
The reason is always you are using "10", either '/10' or '%10'.
The best solution would convert them into string, and check for sub-string.
String computerGuessStr = new String (computerGuess);
String userGuess = new String (userGuess);
for (int i=0;i<computerGuessStr.length();i++){
char computerChar = computerGuessStr.getCharAt(i);
if(userGuess.contains(computerChar)){
//do something
} else {
//do something
}
答案3
得分: 0
这样可以吗?
int computerNumber = 543;
int guess = 238;
List<Integer> numbers = Stream.of(10, 100, 1000).map(n -> guess % n).collect(Collectors.toList());
Stream.of(10,100,1000).map(n -> computerNumber % n).anyMatch(num -> numbers.contains(num));
英文:
Something like this?
int computerNumber = 543;
int guess = 238;
List<Integer> numbers = Stream.of(10, 100, 1000).map(n -> guess % n).collect(Collectors.toList());
Stream.of(10,100,1000).map(n -> computerNumber % n).anyMatch(num -> numbers.contains(num));
答案4
得分: 0
另一种选择是使用普通的循环。这将适用于任何长度的数字。
boolean digitMatch(int num1, int num2)
{
for (; num1 > 0; num1 /= 10) {
// 从num1获取一个数字
int digit1 = num1 % 10;
for (int n2 = num2; n2 > 0; n2 /= 10) {
// 从num2获取一个数字
int digit2 = n2 % 10;
// 比较
if (digit1 == digit2) return true;
}
}
return false;
}
注意:对于负数,您需要添加一些额外的代码。
英文:
Another option is the plain old loop. This will work with any length number.
boolean digitMatch(int num1, int num2)
{
for ( ; num1 > 0; num1 /= 10) {
// Get a digit from num1
int digit1 = num1 % 10;
for (int n2 = num2; n2 > 0; n2 /= 10) {
// Get a digit from num2
int digit2 = n2 % 10;
// compare
if (digit1 == digit2) return true;
}
}
return false;
}
Note: you need to add a little extra code for negative numbers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论