Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make the returned integer value in this program print a message, depending on the value?

I have semi-successfully coded this program. As it stands, it prints an integer value as a result of what you type in. The only results can be 0, 1, or 2. I am wondering how to make java display a message for each one of those 3 integer values. For instance, a returned result of 0 might make it print "You received no ticket", while a result of 2 would say "You’re going to jail, buddy!" Any tips on how I could do this?

I know the answer is probably very simple; bear with me, as I am just starting out 🙂

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.Scanner;

public class SpeedingBool {

   public static void main(String[] args) {
   
      Scanner input = new Scanner(System.in);
      
      System.out.println("Your ticket is " + caughtSpeeding(input));
      
   }
   
   public static int caughtSpeeding(Scanner input) {
      System.out.println("How fast were you going?");
      int speed = input.nextInt();
      System.out.println("True or false: today is your birthday");
      boolean isBirthday = input.nextBoolean();
      int ticket;
      if (isBirthday) {
         if (speed <= 65)
            ticket = 0;
         else if (speed > 65 && speed <= 85)
            ticket = 1;
         else ticket = 2;
      }
      else {
         if (speed <= 60)
            ticket = 0;
         else if (speed > 60 && speed <= 80)
            ticket = 1;
         else ticket = 2;
      }
      return ticket;
   }

}

>Solution :

You can just go with simple if-else on returned value. I am giving you an example with some minor logical improvements.

else if (speed > 65 && speed <= 85)
                ticket = 1;

this line will only reach is the speed is already greater than 65. So you can do this:

else if (speed <= 85)
                ticket = 1;

Similar for this:

else if (speed > 60 && speed <= 80)
                ticket = 1;

Make it like this:

else if (speed <= 80)
                ticket = 1;

And finally the full code:

import java.util.Scanner;

public class SpeedingBool {

    public static void main( String[] args ) {

        Scanner input = new Scanner(System.in);

        int i = caughtSpeeding(input);

        String ticket;

        if (i == 0)
            ticket = "You received no ticket";
        else if (i == 1)
            ticket = "Return value 1 ticket";
        else
            ticket = "You're going to jail, buddy!";

        System.out.println("Your ticket is " + ticket);

    }

    public static int caughtSpeeding( Scanner input ) {
        System.out.println("How fast were you going?");
        int speed = input.nextInt();
        System.out.println("True or false: today is your birthday");
        boolean isBirthday = input.nextBoolean();
        int ticket;
        if (isBirthday) {
            if (speed <= 65)
                ticket = 0;
            else if (speed <= 85)
                ticket = 1;
            else ticket = 2;
        } else {
            if (speed <= 60)
                ticket = 0;
            else if (speed <= 80)
                ticket = 1;
            else ticket = 2;
        }
        return ticket;
    }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading