boolean value in a simple loop (do-while)
Code :
public class NewMain {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int loopCount = 0;
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";
}
if ((Continue()) == true) {
loopCount++;
}
}
while (Continue()); //this part will ask me to continue, BUT IT SUPPOSED TO ASK ME ONCE, not TWICE
System.out.print("\n");
System.out.println("The Total Count Of Negative Numbers That Were Entered Is: " + negativeCount);
System.out.print("\n");
System.out.println(loopCount);
}
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;
}
}
}
why is it always asking me the boolean method two times? i cant get rid of it....
Re: boolean value in a simple loop (do-while)
ive already solved the mistaken part...
Code :
if ((Continue()) == true) {
loopCount++;
}
instead of making a condition to count the loop. i just simply define a count which is
just want to know a liitle infomation why is it running like that..?
i know its a liitle bit stupid to define the count of the loop in an IF condition if its 'false' or 'true' because the loop wont run (NOR COUNT) if its false... (it will only generate the loopCount or increment if it is true)
im just trying to make up something.. please.....
Re: boolean value in a simple loop (do-while)
You're calling the method continue twice. Each time, it will ask the user if you want to continue. What you should do is only call it once, and store the result of that call in a variable so you can access that same answer multiple times instead of asking the user for another answer.
Re: boolean value in a simple loop (do-while)
ahh but i supposed its not efficient... i just have to make a loop count... because it wont count if the loop will be false...
whahahahha no sense at all..... just count it.....(it will only count if its true) ryt? hehehehe
Re: boolean value in a simple loop (do-while)
If you don't want it to "over count" use a while loop:
Code :
while(continue())
{
loopCount++;
System.out.println("Enter a number: ");
if (Scanner.nextInt() < 0)
{
negativeNumbersCount++;
}
}
Re: boolean value in a simple loop (do-while)
ahh so it makes no sense if i subtracted 1 from the loopcount...
never thought of that using while loop
so it wont over count.. whahahah...