gpt4 book ai didi

java - 在同一个类中的堆栈上调用方法

转载 作者:行者123 更新时间:2023-11-30 08:06:23 24 4
gpt4 key购买 nike

我正在编写一个程序,该程序接受一系列 Integers 并将它们放入 StatisticianStack 中。我无法弄清楚如何调用此堆栈上的其他方法。关于如何在 nextNumber 方法中调用所有其他方法有什么建议吗?

统计学家堆栈:

public class StatisticianStack {

Stack<Integers> stack = new Stack();


public void nextNumber(Integer stackNums){
stack.push(stackNums);
stack.length(); // what I would want to do, but do not know what to pass it.

}

public static int length(StatisticianStack numbers){

if(numbers.isEmpty())
return 0;

int sizeOfStack = numbers.size();
return sizeOfStack;
}

public static int sum(Stack<Integer> numbers){
int sum = 0;
if(numbers.isEmpty())
return sum;

for(int i = 0; i < numbers.size(); i++)
sum = sum + numbers.pop();

return sum;
}

public static Double mean(Stack<Integer> numbers){
double mean = 0;
double sum = 0;

if(numbers.isEmpty())
return Double.NaN;

for (int i = 0; i < numbers.size(); i++){
sum += i;
}
mean = sum/numbers.size();
return mean;
}

public static Double largestNum(Stack<Integer> numbers){
double largestNum = numbers.firstElement();

if(numbers.isEmpty())
return Double.NaN;

for (int i = 0; i < numbers.size(); i++){
if(largestNum < numbers.pop())
largestNum = numbers.pop();

}
return largestNum;
}

public static Double smallestNum(Stack<Integer> numbers){
double smallestNum = numbers.firstElement();

if(numbers.isEmpty())
return Double.NaN;

for (int i = 0; i < numbers.size(); i++){
if(smallestNum > numbers.pop())
smallestNum = numbers.pop();

}
return smallestNum;
}
}

主要:

public class StatisticianStackDemonstartion {

public static Integer numbers;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
StatisticianStack stack = new StatisticianStack();
stack.nextNumber(-1);


}

}

`

有什么建议吗?谢谢。

最佳答案

现在我明白了。您可能想在这里考虑面向对象

您有一个 StatisticianStack 类型的对象:

StatisticianStack stack = new StatisticianStack();

然后将一些数字放入该堆栈:

stack.nextNumber(-1);
stack.nextNumber(2.34);
stack.nextNumber(17);

然后您想对堆栈中的所有元素进行一些计算:

int length = stack.getLength();
double mean = stack.getMean();
double largest = stack.getLargestNumber();
double smallest = stack.getSmallestNumber();

为此,您需要先更改您的方法签名:

public class StatisticanStack {
Stack<Double> stack = new Stack<Double>();

public void nextNumber(Double stackNums) {
stack.push(stackNums);
}

public int length() {
return stack.size();
}

public double sum() {

}

public double mean() {

}

public double largestNum() {

}

public double smallestNum() {

}
}

请确认这就是您想要的 - 然后我们用生命填充这些方法 ;-)

您已确认 - 这是使用 Java 8 Lambda 表达式完成的示例:

import java.util.Stack;

public class StatisticanStack {

Stack<Double> stack = new Stack<Double>();

public void nextNumber(Double stackNums) {
stack.push(stackNums);
}

public void nextNumber(Integer stackNums) {
stack.push(new Double(stackNums));
}

public int length() {
return stack.size();
}

public double sum() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.sum();
}

public double mean() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.average().getAsDouble();
}

public double largestNum() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.max().getAsDouble();
}

public double smallestNum() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.min().getAsDouble();
}

public static void main(String... args) {
StatisticanStack stack = new StatisticanStack();
stack.nextNumber(-1);
stack.nextNumber(2.34);
stack.nextNumber(17);
System.out.println(stack.smallestNum());
System.out.println(stack.largestNum());
System.out.println(stack.mean());
System.out.println(stack.sum());
}

}

或者如果您更喜欢在没有 Lambda 的情况下工作:

public double sum() {
double sum = 0;
for(Double entry : stack) {
sum += entry.doubleValue();
}
return sum;
}

等等。

您可能想澄清您想要什么作为“最小”数字:最接近于零?最消极?

关于java - 在同一个类中的堆栈上调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com