gpt4 book ai didi

java - 方法的可变输出

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:21 24 4
gpt4 key购买 nike

大家好,

public void bubbleSort(boolean radioValue, Integer[] bubbleArray) {
//declare and initialize variables
int tempVar = 0;
int swappedValue = 0;
int loopExecuted = 0;
int comparisonMade = 0;

//if radioValue is true run this if statement
if (radioValue) {
//declare boolean true and run while statement as long as boolean is true
//increase loopExecuted
boolean swapped = true;
while (swapped) {
loopExecuted++;

//declare boolean as false and run for loop to go over array
swapped = false;
for (int i = 1; i < bubbleArray.length; i++) {
//declare and intialize variable and increase loopExecuted and comparisonMade
int temp = 0;
loopExecuted++;
comparisonMade++;

//if bubbleArray i-1 is greater than bubbleArray i run this if statement
if (bubbleArray[i - 1] > bubbleArray[i]) {

//make temp value bubbleArray i-1
//since bubbleArray i is greater than bubbleArray i-1 swap the values
//bubbleArray i is now temp which is bubbleArray i-1
//make boolean true and increase swappedValue counter
temp = bubbleArray[i - 1];
bubbleArray[i - 1] = bubbleArray[i];
bubbleArray[i] = temp;
swapped = true;
swappedValue++;
}
}
}

}
//output the stats for bubble sort
outputArea.append("Bubble Sort \n"
+ "Number of the loop was executed:" + loopExecuted + "\n Number of times a comparison was made" + comparisonMade
+ "\n Number of times a value was shifted" + swappedValue);

}

我的问题是,有什么办法可以将 tempVar、swappedValue、loopExecuted 和 ComparisonMade 的变量传递给另一个方法吗?例如,在 bubbleSort 方法的末尾,我有一行代码输出所有这些变量,但相反,我想要它,以便我有一个方法可以代替该行代码来执行此操作。这主要是因为我有不止一种排序算法,只是想简化一切。

最佳答案

尝试创建一个这样的类:

class BubbleSortParams{
private int loopExecuted = 0;
private int swappedValue = 0;
private int comparisonMade = 0;
int getLoopExecuted() {
return loopExecuted;
}
void setLoopExecuted(int loopExecuted) {
this.loopExecuted = loopExecuted;
}
int getSwappedValue() {
return swappedValue;
}
void setSwappedValue(int swappedValue) {
this.swappedValue = swappedValue;
}
int getComparisonMade() {
return comparisonMade;
}
void setComparisonMade(int comparisonMade) {
this.comparisonMade = comparisonMade;
}
}

然后,不要打印它们的值,而是设置冒泡排序方法的返回类型 BubbleSortParams 并在方法末尾返回该类型的对象。像这样:

public BubbleSortParams bubbleSort(boolean radioValue, Integer[] bubbleArray) {
//declare and initialize variables
int tempVar = 0;
int swappedValue = 0;
int loopExecuted = 0;
int comparisonMade = 0;

//if radioValue is true run this if statement
if (radioValue) {
//declare boolean true and run while statement as long as boolean is true
//increase loopExecuted
boolean swapped = true;
while (swapped) {
loopExecuted++;

//declare boolean as false and run for loop to go over array
swapped = false;
for (int i = 1; i < bubbleArray.length; i++) {
//declare and intialize variable and increase loopExecuted and comparisonMade
int temp = 0;
loopExecuted++;
comparisonMade++;

//if bubbleArray i-1 is greater than bubbleArray i run this if statement
if (bubbleArray[i - 1] > bubbleArray[i]) {

//make temp value bubbleArray i-1
//since bubbleArray i is greater than bubbleArray i-1 swap the values
//bubbleArray i is now temp which is bubbleArray i-1
//make boolean true and increase swappedValue counter
temp = bubbleArray[i - 1];
bubbleArray[i - 1] = bubbleArray[i];
bubbleArray[i] = temp;
swapped = true;
swappedValue++;
}
}
}

}

BubbleSortParams bsp = new BubbleSortParams();
bsp.setComparisonMade(comparisonMade);
bsp.setLoopExecuted(loopExecuted);
bsp.setSwappedValue(swappedValue);
return bsp;

}

现在您可以用不同的方法对变量执行任何您想要的操作!比如下面这样:

public void myMethod(BubbleSortParams bsp) {
//now you can access the parameters from your other method
int swappedValue = bsp.getSwappedValue();
int loopExecuted = bsp.getLoopExecuted();
int comparisonMade = bsp.getComparisonMade();
//do whatever you want with the variables now!
}

关于java - 方法的可变输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48696730/

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