我现在很困惑。我对编程知之甚少,只是很难完成这项关于数组的任务。。这就是我的任务:(请忽略底部的评论,我还没有做这些。(
public interface MyArrayInterface {
public String toString();
//this returns true if the item is was added in the array, otherwise false
public boolean add(int x);
// this returns true if the array is full
public boolean isFull();
//will return the index of the element if the element x is in the array, otherwise returns -1
public int locate(int x);
//this returns true if the arrays is empty
public boolean isEmpty();
//will return true if the item x is in the array otherwise false
public boolean search(int x);
//clears all the elements in the array
public void clear();
//this returns true if the item is removed from the array, otherwise false
public boolean remove(int x);
//insert data to array at given location
//public boolean insertAt(int x, int loc);
/*
public boolean insertAtBegining(int x); //insert element at the beginning of the array
//public boolean bubbleSort();
public boolean removeAt(int loc); //remove an element from a specific location in the array
public boolean removeAll(int x); //remove all elements in an array
public int getSum(); //get sum of all elements in the array
public int getMax(); //get maximum value of elements in the array
public int getMin(); //get minimum value of elements in the array
public boolean fillArray(int otherArray[]); //replay the existing array with this
*/
}
我只是根据老师的指示和对每种方法应该做什么的描述,分别对这些方法进行了编码。现在,我在将它们一起使用以生成一种菜单时遇到了问题,在这种菜单中,用户被要求输入数组的元素,然后选择他们想要采取的下一步,即向数组中添加元素或从数组中删除元素。。到目前为止,我已经完成了以下内容:
public class MyArrayImplementation implements MyArrayInterface {
private int [] arr; // this will be our actual array
private int count; // this will be used to determine the number of elements inside our array
//default constructor
public MyArrayImplementation() {
arr = new int [10];
count = 0;
}
public MyArrayImplementation(int size) {
arr = new int[size];
count=0;
}
//this will return a string equivalent of the array
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i=0;i<count;i++) {
sb.append(arr[i]);
if(i<count-1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
//this returns true if the item is was added in the array, otherwise false
public boolean add(int x) {
boolean retVal=false;
if(isFull()) {
System.out.println("The array is full!");
}
else {
arr[count++]=x;
retVal = true;
System.out.println("Item "+x+" successfully added !");
}
return retVal;
}
// this returns true if the array is full
public boolean isFull() {
return (arr.length == count);
}
//this returns true if the arrays is empty
public boolean isEmpty() {
return (count==0);
}
//will return the index of the element if the element x is in the array, otherwise returns -1
public int locate(int x) {
int loc = -1;
for(int i=0;i<count;i++) {
if(x == arr[i]) {
loc = i;
break;
}
}
return loc;
}
//will return true if the item x is in the array otherwise false
public boolean search(int x) {
return (locate(x)!= -1);
}
//clears all the elements in the array
public void clear() {
/*
for(int i=0;i<count;i++) {
arr[i]=0;
}*/
count=0;
}
//this returns true if the item is removed from the array, otherwise false
public boolean remove(int x) {
boolean retVal = false;
if(isEmpty()) {
System.out.println("The array is empty, cannot remove an item!");
}
else {
if(search(x)) {
if (locate(x) < count-1) {
for(int i=locate(x);i<count-1;i++) {
arr[i]= arr[i+1];
}
}
System.out.println("Item "+x+" is succesfully removed!");
count--;
retVal = true;
}
else {
System.out.println("Item "+x+" is not found in the array!");
}
}
return retVal;
}
}
我只能通过以下代码显示输出:
public class TestArray {
public static void main(String[] args) {
MyArrayImplementation ar1 = new MyArrayImplementation();
System.out.println(ar1);
ar1.add(10);
System.out.println(ar1);
ar1.add(15);
System.out.println(ar1);
ar1.add(20);
System.out.println(ar1);
System.out.println("Locating 30 in the array:"+ar1.locate(30));
System.out.println("Searching 30:"+ar1.search(30));
ar1.add(10);
ar1.add(30);
ar1.add(40);
ar1.add(20);
System.out.println(ar1);
ar1.remove(20);
System.out.println(ar1);
ar1.remove(20);
System.out.println(ar1);
}
}
如何在不必在编辑器上键入ar1.add或ar1.remove的情况下显示输出?比如,用户必须输入这些。。。另外,如果C有scanf,那么它在java中的等价物是什么?
我真的希望我能理解。。。HELPPP T_T
如果你想让用户输入,我会使用Scanner类
用于初始化数组:
Scanner scan = new Scanner(System.in);
System.out.println("Enter array: "); // Asks for the array
String str = scan.nextLine();
// Pass str for integers to store in the array
然后设置一个读取下一行的循环,例如
System.out.println("Instructions for what you can input ... or just hit enter to end");
str = scan.nextLine();
// do a loop until input is empty
// inside loop parse the string
我强烈建议你退房https://www.w3schools.com/java/java_user_input.asp
如果这不能回答您的问题,请修改,以便我们能够给出更准确的答案-欢迎使用Stack Overflow:-(