简体   繁体   English

如何通过System.out.println在控制台上不显示任何内容?

[英]How to show nothing on the console via System.out.println?

Here is a question about Stack on StackOverflow. 这是有关StackOverflow上的Stack的问题。

My question might seem very very vague but if you check my program which I have written then you might understand what I am trying to ask. 我的问题似乎非常模糊,但是如果您检查我编写的程序,那么您可能会明白我要问的问题。 I have implemented the stack myself. 我自己实现了堆栈。 I present the user with 3 choices. 我给用户提供3个选择。 Push, Pop and View the stack. 推送,弹出和查看堆栈。 When view(display) method is called then bunch of 0s show instead of nothing. 当调用view(display)方法时,显示的是一堆0,而不是没有显示。 We know the stack contains nothing unless we put something on it. 我们知道堆栈不包含任何东西,除非我们在上面放了东西。 But since my implemented stack is stack of integers using array, the display method when called shows bunch of 0s(the default values of integers in the array). 但是由于我实现的堆栈是使用数组的整数堆栈,因此调用display方法时会显示一堆0(数组中整数的默认值)。 How do I show nothing instead of 0s. 我如何不显示任何数字而不是0。 I know I can add ASCII for whitespace character but I think it would still violate the rule of stack(Stack should be empty when there is not element, not even code for whitespace). 我知道我可以为空格字符添加ASCII,但是我认为它仍然会违反堆栈规则(当没有元素时,堆栈应该为空,甚至没有空格代码)。

Here is my program: 这是我的程序:

import java.util.Scanner;
public class StackClass
  {

public static void main(String []args)
{

    Scanner input=new Scanner(System.in);
    int choice=0;
    int push;
    Stack stack=new Stack();

    do
    {
        System.out.println("Please select a stack operation:\n1. Press 1 for adding to stack\n2. Press 2 for removing elements from stack\n3. View the stack");
        choice=input.nextInt();

        switch(choice)
            {
                case 1:
                        System.out.println("Please enter the number that you want to store to stack");
                        push=input.nextInt();
                        stack.push(push);

                case 2:
                        stack.pop();
                case 3:
                        stack.display();
            }



    }
    while((choice==1)||(choice==2)||(choice==3));

}    
}
class Stack

{

    private int size;
    private int[] stackPlaces=new int[15];
    private int stackIndex;

    Stack()
    {
        this.size=0;
        this.stackIndex=0;
    }

    public void push(int push)
    {
        if(size<15)
        {
            stackPlaces[stackIndex]=push;
            size++;
            stackIndex++;
        }
        else
        {
            System.out.println("The stack is already full. Pop some elements and then try again");
        }
    }
    public void pop()
    {
        if(size==0)
        {
        System.out.println("The stack is already empty");
        }
        else
        {
        stackPlaces[stackIndex]=0;
        size--;
        stackIndex--;
        }
    }
    public void display()
    {
        System.out.println("The stack contains:");
        for(int i=0;i<stackPlaces.length-1;i++)
            {
            System.out.println(stackPlaces[i]);
            }
    }

}

In display() , simply change your loop to use size for the loop condition, so that you display the logical number of elements: display() ,只需更改循环以将size用于循环条件,即可显示逻辑数量的元素:

for (int i=0;i < size; i++)
{
    System.out.println(stackPlaces[i]);
}

Note that your existing loop was only showing 14 of the 15 values, too... 请注意,您现有的循环也只显示了15个值中的14个。

You initialize an array of int-s, of size 15. The int datatype defaults to 0 (as opposed to its wrapper class Integer which defaults to null), so what you are really doing is creating an int array with 15 0's. 您初始化一个大小为15的int -s数组。int数据类型默认为0(与其包装器类Integer的默认值为null相对),因此您真正要做的是创建一个15的int数组。 So, when you loop through the array and print its content, you will get, well, 15 0's. 因此,当您遍历数组并打印其内容时,将得到15 0。

The solution is, as implied by others, exchange the loop limitation to the size of the stack (the number of elements actually added), rather than the size of the array. 正如其他人所暗示的,该解决方案将循环限制交换为堆栈的大小(实际添加的元素数),而不是数组的大小。

代替for(int i=0;i<stackPlaces.length-1;i++) ,执行for(int i=0;i<stackIndex;i++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM