Java 我想在ecllipse中运行此代码,它在codingBat.com上运行良好
给定一个整数数组,如果值3在数组中正好出现3次,并且没有3个相邻,则返回trueJava 我想在ecllipse中运行此代码,它在codingBat.com上运行良好,java,arrays,eclipse,Java,Arrays,Eclipse,给定一个整数数组,如果值3在数组中正好出现3次,并且没有3个相邻,则返回true haveThree({3, 1, 3, 1, 3}) → true haveThree({3, 1, 3, 3}) → false haveThree({3, 4, 3, 3, 4}) → false public boolean haveThree(int[]nums) { 整数计数=0; 布尔值isPerv3=false; 对于(int i=0;i
haveThree({3, 1, 3, 1, 3}) → true
haveThree({3, 1, 3, 3}) → false
haveThree({3, 4, 3, 3, 4}) → false
public boolean haveThree(int[]nums)
{
整数计数=0;
布尔值isPerv3=false;
对于(int i=0;iListIterator
对象将非常适合这种情况。通过这种方式,您可以将列表中的下一个值与上一个值进行比较。您可以保留一个计数器来检查是否有三个连续的3
import java.util.ArrayList;
import java.util.ListIterator;
public class Test {
public static void main(String[] args) {
int[] test1 = { 3, 2, 5, 11, 11, 11, 233, 22, 3, 3, 3, 5, 5, 5 }; // false
int[] test2 = { 3, 3, 3, 8, 3, 11, 233, 22, 3, 3, 3, 5, 5, 5 }; // false
int[] test3 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 4, 3, 3, 3 }; // false
int[] test4 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 5, 5, 5 }; // true
System.out.println(haveThree(test1));
System.out.println(haveThree(test2));
System.out.println(haveThree(test3));
System.out.println(haveThree(test4));
}
public static boolean haveThree(int[] nums) {
int totalNumberOfThrees = 0;
int threeInSuccessionCount = 0;
boolean result = false;
// convert to ArrayList
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
arrayList.add(nums[i]);
}
// use ListIterator to compare next value with previous value
ListIterator<Integer> iterator = arrayList.listIterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
Integer previous = iterator.previous();
// counts total number of 3's
if (next.intValue() == 3) {
totalNumberOfThrees++;
}
// if next and previous values == 3 then increase count by 1
if (next.intValue() == 3 && previous.intValue() == 3) {
threeInSuccessionCount++;
}
// if next and previous values != 3 then reset counter
else {
threeInSuccessionCount = 0;
}
// if there are three consecutive 3's set threeInSuccessionCount == 3
// and then break from loop
if (threeInSuccessionCount == 3) {
break;
}
iterator.next();
}
if (threeInSuccessionCount != 3 && totalNumberOfThrees >= 3) {
result = true;
} else {
result = false;
}
return result;
}
}
编辑:
要回答您的问题“如果不是声明的静态,如何调用(在主方法中)haveThree()”,您可以在主方法中声明一个新的Test实例,然后使用该实例调用haveThree()
像这样:
public static void main(String[] args) {
int[] test1 = { 3, 2, 5, 11, 11, 11, 233, 22, 3, 3, 3, 5, 5, 5 };
int[] test2 = { 3, 3, 3, 8, 3, 11, 233, 22, 3, 3, 3, 5, 5, 5 };
int[] test3 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 4, 3, 3, 3 };
int[] test4 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 5, 5, 5 };
Test test = new Test();
System.out.println(test.haveThree(test1));
System.out.println(test.haveThree(test2));
System.out.println(test.haveThree(test3));
System.out.println(test.haveThree(test4));
}
那么,你真正的问题是什么呢?如果你复制/粘贴了你发布到eclipse中的代码,单击“运行”,并获得了预期的结果,那么请参考“嗨,我是这个网站的新用户,我不知道如何进行投票或勾选”。当我尝试投票时,它表示你不能投票自己的帖子。我是一个求职者,正在寻找java j2EE中的运营商。我应该如何进行逻辑上的选择va strong.每次我去面试的时候,我都发现有一个程序是不同的,而且非常困难,我无法解决这个问题..如何使核心java更强大。。
false
false
false
true
public static void main(String[] args) {
int[] test1 = { 3, 2, 5, 11, 11, 11, 233, 22, 3, 3, 3, 5, 5, 5 };
int[] test2 = { 3, 3, 3, 8, 3, 11, 233, 22, 3, 3, 3, 5, 5, 5 };
int[] test3 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 4, 3, 3, 3 };
int[] test4 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 5, 5, 5 };
Test test = new Test();
System.out.println(test.haveThree(test1));
System.out.println(test.haveThree(test2));
System.out.println(test.haveThree(test3));
System.out.println(test.haveThree(test4));
}