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 3 of 3

Thread: Simple Division not working heeeeelp

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

    Default Simple Division not working heeeeelp

    Doing a small quiz program. At the end trying to do a percentage but I get 0. Dont know why. The code is below:

    import java.util.Scanner;


    public class Quiz

    {

    public static void main(String[] args)
    {

    Scanner ms = new Scanner(System.in);

    System.out.println("HIP-HIP MUSIC QUIZ");
    System.out.print("\n1) What is the real name of famous hiphop Rapper 50 Cent ? \nA - Wayne Marshal \nB - Curtis Jackson \nC - Marion Jones \nD - Jamie Foxx\n");

    System.out.print("\nYour answer: ");
    String quest1 = ms.nextLine();

    int marks = 0;
    final int questions = 4;
    int percent = 0;

    switch(quest1)
    { case "A":
    System.out.println("Wrong !!! Correct Answer was: B - Curtis Jackson");
    break;
    case "B":
    System.out.println("Correct !!!");
    marks++;
    break;
    case "C":
    System.out.println("Wrong !!! Correct Answer was: B - Curtis Jackson");
    break;
    case "D":
    System.out.println("Wrong !!! Correct Answer was: B - Curtis Jackson");
    break;
    default:
    System.out.println("Sorry... Invalid Input");
    }

    System.out.print("\n2) The nickname Weezy is know to which artist ? \nA - Wayne Little \nB - Lil Wee \nC - Wesley Tip \nD - Lil Wayne\n");

    System.out.print("\nYour answer: ");
    String quest2 = ms.nextLine();

    switch(quest2)
    { case "A":
    System.out.println("Wrong !!! Correct Answer was: D - Lil Wayne");
    break;
    case "B":
    System.out.println("Wrong !!! Correct Answer was: D - Lil Wayne");
    break;
    case "C":
    System.out.println("Wrong !!! Correct Answer was: D - Lil Wayne");
    break;
    case "D":
    System.out.println("Correct !!!");
    marks++;
    break;
    default:
    System.out.println("Sorry... Invalid Input");
    }

    System.out.print("\n3) Which rapper is known as 'The King' ? \nA - T.I \nB - Taio Cruz \nC - King Shady \nD - Eminem\n");

    System.out.print("\nYour answer: ");
    String quest3 = ms.nextLine();

    switch(quest3)
    { case "A":
    System.out.println("Correct !!!");
    marks++;
    break;
    case "B":
    System.out.println("Wrong !!! Correct Answer was: A - T.I");
    break;
    case "C":
    System.out.println("Wrong !!! Correct Answer was: A - T.I");
    break;
    case "D":
    System.out.println("Wrong !!! Correct Answer was: A - T.I");
    break;
    default:
    System.out.println("Sorry... Invalid Input");
    }

    System.out.print("\n4) Chris Brown is a ________________ & _________________ ? \nA - Producer & Rapper \nB - Backup Singer & Vocalist \nC - Singer & Dancer \nD - Guitarist & Violin Player\n");

    System.out.print("\nYour answer: ");
    String quest4 = ms.nextLine();

    switch(quest4)
    { case "A":
    System.out.println("Wrong !!! Correct Answer was: C - Singer & Dancer");
    break;
    case "B":
    System.out.println("Wrong !!! Correct Answer was: C - Singer & Dancer");
    break;
    case "C":
    System.out.println("Correct !!!");
    marks++;
    break;
    case "D":
    System.out.println("Wrong !!! Correct Answer was: C - Singer & Dancer");
    break;
    default:
    System.out.println("Sorry... Invalid Input");
    }

    percent = ((marks / questions) * 100);
    System.out.println("You got " + percent+"%");

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: Simple Division not working heeeeelp

    trying to do a percentage but I get 0
    integer arithmetic does not return a fractional part. The result is truncated: 9/12 = 0, 5/2 = 2

    If you want decimal points, use a double in the arithmetic expression: 9.0/12 = .75, 5.0/2 = 2.5

    For your expression: (m/q)*100 multiple first then divide: (m*100)/q
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ashboi (September 8th, 2012)

  4. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Simple Division not working heeeeelp

    taking advice from norm is usually a good move.... just change two of ur variables to double at declaration because like he said ur results are getting truncated because int variables dont return decimal values.... either that or typecast percentage = ....; to (double) at the end and it should work

Similar Threads

  1. Division by Zero
    By mael331 in forum Java Theory & Questions
    Replies: 16
    Last Post: December 6th, 2011, 05:13 PM
  2. Simple code, cant get it working.
    By Malmen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2011, 02:28 PM
  3. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM
  4. Need Help Getting A Simple Stop-Watch Working?
    By logmein in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:57 AM
  5. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 11:40 AM

Tags for this Thread