为什么程序没有显示任何输出?

时间:2017-10-23 18:33:17

标签: java

我一直在尝试从几个小时开始对这个java程序进行故障排除,并且无法找到执行的错误。我认为主类没有正确定义。

它成功编译但是输出是空白的,不应该是这样吗?我最初尝试使用对象调用主类但仍然没有运气。任何建议都有效。

原始程序:在添加main方法时成功编译,但输出为空白。

import java.lang.Math; // headers MUST be above the first class

// one class needs to have a main() method
public class OrdSetSimple
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args){

      OrdSetSimple obj = new OrdSetSimple(10);
      System.out.print("Success");

  }


  private int _set_size;
  private int _set[];
  private int _last;


  public OrdSetSimple(int size) {
    int k;
    _set_size=size;
    _set = new int[_set_size];
    for(k=0; k<_set_size; k++)
        _set[k]=0;
    _last=-1;
  }

  private int make_a_free_slot(int val) {
    int pos, i, j;

      pos = binSearch(val);
    if (pos >= 0)
        return -1;
    for (i=0; i<=_last; i++) {
        if (_set[i] > val)
            break;
    }

    if ((i<=_last)||(_last==-1)) {
        for (j=_last; j>=i; j--)
            _set[j+1] = _set[j];
        pos = i;
    } else {
            pos = _last+1;
    }
      _last++;
    return pos;
  }

  public void addElement(int n) {
    int pos;
      if (n<0) {
          System.out.println("Addition of a negative integer impossible\n");
        return;
      }
    if (_last+1>=_set_size) {
        System.out.print("Addition of " + n);
      System.out.println(" impossible since the array is already full");
          System.out.println("The array is: " + toString());
    } else {
          pos = make_a_free_slot(n);
        if (pos>=0)
            _set[pos]=n;
    }
    return;
  }

  public int getSize() {
    return _last+1;
  }

  public int getElementAt(int i) {
    if ((i<0)||(i>_last))
        return -1;
    else
        return _set[i];
  }

  private int binSearch(int x) {
    int i=0;
      int j=_last-1;
    int m=0;

    if (_last==-1)
        return -1;

    while(i<j) {
        m= (i+j)/2;
        if (x>_set[m])
            i=m+1;
        else
            j=m;
    }
    if (x == _set[i]) return i;
    else return -1;
  }

  public OrdSetSimple difference(OrdSetSimple s2) {
    OrdSetSimple s1 = this;
    int size1=s1.getSize();
    int size2=s2.getSize();

    OrdSetSimple set=new OrdSetSimple(size2);

    int k;

    for(k=0; k<size1; k++)
        if (s2.binSearch(s1.getElementAt(k)) < 0)
            set.addElement(s1.getElementAt(k));

    return set;
  }

  public String toString() {
    int k = 0;
    String s="";

    for (k=0; k<=_last; k++)
        s += _set[k] + " ";

    return s;
  }

}

1 个答案:

答案 0 :(得分:1)

你的第一个陈述是错误的。

 OrdSetSimple obj = new OrdSetSimple();//This will call the default constructor which will not initialize anything. This constructor will be added to your program by compiler, hence you don't get any compilation error.

纠正它

 OrdSetSimple obj = new OrdSetSimple(100);