我创建了一个简单的OOP代码来计算百分位数。当我在主类中对其进行测试时,它可以正常工作,但即使代码已结束,它仍可以无限期运行。我在笔记本电脑上而不是PC(相同的代码,和相同版本的Netbeans)上试用了它,它运行良好,在结束时停止了运行。
这是方法类:
package P1;
import javax.swing.JOptionPane;
public class methods {
public double[] ar;
public void createAR(int n) {
ar = new double[n];
for (int c = 0; c < ar.length; c++) {
ar[c] = Double.parseDouble(JOptionPane.showInputDialog("Enter value #" + (c + 1) + "."));
}
}
public void sort() {
double flag = 0.0;
for (int r = 0; r < ar.length - 1; r++) {
for (int c = 0; c < ar.length - 1; c++) {
if (ar[c] > ar[c + 1]) {
flag = ar[c];
ar[c] = ar[c + 1];
ar[c + 1] = flag;
}
}
}
}
public int absFreq(double n) {
int ac = 0;
for (int c = 0; c < ar.length; c++) {
if (ar[c] == n) {
ac++;
}
}
return ac;
}
public String percentile(double n) {
sort();
int r = 0;
double mydouble = (n / 100) * (ar.length + 1);
r = (int) mydouble;
return "The percentile " + n + " is: " + ar[r - 1];
}
}
主要内容在这里:
package P1;
import javax.swing.JOptionPane;
public class main {
public static void main(String[] args) {
methods obj = new methods();
obj.createAR(10);
JOptionPane.showMessageDialog(null, obj.percentile(50));
}
}