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