Reputation:
I am teaching myself Java and have got to the stage of learning how to use methods and I am struggling. One of the tutorials I am doing has asked me to create a simple scoring system. Could someone please look at my code and give me some advice on how I could get it working and let me know what I have done wrong.
import.java.util.scanner;
public class GameScorer
{
int score;
String result;
public static void main (String[] args)
{
printHeader();
getScore();
setScore();
}
public void printHeader()
{
System.out.println("Game Scorer");
System.out.println(" ");
System.out.println(" ");
}
public void getScore()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter score :> ");
score = in.nextLine();
}
public void setScore(int score)
{
if (score >= 100)
System.out.println("Fantastic");
else
if (score >= 90)
System.out.println("Brillaint");
else
if (score >= 60)
System.out.println("Good");
else
if (score >= 40)
System.out.println("Bad");
else
if (score >= 0)
System.out.println("Awful");
}
}
Upvotes: 1
Views: 2652
Reputation: 1
If you have not learned about arguments in java. Just another word for the stuff inside the (). I would strongly recommend this video:
If you have learned about these then you just made a simple mistake. When you call the method
setScore(int score)
, the method is expecting an int
to be taken in. That is why you need to change setScore();
to setScore(score);
Upvotes: 0
Reputation: 1
There are actually a number of incorrect and confusing parts to this simple example:
The main method can't see any of the other methods - main is static (common and shared by all instances of the GameScorer class) and printHeader(); getScore(); setScore(); are not - they are instance methods - these are specific to the data in each instance of the GameScorer class.
Because the main method is static, and the variables you declared in GameScorer are not static, they are not visible to main.
Because the other methods are NOT static, they CAN see the variables score and results.
You were also inconsistent in your use of the score variable, since you have one in the GameScorer class AND one as a parameter (this is legal, but contributed to your confusion).
You will want to read up more on what static means, what objects are and what/how instances of objects (and instance variables and methods) differ from classes...
Code will probably illustrate best... There are many ways you can fix this,
one approach would be (embracing static methods):
import java.util.Scanner;
public class GameScorer {
private static int score;
public static void main(String[] args) {
printHeader();
getScore();
setScore();
}
private static void printHeader() {
System.out.println("Game Scorer");
System.out.println(" ");
System.out.println(" ");
}
private static void getScore() {
Scanner in = new Scanner(System.in);
System.out.println("Enter score :> ");
String input = in.nextLine();
score = Integer.parseInt(input);
}
private static void setScore() {
if (score >= 100)
System.out.println("Fantastic");
else if (score >= 90)
System.out.println("Brillaint");
else if (score >= 60)
System.out.println("Good");
else if (score >= 40)
System.out.println("Bad");
else if (score >= 0)
System.out.println("Awful");
}
}
another (embracing objects and instance methods):
import java.util.Scanner;
public class GameScorer {
private int score;
public static void main(String[] args) {
GameScorer gs = new GameScorer();
gs.printHeader();
gs.getScore();
gs.setScore();
}
private void printHeader() {
System.out.println("Game Scorer");
System.out.println(" ");
System.out.println(" ");
}
private void getScore() {
Scanner in = new Scanner(System.in);
System.out.println("Enter score :> ");
String input = in.nextLine();
score = Integer.parseInt(input);
}
private void setScore() {
if (score >= 100)
System.out.println("Fantastic");
else if (score >= 90)
System.out.println("Brillaint");
else if (score >= 60)
System.out.println("Good");
else if (score >= 40)
System.out.println("Bad");
else if (score >= 0)
System.out.println("Awful");
}
}
Upvotes: 0
Reputation: 5818
First, pass an argument to the setScore()
like:
setScore(score)
next, make the methods getScore()
, setScore(int sth)
- static. You cant call non-static methods from the static ones (your main
method is static). Just put a word static
in the declaration of methods:
public static void printHeader()
{
...
}
public static void getScore()
{
...
}
public static void setScore(int score) {
...
}
Upvotes: 0
Reputation: 95948
setScore();
- No match method to call. You should pass an argument to this method as it's signature is:
public void setScore(int score)
↑
Expecting an argument of type int
In the main
method do:
setScore(score);
I advise you to go through a basic tutorial.
Another thing, you should make your methods static
as you are calling them from a static
method.
Upvotes: 2