我有三个正在使用的 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 类仅用于将整数插入数组,然后以不同的方式操作数组。
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;
}
}