#Can someone help me with this methods question
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Hey! according to this question, you need to write 2 functions
the 1st one is to enter the scores made in last 3 games and return the average : sum / 3; I guess
the 2nd function is going to take the result of the 1st one, and distinguish whether the user is qualified
you can make a class and inside you declare the 2 functions with static keyword, and you declare main() method where your whole program is
the return must be place at the end of the function
hmm you need to use Scanner object to enter the scores
the answer is not an input
i think this code is fine
wdym by answer key ?
I see a scanner !
it does
static Scanner input = new Scanner(System.in);
please do not follow in your teacher's footsteps of predeclaring variables
yeah, and he/she made faults aswell
heres a slightly better solution: ```java
import java.util.Scanner;
public class Exercise1 {
public static void main(String[] args) {
double averagePoints = getAveragePoints();
if (isAllStar(averagePoints)){
System.out.println("He is an allstar!");
}
else {
System.out.println("He is NOT an allstar.");
}
}
public static double getAveragePoints(){
Scanner input = new Scanner(System.in);
System.out.print("Enter game 1 points: ");
int gamePoints1 = input.nextInt();
System.out.print("Enter game 2 points: ");
int gamePoints2 = input.nextInt();
System.out.print("Enter game 3 points: ");
int gamePoints3 = input.nextInt();
return (gamePoints1 + gamePoints2 + gamePoints3) / 3.0;
}
public static boolean isAllStar(double averagePoints){
if (averagePoints >= 25){
return true;
}
return false;
}
}```
thats a valid answer too
I forget some pseudo famous quote that says if ur doing something more than 2 times use a lop
dont repeat yourself ? :p
yeah but something else
public static double getAveragePoints(){
Scanner input = new Scanner(System.in);
int totalPoints = 0;
for (int gameNumber = 1; gameNumber <= 3; ++gameNumber) {
System.out.print("Enter game " + gameNumber + " points: ");
totalPoints += input.nextInt();
}
return totalPoints / 3.0;
}```
could also name gameNumber to i but wanted to be more explicit since it has a meaning
careful on that code style though, its stopping at 4 with display
you should not use nextDouble in this context because basketball doesnt half non-integer points.
You should also never close the scanner.
If you intend to reuse the scanner, then make it static like the solution (or a class member when they get there)
euh why
because basketball dont have non-integer points.
(use case problem, not code problem)
I mean why not closing scanner ?
Even a code problem too because adding doubles could give .999999999 errors
If you close the scanner you can never reopen System.in
( aka dont predeclare variables like this instructor is doing )
I don't know why would I use scanner again, if I finish with it