- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图以原始顺序返回一个新堆栈,其中包含原始堆栈中的偶数整数,但我的 junit 测试一直说我的堆栈为空,并且不包含正确的值。有人可以看看我的代码并解释我做错了什么吗?我不能使用静态变量、数组或 Java 集合。
我的代码
public class StackExample {
public static Stack<Integer> getEvenNumbers(Stack<Integer> stack) {
Stack<Integer> a = stack;
Stack<Integer> c = new Stack<Integer>();
System.out.println(length(stack));
if(length(stack)==1)
{
return stack;
}
while (!(a.isEmpty())) {
int num = a.pop();
if (num % 2 == 0) {
c.push(num);
} else {
}
}
System.out.println(c.isEmpty());
return c;
}
public static int length(Stack<Integer> a) {
int length = 0;
while (!(a.isEmpty())) {
a.pop();
length++;
}
return length;
}
}
提供的堆栈类
import java.util.ArrayList;
import java.util.List;
public class Stack<T> {
private List<T> array = new ArrayList<T>();
public void push(T value) {
array.add( value );
}
public T pop() {
int last = array.size() - 1;
return array.remove( last );
}
public boolean isEmpty() {
return array.isEmpty();
}
@Override
public String toString() {
return array.toString();
}
}
我的junit测试
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import org.junit.Test;
public class StackExampleTest {
private class StackTest extends Stack<Integer> {
public StackTest(int[] values) {
for (int i = values.length-1; i > -1; i--) {
push( values[ i ] );
}
}
}
@Test
public void testReflection() {
Class<?> iClass = StackExample.class;
for (Field field : iClass.getDeclaredFields()) {
if (!field.isSynthetic()) {
fail( "class should not have any fields" );
}
}
}
@Test
public void testEmpty() {
int[] input = new int[]{ };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );
assertTrue( "result should be empty", result.isEmpty() );
assertTrue( "stack should be empty", stack .isEmpty() );
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void test1Odd() {
int[] input = new int[]{ 5 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );
assertTrue( "result should be empty", result.isEmpty() );
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void test1Even() {
int[] input = new int[]{ 4 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );
for (int expected : new int[]{ 4 }) {
if (result.isEmpty())
fail( "\"result\" empty: '"+ expected +"' expected" );
else {
int actual = result.pop();
assertEquals( "incorrect result", expected, actual );
}
}
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testNoneEven() {
int[] input = new int[]{ 9, 77, 3, 5, 11 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );
assertTrue( "result should be empty", result.isEmpty() );
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testSomeEven() {
int[] input = new int[]{ 44, 77, 8, 3, 5, 12 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );
for (int expected : new int[]{ 44, 8, 12 }) {
if (result.isEmpty())
fail( "\"result\" empty: '"+ expected +"' expected" );
else {
int actual = result.pop();
assertEquals( "incorrect result", expected, actual );
}
}
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testAllEven() {
int[] input = new int[]{ 12, 22, 6, 14, 12 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );
for (int expected : input) {
if (result.isEmpty())
fail( "\"result\" empty: '"+ expected +"' expected" );
else {
int actual = result.pop();
assertEquals( "incorrect result", expected, actual );
}
}
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
}
最佳答案
您的长度函数会从堆栈中删除(弹出)所有元素。
请注意,将对象引用分配给另一个变量,例如
Stack<Integer> a = c
不复制对象 - 仅复制对象的引用。
关于java - 返回偶数堆栈,但堆栈为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21784638/
我有一个包含多个数字的数组: int[] tab = {1,2,3,4}; 我必须创建两个方法,第一个是 sum() 方法,第二个是 numberOdd()。 这一步就OK了! int length
我一直在研究这个简单的待办事项列表,现在正处于调试阶段。当我实现不同的更改时,我观察到以下情况 保留列表中的所有项目:我添加 1 项,没问题。我添加了 2 个项目,但没有任何效果 - 无法标记完整/不
我想仅在未引用的术语中将 | 替换为 OR,例如: "this | that" | "the | other" -> "this | that" OR "the | other" 是的,我可以分割空格
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我在一些练习中得到了这个示例代码,它展示了如何查找整数是奇数还是偶数。 int x = 4; if ( (x & 1) == 0 ) { System.out.prin
我无法制作简单的 JavaScript 来获取文本框中的数字,并在单击按钮时返回结果。 function check() { var v=document.getElem
我是一名初学者,我正在尝试创建一个程序,该程序将生成仅由偶数组成的数字,并四舍五入到最接近的偶数。 例子: 135 --> 200 2700 --> 2800 我有一个代码,但每当我尝试打印它时,数字
我正在尝试编写一个程序,它将接受两个数字并根据它们的值返回两个都是奇数,两个都是偶数,或者 A 是奇数而 B 是偶数,等等。 我设法让程序检查一个变量,但如果我添加第二个变量,我会得到一个我不期望的输
这是一个带有数据和下一个属性的标准链表。 这就是我正在尝试的: class Node { constructor(data, next) { this.data = data;
我有一个正则表达式问题,我不知道该怎么做。它必须匹配开头包含任意数量的 a 的所有字符串,然后如果 a 的数量是偶数则匹配单个 0,如果 a 的数量是奇数则匹配单个 1。 如何跟踪偶数/奇数? 示例
我试图在数组中执行一个循环,对内容进行排序并创建每个具有 2 个值的 div。 尝试了很多东西,但我不知道我需要做什么。 这就是我需要做的:在数组中循环并创建 div。每个 div 应该有 2 个数组
您好,想知道是否有更简单的方法来显示奇数/偶数。我知道我可以做一个 for 循环并加载一个列表。然后我可以写另一个for循环遍历列表并检查值是否为奇数/偶数: for(i=0; i i % 2 ==
我正在尝试改变边距以使图像上下呈之字形。我发现this article很接近,但将一项更改应用于所有 .brochureImg 类。我做错了什么? HTML GP &
我编写这个方法来重新排列两个堆栈的元素,使堆栈 s1 只包含偶数整数,堆栈 s2 只包含奇数整数。并且 s1 或 s2 中不应存储任何零。 public static void rerange(sta
我需要帮助才能将未知整数分成给定数量的偶数部分——或者至少尽可能地均匀。各部分之和应为原值,但各部分应为整数,且应尽可能接近。 参数 num: Integer - 应该被分成相等部分的数字 parts
我在这里想做的是由偶数消费者打印偶数,由奇数消费者打印奇数。 有一个 evenodd 方法,它基本上消耗任何数字并打印(无论是偶数还是奇数)。我有 2 个偶数消费者线程、2 个奇数消费者线程、2 个偶
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
谁能帮忙。我必须编写一个程序,让用户输入 10 个数字。并将这些数字按奇数(升序)和偶数(降序)排序。 例如:输入:2、8、7、13、23、9、34、11、1、16输出:1、7、9、11、13、23、
给定两个列表,我想合并它们,以便第一个列表中的所有元素都是偶数索引(保留它们的顺序),第二个列表中的所有元素都是奇数索引(也保留它们的顺序)。示例如下: x = [0,1,2] y = [3,4] r
我有一个表,对列和行进行求和,并显示求和的结果。我必须改变每个总数的颜色。如果是偶数,则将其设置为“绿色”。如果是奇数,则将其设置为“红色” 这是我的 table :
我是一名优秀的程序员,十分优秀!