无法更正此代码..
import java.text.DecimalFormat;
public class PeopleWeights {
public static void main(String[] args) {
DecimalFormat decFor = new DecimalFormat("0.00");
int i = 0;
int n = 5;
double arr[]=new double[n];
for(i = 0; i < n; i++){
System.out.println("Enter weight " + (i+1)+": ");
}
System.out.print("\nYou entered: ");
for(i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
double total = 0;
double max = 0;
for(i=0; i<n; i++){
if(max < arr[i]){
max = arr[i];
}
total = total + arr[i];
}
DecimalFormat df = new DecimalFormat("#.##");
double average = total/n;
System.out.println("Total weight: "+ df.format(total));
System.out.println("Average weight: "+ df.format(average));
System.out.println("Max weight: "+ df.format(max));
return;
}
}
这是我保持的错误。
您输入了:0.0 0.0 0.0 0.0 0.0 总重量:0 平均 预期输出以Enter weight 1开始: 输入重量2: 输入重量3: 输入重量4: 输入重量5:
您输入:236.0 89.5 142.0 166.3 93.0
您输入了:0.0 0.0 0.0 0.0 0.0 总重量:0 平均 预期输出以Enter weight 1开始: 输入重量2: 输入重量3: 输入重量4: 输入重量5:
您输入了:123.4 56.0 98.0 174.0 215.8
您输入了:0.0 0.0 0.0 0.0 0.0 总重量:0 平均体重:0 最大重量:0
预期输出以输入重量1开始: 输入重量2: 输入重量3: 输入重量4: 输入重量5:
您输入:236.0 89.5 142.0 166.3 93.0 总重量:726.8
您输入了:0.0 0.0 0.0 0.0 0.0 总重量:0 平均体重:0 最大重量:0
预期输出以输入重量1开始: 输入重量2: 输入重量3: 输入重量4: 输入重量5:
您输入:236.0 89.5 142.0 166.3 93.0 总重量:726.8 平均重量:145.35999999999999
您输入了:0.0 0.0 0.0 0.0 0.0 总重量:0 平均体重:0 最大重量:0
预期输出输入重量1: 输入重量2: 输入重量3: 输入重量4: 输入重量5:
您输入:236.0 89.5 142.0 166.3 93.0 总重量:726.8 平均重量:145.35999999999999 最大重量:236.0
您输入了:0.0 0.0 0.0 0.0 0.0 总重量:0 平均体重:0 最大重量:0
预期输出输入重量1: 输入重量2: 输入重量3: 输入重量4: 输入重量5:
您输入:123.4 56.0 98.0 174.0 215.8 总重量:667.2 平均体重:133.44 最大重量:215.8
答案 0 :(得分:1)
您没有接受用户的输入。使用Scanner
或BufferedReader
获取用户在控制台中输入的内容。
阅读此问题以获取更多详细信息: How can I get the user input in Java?
答案 1 :(得分:0)
您没有收到输出,因为您没有从用户那里获取输入。你需要做的是获取变量的输入并将其添加到数组中。要从控制台获取输入,您可以使用扫描仪。 以下是编辑过的代码。试试这个,我想你可以得到输出。
import java.text.DecimalFormat;
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
DecimalFormat decFor = new DecimalFormat("0.00");
Scanner sc = new Scanner(System.in);
int i = 0;
int n = 5;
double arr[]=new double[n];
for(i = 0; i < n; i++){
System.out.println("Enter weight " + (i+1)+": ");
double weight = Double.parseDouble(sc.nextLine());
arr[i] = weight;
}
System.out.print("\nYou entered: ");
for(i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
double total = 0;
double max = 0;
for(i=0; i<n; i++){
if(max < arr[i]){
max = arr[i];
}
total = total + arr[i];
}
DecimalFormat df = new DecimalFormat("#.##");
double average = total/n;
System.out.println("Total weight: "+df.format(total));
System.out.println("Average weight: "+df.format(average));
System.out.println("Max weight: "+df.format(max));
return;
}
}
答案 2 :(得分:0)
仍然得到一个不同的错误。输出4,“最大重量:2”.36用橙色突出显示,平均重量:145.3“6” - 6用橙色突出显示输出平均重量:145.3“5999999999999”“”突出显示。比较输出5:您的输出平均重量:145.3“6” - “”突出显示....预期输出平均重量:145.3“5999999999999”最大重量:23“6.0”突出显示“”。你有解决方法吗?
这是由于使用了DecimalFormat。
尝试:
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = 0;
int n = 5;
double total = 0;
double max = 0;
double arr[]=new double[n];
for(i = 0; i < n; i++){
System.out.println("Enter weight " + (i+1)+": ");
double weight = Double.parseDouble(sc.nextLine());
arr[i] = weight;
if(max < arr[i]){
max = arr[i];
}
total = total + arr[i];
}
System.out.printf("%nYou entered: ");
for(i = 0; i < n; i++){
System.out.printf("%s ", arr[i]);
}
System.out.println();
double average = total/n;
System.out.printf("Total weight: %.1f%n", total);
System.out.println("Average weight: " + average);
System.out.printf("Max weight: %.1f%n" + max);
return;
}
}
答案 3 :(得分:0)
这是我使用的:
import java.util.Scanner;
/**
* Handles an array of peoples weights.
*/
public class PeopleWeights
{
// static variables that we can access in our static methods.
static final int WEIGHT_ARRAY_SIZE = 5;
static double[] weightsOfPeople = new double[WEIGHT_ARRAY_SIZE];
static double weightElement;
static double sum = 0.0;
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args)
{
GetWeights();
WeightsSum();
WeightsAverage();
WeightsMax();
return;
}
/**
* Takes input for the weights and store them inside our array.
*/
public static void GetWeights()
{
int i = 0; // Local count variable.
// Take input for the weights and store them in our array.
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
System.out.println("Enter weight " + (i + 1) + ": ");
weightElement = scnr.nextDouble();
weightsOfPeople[i] = weightElement;
}
// Display what the user entered
System.out.print("\nYou entered: ");
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
System.out.print(weightsOfPeople[i] + " ");
}
return;
}
/**
* Gets the sum of our weights and then displays that sum.
*/
static void WeightsSum()
{
int i = 0;
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
sum += weightsOfPeople[i];
}
System.out.println("\nTotal weight: " + sum);
return;
}
/**
* Gets the average of our weights and then displays that average.
*/
static void WeightsAverage()
{
double averageWeight = 0.0;
// Convert WEIGHT_ARRAY_SIZE to a double for good calculation.
averageWeight = sum / (double)WEIGHT_ARRAY_SIZE;
System.out.println("Average weight: " + averageWeight);
return;
}
/**
* Gets the max of our weights and then displays that max.
*/
static void WeightsMax()
{
int i= 0;
double maxWeight = 0.0;
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
if(weightsOfPeople[i] > maxWeight)
{
maxWeight = weightsOfPeople[i];
}
}
System.out.println("Max weight: " + maxWeight);
return;
}
}