简体   繁体   中英

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. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. 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. Continue reading values until 10 valid integers have been entered.

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.

So the println string when I enter a incorrect input is messed up and the values are messed up. What am I missing?

Try intValues[i] , if you want to output a specific item of your array. Otherwise you get the toString representation of the array object itself.

And please next time post your code instread of screenshots ;)

You're not printing the elements of the array . You're printing the array string representation directly. The error is done in your printValues() method.

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 . 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]);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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