-1

Basically, I want the user to input 'Average' or 'Sum' so I can then display the relevant one that was requested using System.out.println();

But how can I do this? (Apologies if this is really simple)

int SumChocolates = 0;
SumChocolates = (int) (data [0][7] + data [1][7] + data [2][7] + data [3][7]);

int AvgChocolates = 0;
AvgChocolates = (int) (SumChocolates / 4);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
OJL
  • 1
  • 1

3 Answers3

1

You can use a Scanner then use Scanner.nextLine(); to get input from the user in the console.

Also, you may want to take a look at the standard naming schemas in Java

Scanner scan = new Scanner(System. in ); //Create a new Scanner
System.out.println("Would you like the average, or sum?");
String input = scan.nextLine(); //Get input from the user

int SumChocolates = 0; //Must declare out here, because Average case uses this
SumChocolates = (int)(data[0][7] + data[1][7] + data[2][7] + data[3][7]);

if (input.equalsIgnoreCase("Sum")) {
    System.out.println("The Sum is: " + SumChocolates);
} else if (input.equalsIgnoreCase("Average")) {
    int AvgChocolates = 0;
    AvgChocolates = (int)(SumChocolates / 4);
    System.out.println("The average is: " + AvgChocolates);
}
austin wernli
  • 1,801
  • 12
  • 15
0

You can use a Scanner:

Scanner scanner = new Scanner(System.in);
String userInput = scanner.next();
Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55
0

Use a scanner. Depending on their input, display the output you want. Also, variables should be lowerCamelCase to comply with Java coding standards.

This checks to make sure they enter valid input before displaying a result.

Scanner s = new Scanner(System.in);
String input = "";
System.out.println("Would you like to see the Average, or the Sum?");

while (true) {
    input = s.nextLine();
    if (input.equalsIgnoreCase("Average")) {
        System.out.println(avgChocolates);
        break;
    } else if (input.equalsIgnoreCase("Sum")) {
        System.out.println(sumChocolates);
        break;
    } else {
        System.out.println("Invalid input. Please enter 'Average' or 'Sum'.");
    }
}
mbomb007
  • 3,788
  • 3
  • 39
  • 68