我正在使用三个Java文件,但似乎无法打印数组。 我尝试了各种事情,似乎我正在转圈。 我对如何使用其他类的数组感到困惑。 预先感谢您的任何帮助。

这是主要的:

 public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
     TestInteger TI = new TestInteger();
     TestDouble TD = new TestDouble();
     Distance TDist = new Distance();
     mainMenu();
}
     public static void mainMenu() {
            int option;
            Scanner sc = new Scanner(System.in);

            while(true){
                System.out.println("Please choose what type of numbers will be stored in the Array");
                System.out.println();
                System.out.println("******* MAIN MENU ********");
                System.out.println("** 1. Integers          **");
                System.out.println("** 2. Double            **");
                System.out.println("** 3. Distance          **");
                System.out.println("** 4. Exit              **");
                System.out.println("**************************");

                try{
                    option = sc.nextInt();

                    switch (option) {
                    case 1: {

                        TestInteger.arrayMenu();
                    }
                    case 2: {

                        TestDouble.arrayMenu();
                    }
                    case 3: {

                        Distance.arrayMenu();
                    }

                    case 4: {
                        System.out.println("Exiting Program");
                        System.exit(0);
                    }

                    default: {
                        System.out.println();
                        System.out.println("Invalid option. Please select option" + " 1 - 4");
                        System.out.println();

                        mainMenu();
                    }
                    }
                }catch(InputMismatchException e){
                    System.out.println("Enter only 1-4");
                    sc.nextLine();
                }
            }
        }

}

应该使用SortedArray类来保存对象的排序数组以及所有与SortedArray一起使用的方法。

   import java.util.Arrays;

   public class SortedArray implements Comparable {

//private int[] sa;

//public SortedArray(int i, int j) {
    // TODO Auto-generated constructor stub
//}

public void constructor() {
    final int initialSize = 5;
    int incrementAmount = 3;
    int top = -1;

    Comparable [] sa = new Comparable[initialSize];
    //for(int i=0; i<5;++i){
    //  SortedArray sa = new SortedArray();
    //}
    top = -1; //shows the last item of the array
}

public int appropriatePosition(){
    int ap = 0;
    return ap;
}

public int smallest(){
    int smallest = 0;
    System.out.println("The smallest is ");
    return smallest;
}

public int largest(){
    int largest = 0;
    System.out.println("The largest is ");
    return largest;

}

public void insert(int i){
    int pos = 0;

    pos++;
}

public void find(){

}

public void delete(){

}

public Comparable[] print(){
    Comparable[] sa = {2,3,4,5};
    System.out.println(sa);
    return sa;
}

public void clear(){
    System.out.println("Now Clearing the Array....................................");
    // Arrays.fill(sa, null);

}

public boolean full(){
    boolean full = false;
    return full;
}

public boolean empty(){
    boolean empty = false;
    return empty;
}

@Override
public int compareTo(Object arg0) {
    // TODO Auto-generated method stub
    return 0;
}

TestInteger类用于仅将Integers插入到数组中,然后以不同的方式操作数组。

 import java.util.Arrays;
 import java.util.InputMismatchException;
 import java.util.Scanner;

public class TestInteger implements Comparable{

/**
 * This is the User Interface for manipulating the sorted array
 */

    static SortedArray sa = new SortedArray (5,3);

    public static void arrayMenu() {

        int option;

        while(true){
            System.out.println();
            System.out.println("**** Integer Array Menu ****");
            System.out.println("****************************");
            System.out.println("** 1. Insert              **");
            System.out.println("** 2. Delete              **");
            System.out.println("** 3. Clear               **");
            System.out.println("** 4. Smallest            **");
            System.out.println("** 5. Largest             **");
            System.out.println("** 6. Return to Main Menu **");
            System.out.println("****************************");

            Scanner sc = new Scanner(System.in);
        try{

            option = sc.nextInt();
            switch (option){
            case 1:{
                try{
                    System.out.println("Type an integer to insert: ");
                    int x = sc.nextInt();
                    int index = 0;
                    sa.insert(x);
                    sa.print();
                }catch(InputMismatchException e){
                    System.out.println("Enter only integers");
                    sc.nextLine();
                }
                arrayMenu();
            }
            case 2:{
                try{
                    System.out.println("Type the index of the item you wish to delete:");
                    int d = sc.nextInt();
                }catch(InputMismatchException e){
                    System.out.println("Enter only integers");
                    sc.nextLine();
                }
                arrayMenu();
            }
            case 3:{
                System.out.println("Before clearing");
                sa.print();
                sa.clear();
                System.out.println("After clearing");

                sa.print();
                arrayMenu();
            }
            case 4:{
                sa.smallest();
                arrayMenu();
            }
            case 5:{
                sa.largest();
                arrayMenu();
            }
            case 6:{
                Main.mainMenu();
            }
            default: {
                System.out.println();
                System.out.println("Invalid option. Please select option 1 - 6");
                System.out.println();

                arrayMenu();
            }
            }
        }catch(InputMismatchException e){
            System.out.println("Enter only 1-6");
            sc.nextLine();
            }

        }
}


    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }
}

===============>>#1 票数:0 已采纳

请注意,您的SortedArray类没有实际状态。 Java中的内容仅存在于将它们括起来的大括号内。 我假设您的构造函数是:

public void SortedArray(int initialSize, int incrementAmount) {
    final int initialSize = 5;
    int incrementAmount = 3;
    int top = -1;
    Comparable [] sa = new Comparable[initialSize];
    top = -1; //shows the last item of the array
}

构造函数始终共享该类的名称。 您的代码默认为无参数的空构造函数。

但是应该更像这样:

private int incrementAmount;
private int top;
private Comparable[] sa;
public void SortedArray(int initialSize, int incrementAmount){
  this.initialSize = initialSize;
  this.incrementAmount = incrementAmount;
  this.top = -1
  this.sa = new Comparable[initialSize];
}

将类视为一种“事物”的“蓝图”。 字段是该“事物”的属性和数据。 构造函数是该“事物”的实例化,它实际上是根据某些参数来烹调作为类的配方的构造函数。

  ask by Neophyte translate from so

未解决问题?本站智能推荐: