#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Need help with boolean


    The problem is,
    As activity directory at Lake LazyDays Resort, it is your job to suggest appropriate activities to guests based on the weather:
    temp >= 80: swimming
    60 <= temp < 80: tennis
    40 <= temp < 60: golf
    temp < 40: skiing
    1. Write a program that prompts the user for a temperature, then prints out the activity appropriate for that temperature. Use a cascading if, and be sure that your conditions are no more complex than necessary.
    2. Modify your program so that if the temperature is greater than 95 or less than 20, it prints "Visit our shops!". (Hint: Use a boolean operator in your condition.) For other temperatures print the activity as before.

    How would i do the boolean because i have no idea how they work this is the current code i have,



    Code:
    import java.util.*;
    
    public class Charge
    {
    public static void main(String [] args)
    {
    double temp = 0;
    System.out.println("What is the temperature:");
    Scanner scan  = new Scanner(System.in);
    temp = scan.nextDouble();
    if (temp >= 80.0)
    {
    System.out.println("Swimming");
    }
    else if (temp >= 60.0 && temp <= 80.0)
    {
    System.out.println("Tennis");
    }
    else if (temp >= 40.0 && temp <= 60.0)
    {
    System.out.println("Golf");
    }
    else if (temp < 40)
    {
    System.out.println("Skiing");
    }
    else if (temp >= 95 || temp <= 20)
    {
    System.out.println("Visit our shops!");
    }
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    i have no idea how they work
    The boolean operators: AND (&&), or (||) and ! are used to combine boolean expressions to return a single boolean value:
    For example:
    (a>b) && (c<d) would return true if a is greater than b AND c is greater than d
    (a>b) || (c<d) would return true if a is greater than b OR c is greater than d
    !(a>b) would return true if a is NOT greater than b (ie less or equal)

    Please edit your code and properly format it to show logic nesting levels. Statements within {} should be indented.

    Can you explain what happens when you execute your program?
    Post the contents of the console that shows what you enter and what it prints out and explain what is wrong.

    In windows To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Last edited by NormR; November 7th, 2012 at 01:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo