该程序应该返回
考试成绩按降序排列是:(分数)平均值是 (平均)
我收到的结果是
平均分为(平均)测试分数按降序排列: (分数)
如何解决它以获得适当的回报
//Check whether the device has a fingerprint sensor//
if (!mFingerprintManager.isHardwareDetected()) {
// If a fingerprint sensor isn’t available, then inform the user that they’ll be unable to use your app’s fingerprint functionality//
textView.setText("Your device doesn't support fingerprint authentication");
}
//Check whether the user has granted your app the USE_FINGERPRINT permission//
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
// If your app doesn't have this permission, then display the following text//
Toast.makeText(EnterPinActivity.this, "Please enable the fingerprint permission", Toast.LENGTH_LONG).show();
}
//Check that the user has registered at least one fingerprint//
if (!mFingerprintManager.hasEnrolledFingerprints()) {
// If the user hasn’t configured any fingerprints, then display the following message//
Toast.makeText(EnterPinActivity.this, "No fingerprint configured. Please register at least one fingerprint in your device's Settings", Toast.LENGTH_LONG).show();
}
//Check that the lockscreen is secured//
if (!mKeyguardManager.isKeyguardSecure()) {
// If the user hasn’t secured their lockscreen with a PIN password or pattern, then display the following text//
Toast.makeText(EnterPinActivity.this, "Please enable lockscreen security in your device's Settings", Toast.LENGTH_LONG).show();
}
要完成这项工作,我需要交换些什么?
编辑:此文件中使用了平均值类
import java.util.Scanner;
public class Average
{
private int[] data;
private double mean;
public Average()
{
data = new int[5];
int number = 0;
int Temp = 0;
String[] Scores = new String[5];
Scanner keyboard = new Scanner(System.in);
for(int index = 0; index < data.length; index++)
{
number++;
System.out.print("Enter test score #" + number + ":");
Temp = keyboard.nextInt();
data[index] = Temp;
}
selectionSort();
calculateMean();
}
public void selectionSort()
{
int n = data.length;
for(int index = 0; index < n-1; index++)
{
int min_idx = index;
for(int j = index+1; j < n; j++)
if(data[j] >= data[min_idx])
min_idx = j;
int temp = data[min_idx];
data[min_idx] = data[index];
data[index] = temp;
}
}
public String toString()
{
String MyScore = "Your test scores in descending order are: " + "\n";
for(int index = 0; index < data.length; index++)
{
MyScore += data[index] + " ";
}
return MyScore;
}
public void calculateMean()
{
int Sum = 0;
int Mean = 0;
for(int index = 0; index < data.length; index++)
{
Sum += data[index];
}
Mean = Sum/data.length;
System.out.println("The average is " + mean);
}
}
答案 0 :(得分:1)
仅应在致电calculateMean()
后致电toString()
。
因此,您可以修改构造函数
selectionSort();
System.out.println(toString());
calculateMean();
或仅在构造函数中调用selectionSort();
,然后在您的main
public class AverageDriver
{
public static void main(String[] args)
{
Average myScores = new Average();
System.out.print(myScores);
myScores.calculateMean();
}
}
顺便说一句,您应该为变量使用小写名称,这将解决calculateMean()
中的错误:您正在调用System.out.println("The average is " + mean);
,但变量为Mean
。
答案 1 :(得分:0)
无法打印正确平均值的错误在于此方法:
n
您要打印出public void calculateMean() {
int Sum = 0;
int Mean = 0;
for (int index = 0; index < data.length; index++) {
Sum += data[index];
}
Mean = Sum / data.length;
System.out.println("The average is " + mean);
}
,它是全局定义的,但从未初始化,因此默认为mean
。在此方法中,您拥有变量0
(大写),它是实际值存储在其中的变量,即您要打印的变量。
答案 2 :(得分:0)
public static void main(String[] args)
{
Average MyScores = new Average();
System.out.print(MyScores);
}
创建Average
实例时,将调用构造函数。在构造函数中,有一个对calculateMean()
的调用,它会打印
平均值是(平均值)
当您打印MyScores
时,将调用覆盖toString()
,并进行打印。测试分数按降序排列:(分数)。
从构造函数中取出方法调用(它们不应该放在首位),然后从main
调用它们
public static void main(String[] args)
{
Average myScores = new Average();
System.out.print(myScores);
myScores.selectionSort();
myScores.calculateMean();
}