简体   繁体   English

Java 程序中的错误输出

[英]Incorrect output in Java program

This is the description of the problem and here is the code that I got:这是问题的描述,这是我得到的代码:

PP 10.1 Design and implement a program that reads a series of 10 inte- gers from the user and prints their average. PP 10.1 设计并实现一个程序,该程序从用户处读取一系列 10 个整数并打印它们的平均值。 Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method.将每个输入值作为字符串读取,然后尝试使用 Integer.parseInt 方法将其转换为整数。 If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again.如果此过程抛出 NumberFormatException(意味着输入的数字不是有效数字),则打印相应的错误消息并再次提示输入数字。 Continue reading values until 10 valid integers have been entered.继续读取值,直到输入 10 个有效整数。

This is the code I have so far:这是我到目前为止的代码:

import java.util.Scanner;

public class average 
{
private int number_values;
private int[] int_values;
private double average;

public average(int number_values)
    {
    this.number_values = number_values;
    }
public void values()
    {
    String value_string = null;
    int int_value = 0,a;
    Scanner number = null;
    a = 0;
    int_values = new int [number_values];

while (a < number_values)
    {
    try{
        number = new Scanner(System.in);
        System.out.print("Please enter a value:");

        value_string = number.nextLine();

        int_value = Integer.parseInt(value_string);

        int_values[a++] = int_value;
        } 
    catch (NumberFormatException ex)
            {
            System.out.print("This is an invalid input. Please renter another number:");
            continue;
            }
    }
}
public void printValues()
{
System.out.println("The values are:");

    for (int a = 0; a < number_values; a++)
    {
        System.out.println("Number - " + (a + 1) + " = " + int_values);

    }
}
public double get_average()
{
int sum = 0;

for(int a = 0; a < number_values; a++)
    {
    sum += int_values[a];
    }
average = (double) sum / number_values;
return (average);
}
public static void main(String[] args)
{
    average a = new average(10);
    a.values();
    a.printValues();
    System.out.println("Average = " + a.get_average());

}
}

When I enter an incorrect character it says "This is an invalid input. Please renter another number:Please enter a value:"当我输入不正确的字符时,它会显示“这是一个无效输入。请租用另一个号码:请输入一个值:”

And when I displays the average it says Number - 1 = [I@330bedb4" for all of the values.当我显示平均值时,它会为所有值显示 Number - 1 = [I@330bedb4"。

So the println string when I enter a incorrect input is messed up and the values are messed up.因此,当我输入不正确的输入时, println 字符串被弄乱了,并且值也被弄乱了。 What am I missing?我错过了什么?

Try intValues[i] , if you want to output a specific item of your array.如果要输出数组的特定项,请尝试intValues[i] Otherwise you get the toString representation of the array object itself.否则,您将获得数组对象本身的toString表示。

And please next time post your code instread of screenshots ;)下次请在屏幕截图中发布您的代码;)

You're not printing the elements of the array .您没有打印array的元素。 You're printing the array string representation directly.您正在直接打印array字符串表示形式。 The error is done in your printValues() method.错误是在您的printValues()方法中完成的。

To solve this, you must call an element of the array.要解决这个问题,您必须调用数组的一个元素。 In this case, you want to call all elements, so you must use a loop .在这种情况下,您要调用所有元素,因此必须使用loop Here:这里:

public void printValues()
{
System.out.println("The values are:");

    for (int a = 0; a < number_values; a++)
    {
        System.out.println("Number - " + (a + 1) + " = " + int_values[i]);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM