Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help with switch case

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with switch case

    Hello im having trouble with a code im trying to make. What im trying to do is if the user select 'a' instead of 'b' they would get a 05 percent discount off of 3000 dollars. how can i do this with switch case or with else if

    I started with
    example
    System.out.print("select a or b?");

    what do i put now? i want to put if user selects 'a' they will receive a 5 percent discount off 3000 dollars
    thanks
    Last edited by niko25; October 17th, 2012 at 09:20 PM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Need help with switch case

    Hey! If you want to use the switch or if/else if statements, I'd recommend reading The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics) and The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics). If you have any other questions, please don't hesitate to ask.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with switch case

    this is what i have so far but i dont think its correct.

    import java.util.Scanner;

    import javax.swing.JOptionPane;
    public class InsuranceQuotes {

    private static String status;

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner keyboard = new Scanner (System.in);

    System.out.print("Are you male or female? Answer m or f:");
    char appGender = keyboard.next().charAt(0);
    short insurance = 4000;
    double discount = 0.5 ;

    if( appGender=='f')
    {
    discount = 5 / 100*insurance;
    }
    else if( appGender=='m')
    {
    discount = 0;
    }
    double inTotal = (discount - insurance);
    System.out.println ("your total is " + inTotal);

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Need help with switch case

    I think you should try using println statements to see what's wrong. Try printing out your variables (i.e. discount, insurance). Try and see what happens to discount and insurance when you enter 'f', then see what happens you enter 'm'. Also, consider this:

    int/int = int
    double/int = double
    int/double = double
    double/double = double

    Also, short ranges from -32,768 to 32,767. Just something to think about when you do your calculations

Similar Threads

  1. Please help me with my Case Study
    By dalexquisite in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2012, 11:58 PM
  2. Switch Case Problem help
    By Kikiam in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 18th, 2012, 06:11 PM
  3. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM
  4. Ranging Case Values in Switch Construct
    By WhiteSoup12 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 6th, 2011, 11:50 AM
  5. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 11:54 PM