Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实现方法:搜索、加载和打印_Java - Fatal编程技术网

Java 实现方法:搜索、加载和打印

Java 实现方法:搜索、加载和打印,java,Java,请看我在代码中的注释,以便更好地解释事情。基本上有以下方法的问题。我可以让load方法运行,但我不确定用户输入的数字是否实际存储在数组中 此外,搜索方法一直在抛开一些东西,我认为这是一个循环 详见下文。先谢谢你 import java.util.Scanner; public class MyContainer { private int[] values; private int size; public MyContainer(){ values=new int[50]; size=0;}

请看我在代码中的注释,以便更好地解释事情。基本上有以下方法的问题。我可以让load方法运行,但我不确定用户输入的数字是否实际存储在数组中

此外,搜索方法一直在抛开一些东西,我认为这是一个循环

详见下文。先谢谢你

import java.util.Scanner;
public class MyContainer {
private int[] values;
private int size;
public MyContainer(){
values=new int[50];
size=0;}

//Load Method - Display a message to the user 
//and get positive intergers from user
public void load()
{
int input;
Scanner in = new Scanner(System.in);
System.out.println("Enter a series of positive integers (Negative to Terminate): ");

input=in.nextInt();
while (input >=0) {
  values[size]=input;
  size++;
  input=in.nextInt();
}

}//End Load

//Compute Average from the above entered numbers
public double computeAverage() {
double avg= 0.0;
int count = 0;
while(values[size] >=0)
{avg = avg + values[size];
count++;
}
size = size + 1;
avg = avg / size;
return avg;

}

//Get user input to search for a number in the array
public boolean search(int myInt){

while(values[size] >=0) {
if (values[size] == myInt){
  return true;}
else{
  size++;}
}
return false;
}

 //print the position of the number
  public void print(){
   for(int i=0;i>=size;i++) {
   System.out.println("The number at position " + i + " is " + values[i]);
 }
 }
}
这就是我目前所拥有的。我还为上述容器创建了一个tester类

   class Tester {
   public static void main(String[] args) {
   MyContainer in = new MyContainer();
   in.load();
   in.computeAverage();
   in.search(); //i know for a fact this is wrong just stuck        
   in.print();    
 }
}
如有任何建议/帮助,将不胜感激。我的教授在教学方面很差劲,这本书只解释了部分内容。

您的search()方法有一些您无法传递的参数

你宣布它是

public boolean search(int myInt) {

    while (values[size] >= 0) {
        if (values[size] == myInt) {
            return true;
        } else {
            size++;
        }
    }
    return false;
}
但是用…来称呼它

in.search();
这段代码甚至无法编译。为了便于讨论,我将其设置为5

computeAverage()
方法中,这是一个无限循环

while (values[size] >= 0) {
        avg = avg + values[size];
        count++;
    }

我认为您遇到的主要问题是重用
size
变量。在加载功能中,它将按预期工作,例如加载10个数字
size
将为10,并且
值中的元素0->9将包含数字。但是,当您到达
computeAverage
时,大小仍然是10。所以你在一个无限循环中

 while(values[size] >= 0) {
    avg = avg + values[size];
    count++;
 }

第一次迭代您将检查
值[10]
(如果
大小
为10,则有效元素仅在0->9中,这是错误的)。下一次迭代
avg
count
增加,但
size
保持不变,因此您将向
avg
添加相同的数字并继续循环。您应该在
computeAverage
search
中为while循环使用不同的条件。最后输入的要退出的负数将不在数组中;你需要用别的东西。作为提示,它将涉及
count
size

您的
search
方法在声明中有一个参数,但您不会在类
Tester
中传递任何内容。这是正确的吗?@DanielPereira-是的,我相信我也在运行一个无限a循环。任何帮助都会使你仍然对这个程序感到困惑。