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

Thread: Need Help with some code

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need Help with some code

    hey im trying to write a program that has a loop in it and takes the percentage of the total number of scores and the scores that passed which are above 70
    here's my code when i go to run it and input the scores and it comes up with a question mark as an answer
    <import java.util.Scanner;
    import java.text.NumberFormat;
    public class DANPercentPassage {
     
     
    	public static void main(String[] args) {
    		int score, pass, fail, total;
    		double percent;
    		percent = 0;
    		pass = 0;
    		fail = 0;
    		total = pass + fail;
    		Scanner sc = new Scanner(System.in);
    		NumberFormat per = NumberFormat.getPercentInstance();
    		per.setMinimumFractionDigits(2);
    		per.setMaximumFractionDigits(0); 
    		do{
    			System.out.print("Enter the score: ");
    		score = sc.nextInt();
    		if(score>=70)
    			pass = pass + 1;
    		else if(score>0&&score<70)
    			fail = fail + 1; 
    		}while(score!=-1);
    		percent = (double) pass/total;
     
    		if(score == -1)
    			System.out.print("The percent of the scores is "+ per.format(percent));
     
     
     
     
    	}
     
    }
    And heres my console window when i run it
     
    Enter the score: 80
    Enter the score: 80
    Enter the score: 35
    Enter the score: 35
    Enter the score: -1
    The percent of the scores is ?%>
    Get back to me ASAP


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help with some code

    Try printing the value of the variable: percent without formatting to see what its value is.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    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:

    dnewell15 (April 13th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with some code

    there you go thanks for the tip ill try and that and reply if i still can't figure it out

    --- Update ---

    when i do that it is saying that it is infinity

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help with some code

    That's what you get from dividing by 0
    If you don't understand my answer, don't ignore it, ask a question.

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

    dnewell15 (April 13th, 2013)

  7. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with some code

    ok i figured it out thanks for your help

Similar Threads

  1. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  2. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  3. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM