-2

my console doesn't seem to be showing any output on my projects, on some, it does at first. I am new to this so I don't understand it, this is an example of my code

public class TestAmazing {

    public static void main(String args[]) {
        // Put your data type declarations below.
        int count = 0;
        double cost = 3.45;
        char choice = 'x';
        boolean goodChoice = true;
        short lowest = '5';

        // Put the code for your calculations in this method.
        // temp in a room

    }

    public void roomtemp() {
        int person = 1;
        int temp = 20 + (person);
        System.out.println("the temperature in the room is" + temp);
    }

    // nunber of jackpot ball
    public void bonusball() {
        int bonusball;
        bonusball = (int) (Math.random() * 59);
        System.out.println("the jackpot ball is " + bonusball);
        // population of china
    }

    public void currentpopulationofchina() {
        // check whether a game is finished or not
        long populationOfChina2017 = 1394200000;
        long populationexpectedincreaseto2019 = 5840000;
        long populationOfChina = populationOfChina2017 + populationexpectedincreaseto2019;
        System.out.println("the population of china is" + populationOfChina);
    }

    // check where a game is finished or not
    public void gameloadingstatus() {
        int gameLoading;
        gameLoading = (int) (Math.random() * 100);
        if (gameLoading == 100) {
            System.out.println("game is ready");
        } else {
            System.out.println("game is not ready");
        }
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110

2 Answers2

1

Replace your main method with the following code and it should work as you are expecting:

public static void main(String args[]) {
    // Put your data type declarations below.
    int count = 0;
    double cost = 3.45;
    char choice = 'x';
    boolean goodChoice = true;
    short lowest = '5';

    // Put the code for your calculations in this method.
    // temp in a room
    TestAmazing t=new TestAmazing();
    t.roomtemp();
    t.bonusball();
    t.currentpopulationofchina();
    t.gameloadingstatus();
}

The reason why it didn't work for you is because you have missed to call the methods you have created in the class. I have added the following lines in the main method to complete that missing part:

TestAmazing t=new TestAmazing();
t.roomtemp();
t.bonusball();
t.currentpopulationofchina();
t.gameloadingstatus();
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

None of your methods are static, so you'll need to create an instance of your TestAmazing class, and call methods via your instance.

That might look something like:

public static void main(String args[]) {
    TestAmazing test = new TestAmazing();
    test.roomtemp();
    test.bonusball();
    // etc...
}

Comment from QA:

this worked thank you! do I have to always do this and put it in the same place? – Joe Emery

Not necessarily. It depends on the requirements of your program. If all of your methods are STATIC then you don't need an instance of your class. In that case, then you'd simply call all of your methods directly from main, like this:

public class TestAmazing {

    public static void main(String args[]) {
        // Put your data type declarations below.
        int count = 0;
        double cost = 3.45;
        char choice = 'x';
        boolean goodChoice = true;
        short lowest = '5';

        // Put the code for your calculations in this method.
        // temp in a room
        roomtemp();
        bonusball();
        currentpopulationofchina();
        gameloadingstatus()
    }

    public static void roomtemp() {
        int person = 1;
        int temp = 20 + (person);
        System.out.println("the temperature in the room is" + temp);
    }

    // nunber of jackpot ball
    public static void bonusball() {
        int bonusball;
        bonusball = (int) (Math.random() * 59);
        System.out.println("the jackpot ball is " + bonusball);
        // population of china
    }

    public static void currentpopulationofchina() {
        // check whether a game is finished or not
        long populationOfChina2017 = 1394200000;
        long populationexpectedincreaseto2019 = 5840000;
        long populationOfChina = populationOfChina2017 + populationexpectedincreaseto2019;
        System.out.println("the population of china is" + populationOfChina);
    }

    // check where a game is finished or not
    public static void gameloadingstatus() {
        int gameLoading;
        gameLoading = (int) (Math.random() * 100);
        if (gameLoading == 100) {
            System.out.println("game is ready");
        } else {
            System.out.println("game is not ready");
        }
    }
}

Notice how every method has static in its declaration.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40