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

Thread: loop , passed and failed

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default loop , passed and failed

    this program is an example of how a grade should compute
    heres the code

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package ConsoleApplicationsSamples;
     
    /**
     *
     * @author Administrator
     */
    import java.util.*;
    public class Sample
     
    {
        private static Scanner scanner = new Scanner(System.in);
     
        public static void main(String[] args)
     
        {
            int grades,
                keyIn;
     
            keyIn = 1;
     
            do
            {
                System.out.print("Enter the grades: 1 for passed / 0 for failed");
                grades = scanner.nextInt( );
     
     
                if(grades == 1)          
                    System.out.println("PASSED");         
     
                else if (grades == 0)           
                    System.out.println("FAILED");
     
                else if(grades > 1)          
                    System.exit(0);
     
                keyIn ++;
     
            }
            while(keyIn <= 10);
     
            if(grades <= 7)
                System.out.println("Stubborn");                  //if i entered 7 time the it should print this
     
     
            else if(grades >= 8)
                System.out.println("NAKS SIPAG MAG ARAL AHH");    // but if i entered greater than 7 times then it 
                                                                                                      // should print this
     
        }
     
    }


    the looping of the input is fine , the selection inside the loop is still fine...

    but the problem is the condition outside the loop, no matter how many 1(passed) or 0(failed) I enter
    it only prints the first condition ("Stubborn") it always ignore the second one.

    need help regarding this matter.. tnx a lot again


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: loop , passed and failed

    thats because you only ever allow te variable grades to be 1 or 0, so it cannot be greater than 7

    Chris

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: loop , passed and failed

    Maybe you had something like this in mind.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package ConsoleApplicationsSamples;
     
    /**
     *
     * @author Administrator
     */
    import java.util.*;
    public class Sample
    {
        private static Scanner scanner = new Scanner(System.in);
     
        public static void main(String[] args)
        {
            int grades,
                keyIn;
     
            keyIn = 1;
     
            do
            {
                if(keyIn == 7) {
                    System.out.println("Stubborn");                  //if i entered 7 time the it should print this
                }
                else if(keyIn >= 8) {
                    System.out.println("NAKS SIPAG MAG ARAL AHH");    // but if i entered greater than 7 times then it 
                }                                                                                        // should print this
     
                System.out.print("Enter the grades: 1 for passed / 0 for failed");
                grades = scanner.nextInt( );
     
                 keyIn ++;
            }
            while((grades < 0) || (grades > 1));
     
            if(grades == 1) {
                System.out.println("PASSED");
            }
            else if (grades == 0) {
                System.out.println("FAILED");
            }
        }
    }

    By the way, a tip when it comes to using System.exit, the integer status code you pass in should be non zero if abnormal termination, like the user put in the wrong argument otherwise zero if normal termination as in the program reached its destination.

    // Json

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: loop , passed and failed

    sori i didnt make things clear,heres the code again.

    import java.util.*;
    public class Sample
     
    {
        private static Scanner scanner = new Scanner(System.in);
     
        public static void main(String[] args)
     
        {
            int grades,
                keyIn;
     
            keyIn = 1;
     
            do
            {
                System.out.print("Enter the grades: 1 for passed / 0 for failed: ");
                grades = scanner.nextInt( );
     
     
                if(grades == 1)          
                    System.out.println("PASSED");         
     
                else if (grades == 0)           
                    System.out.println("FAILED");
     
                else if(grades > 1)          
                    System.exit(0);
     
                keyIn ++;
     
            }
            while(keyIn <= 10);
     
            if(grades <= 7)
                System.out.println("7 below total ofgrades are failed");                
     
            else if(grades >= 8)
                System.out.println("Above 8 you can proceed to next Level");:-*    
     
        }
     
    }

    the output should be like this


    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 0
    Enter the grades: 1 for passed / 0 for failed: 0
    Enter the grades: 1 for passed / 0 for failed: 0

    7 below total ofgrades are failed

    ---

    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 1
    Enter the grades: 1 for passed / 0 for failed: 0

    Above 8 you can proceed to next Level

    -----
    the problem with the output is . it only prints the first condition, this one.
    if(grades <= 7)
    System.out.println("7 below total ofgrades are failed");


    the program is not selecting the second condition even if i entered it the right way..
    but it reads the first one..

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: loop , passed and failed

    It sounds like you need a variable to store the number of passing grades in. When the loop is over, you can then put that variable into your conditionals.

    Edit: Because you are always entering either a 0 or a 1, the first "if" conditional will always print.

    import java.util.*;
    public class Sample
     
    {
        private static Scanner scanner = new Scanner(System.in);
     
        public static void main(String[] args)
     
        {
            int grades,
                keyIn;
           [B][U]int passCount=0;[/U][/B]
     
            keyIn = 1;
     
            do
            {
                System.out.print("Enter the grades: 1 for passed / 0 for failed: ");
                grades = scanner.nextInt( );
     
     
               if(grades == 1)[B][U]{[/U][/B]          
                    System.out.println("PASSED");
                   [U][B] passCount++;[/B][/U]
    [B][U]}[/U][/B]        
     
                else if (grades == 0)           
                    System.out.println("FAILED");
     
                else if(grades > 1)          
                    System.exit(0);
     
                keyIn ++;
     
            }
            while(keyIn <= 10);
     
            [B][U]if(passCount <= 7)[/U][/B]
                System.out.println("7 below total ofgrades are failed");                
     
           [B][U] else if(passCount>= 8)[/U][/B]
                System.out.println("Above 8 you can proceed to next Level");:-*    
     
        }
     
    }
    Last edited by cfmonster; August 24th, 2009 at 02:07 PM.

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

    chronoz13 (August 26th, 2009)

  7. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: loop , passed and failed

    ahh i get it cfmonster.. it always print because "0" and "1" are less than "7"(seven)

    thats why it always print the first condition!!

    and everytime that i INPUT "1" it doesnt add nor increment, and the value of "1" stays one.. even the next "1"s(ones)

    am i right sir?


    tnx sir!!!! tnx cfmonster!!!

    what a monster..
    Last edited by chronoz13; August 25th, 2009 at 07:06 AM.

Similar Threads

  1. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM
  2. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  3. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM