【发布时间】:2025-12-23 15:35:11
【问题描述】:
问题:每当我运行我的代码时,结果总是显示为 false。
我写了一个比较两个值大于10的整数的代码,如果整数上的位数相同,结果应该返回true。
示例:11 和 41 = 真,26 和 45 = 假
但是,每当我运行我的代码时,结果总是错误的。
代码:
import java.util.ArrayList;
class SharedDigit {
public static boolean hasSharedDigit(int number1, int number2){
boolean result = false;
//If the number is a single digit or negative ,return false
if (number1 < 9 || number2 < 9) {
return false;
}
//Get 2 ArrayList for each number
ArrayList<Integer> firstNumberArray = new ArrayList<Integer>();
ArrayList<Integer> secondNumberArray = new ArrayList<Integer>();
//Get each digit of each digit of number1 and store in the array
while (number1 > 0) {
//Get the last digit of the number by modulating by 10
int lastDigit = number1 % 10;
//Assign the reverse value to the ArrayList
firstNumberArray.add(lastDigit);
//Remove the last digit of number1
number1 = number1 / 10;
}
//Get each digit of each digit of number2 and store in the array
while (number2 > 0) {
//Get the last digit of the number by modulating by 10
int lastDigit = number2 % 10;
//Assign the reverse value to the ArrayList
firstNumberArray.add(lastDigit);
//Remove the last digit of number2
number2 = number2 / 10;
}
//Get the highest count of the array and compare each integer to check if they are the same
int highestcount;
int lowestcount;
//If the first number array size is the highest
if (firstNumberArray.size() > secondNumberArray.size()) {
highestcount = firstNumberArray.size();
lowestcount = secondNumberArray.size();
//Compare each array and see if the integer is the same
for (int i = 0; i < highestcount; i++) {
for (int j = 0; j < lowestcount; j++) {
if (firstNumberArray.get(i) == secondNumberArray.get(j)) {
result = true;
}
}
}
}
//If the second number array size is the highest
else if (secondNumberArray.size() > firstNumberArray.size()) {
highestcount = secondNumberArray.size();
lowestcount = firstNumberArray.size();
//Compare each array and see if the integer is the same
for (int i = 0; i < highestcount; i++) {
for (int j = 0; j < lowestcount; j++) {
if (firstNumberArray.get(j) == secondNumberArray.get(i)) {
result = true;
}
}
}
}
//If both array size is the same
else {
highestcount = firstNumberArray.size();
lowestcount = firstNumberArray.size();
//Compare each array and see if the integer is the same
for (int i = 0; i < highestcount; i++) {
for (int j = 0; j < lowestcount; j++) {
if (firstNumberArray.get(i) == secondNumberArray.get(j)) {
result = true;
}
}
}
}
//If no integer is the same, return false
return result;
}
}
public class testing
{
public static void main(String[] args)
{
//Call the class into the main subroutine
SharedDigit test = new SharedDigit();
//Test this two numbers: It should return true as both numbers has 9
boolean testing = test.hasSharedDigit(29, 99);
//Test this two numbers: It should return true as both numbers has 5
boolean testing2 = test.hasSharedDigit(35, 58);
//Test this two numbers: It should return false as one of the numbers is less than 10
boolean testing3 = test.hasSharedDigit(8, 84);
//Test this two numbers: It should return false as both numbers has no same digits
boolean testing4 = test.hasSharedDigit(57, 49);
System.out.println("29 and 99 result is: " + testing);
System.out.println("35 and 58 result is: " + testing2);
System.out.println("8 and 84 result is: " + testing3);
System.out.println("57 and 49 result is: " + testing4);
}
}
输出:
29 and 99 result is: false
35 and 58 result is: false
8 and 84 result is: false
57 and 49 result is: false
感谢任何回复和任何建议以改进我的编码。谢谢!
【问题讨论】:
-
能多给点测试用例吗?在你给出的 2 个中,只有 1 个是需要的,所以有很多假设。
标签: java arraylist numbers compare