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

Thread: Little Loop Problem

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

    Default Little Loop Problem

     
    package xTestsx;
     
    import java.util.*;
     
    /**
     *
     * @author  JSoft
     */
     
    public class NegativeCounts_UF {
     
        private static Scanner sc = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            int repeatCount = - 1;
     
            int negativeCount = 0;
     
         /*   int num1Count = 0,
                num2Count = 0,
                num3Count = 0;
         */
     
            int num1,
                num2,
                num3;
     
            int totalCount = 0;
     
            String input1 = "",
                   input2 = "",
                   input3 = "";
     
            do {
     
                System.out.print("Num1: ");
                num1 = sc.nextInt();
     
                System.out.print("Num2: ");
                num2 = sc.nextInt();
     
                System.out.print("Num3: ");
                num3 = sc.nextInt();
     
                if (num1 < 0) {
     
                    negativeCount++;
                   // num1Count++;
     
                    input1 = "Num1" + " ";
                }
     
                if (num2 < 0) {
     
                    negativeCount++;
                   // num2Count++;
     
                    input2 = "Num2" + " ";
                }
     
                if (num3 < 0) {
     
                    negativeCount++;
                   // num3Count++;
     
                    input3 = "Num3" + " ";
                }
     
                repeatCount++;
     
                /**
                 * Check if the Count of negative inputs is greater than 1
                 * then the output of string must be in plural form
                 */
                if (negativeCount > 1) {
     
                    System.out.println(input1 + input2 + input3 + "Are Negative!");
                }
                /**
                 * Otherwise if the negative input is equals to 1 then the output of
                 * string must be in singular form
                 */
                else if (negativeCount == 1) {
     
                    System.out.println(input1 + input2 + input3 + "Is Negative!");
                }
                /**
                 * Otherwise there are no negative inputs
                 */
                else {
     
                    System.out.println("There Are No Negative Inputs!");
                }
            }
            while (Continue());
     
            //Display the count of the negative numbers that the user entered
            System.out.print("\n");
            System.out.println("The Total Count Of Negative Numbers That Were Entered Is: " + negativeCount);
     
            //Dispay how many counts the user repeated in entering 3 numbers
            System.out.print("\n");
            System.out.println("The Total Repeat Count Where You Entered Negative Numbers Is : " + repeatCount);
        }
     
        /**
         * Ask the user if he wants to continue
         *
         * @return true if the user entered ignored case of "Y" or "YES",
         *         else if the user entered ignored case "N" or "NO" return false,
         *         otherwise return false
         */
        public static boolean Continue() {
     
            String cont;
     
            System.out.print("\n");
            System.out.print("Do You Want To Enter Again?: ");
            cont = sc.next();
     
            if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
     
                System.out.print("\n");
                return true;
            }
            else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
        }
    }


    expected out put should be :

    this one is fine
    Num1: -2
    Num2: 4
    Num3: 3
    Num1 Is Negative!


    but this one
    Num1: -2
    Num2: 4
    Num3: 3
    Num1 Is Negative!
     
    Do You Want To Enter Again?: YES
     
    Num1: 2
    Num2: 3
    Num3: -4
    Num1 Num3 Are Negative! //this part !! 
                                                //its still count the previews negative count..
     
    Do You Want To Enter Again?: NO
     
    The Total Count Of Negative Numbers That Were Entered Is: 2 //this part is fine
     
    The Total Repeat Count Where You Entered Negative Numbers Is : 1// this one too


    have you notice the logic error? i hope you understand.. but im stuck with this..

    what i want is... every time i loop it will only display the numbers that were negative not the previews negative numbers that i entered ..

    EXPECTED OUTPUTt:
    Num1: -2
    Num2: 3
    Num3: 4
    Num1 Is Negative!
     
    Do You Want To Enter Again?: YES
     
    Num1: 3
    Num2: 4
    Num3: -1
    Num3 Is Negative!
     
    Do You Want To Enter Again?: YES
     
    Num1: 2
    Num2: -3
    Num3: -4
    Num2 Num3 Are Negative!
     
    Do You Want To Enter Again?: NO
     
    The Total Count Of Negative Numbers That Were Entered Is: 2
     
    The Total Repeat Count Where You Entered Negative Numbers Is : 0


    help me with little algorithm please


  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Little Loop Problem

    i'm not 100% sure , but after each of the
    System.out.println(input1 + input2 + input3 + "Are/Is Negative!");
    add
    input1 = "";
    input2 = "";
    input3 = "";
    that should fix it
    Programming: the art that fights back

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

    chronoz13 (October 17th, 2009)

Similar Threads

  1. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  2. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM
  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