An issue in using while loop with multiple conditions - java

I have the following Java while loop:
boolean finish = false;
int ii = 0;
int counter = 0;
while((!finish) || (counter <= 10)) {
ii++;
if(ii<30) {
System.out.println(ii + " -- " + counter);
}else {
finish=true;
}
counter++;
}
I want the loop to add one to ii until it reaches 30 or the counter reaches 10. Running this code ignores the condition of counter and continues until ii reaches 30. I expect it to stop when counter reaches 10.
How can I fix that?

It should be &&, not ||, since you want the loop to loop as long as both ii<30 AND counter<=10.

change while((!finish) || (counter <= 10))
to
while((!finish) && (counter <= 10))
and it will work as you expect.

Related

How can I make it so my while-loop only prints even numbers? Java Eclipse IDE

Beginner here. For my coding class, we have an assignment that requires us to print numbers 1-20, but configure it so that it only outputs even numbers. Here is what I have so far but I'm quite stuck. He says to put an if statement and use the "%" operator but I've no idea where to put them.
int counter = 1;
System.out.println("Part 2 - Even Numbers");
while (counter <= 20)
{
//if (counter
System.out.printf("%d ", counter);
counter++;
} // end while loop
Instructions for assignment
My Output
CORRECT Output
if(counter % 2 == 0){
System.out.printf("%d ", counter);
}
counter++;
% operator is mod operator, if counter % 2 == 0 , then counter is an even number
% is an arithmetic operator, it is called MODULO.
Modulo operator returns the remainder of 2 numbers. In this case, we use a modulo to find out whether a number is even or odd.
odd%2 returns 1
even%2 returns 0
The while loop loops through the first 20 elements. So we put an if statement before printing the element. If the counter is an even number i.e (counter%2 == 0) we print that.
This is the code that prints even numbers:
int counter = 0;
System.out.println("Part 2 - Even Numbers");
while (counter <= 20)
{
if (counter%2 == 0){
System.out.printf("%d ", counter);
}
counter++;
} // end while loop
This can also be done without using MODULO operator:
int counter = 0;
System.out.println("Part 2 - Even Numbers");
while (counter <= 20)
{
System.out.printf("%d ", counter);
counter+=2;
} // end while loop
use fori
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
% is the remainder operation

My While loop doesn't end even when the condition is met

So as an assignment I am trying to build a Collatz Conjecture program. My program works as expected, but the only problem is that it doesn't end when it reaches 1. Instead, the code loops and the program seem to start from the beginning in its own loop. Below attached is my code. Any help is much appreciated. Thank You!
while (n!=1){
if ((n % 2) == 0) {
n/=2;
System.out.println(n);
}
if ((n % 2) > 0) {
n*=3;
n++;
System.out.println(n);
}
n = n;
}
You are running both conditions in each iteration, so even if the first condition updates n to 1, the following condition immediately changes its value to becomes larger than 1 again.
You should only run the second condition's body if the first condition is false, which means you can transform the second condition to an else clause:
if ((n % 2) == 0) {
n/=2;
System.out.println(n);
} else {
n*=3;
n++;
System.out.println(n);
}
Example:
Suppose n==2 - the first condition is true, and it modifies n from 2 to 1. Now, in the same iteration, the second condition becomes true, and n is multiplied by 3 and incremented by 1, so the loop doesn't end.
You should do it like this,the problem occurs because you updated your n in every condition and loop continue to run and that is why your program is not running.
int m=0;
while (n!=1){
if ((n % 2) == 0) {
m=n/2;
System.out.println(m);
}
if ((n % 2) > 0) {
m=n*3;
m++;
System.out.println(m);
}
n = m;
}

Supposed variable cannot be resolved as variable?

I'm doing a basic Java tutorial and below is the question.
Write a method that prints the numbers from 1 to 100. But for multiples of three print ÒFizzÓ instead of the number,and for the multiples of five print ÒBuzzÓ. For numbers which are multiples of both three and five print ÒFizzBuzzÓ."
My code is below
public static void fizzBuzz(){
for(int i = 0; i < 101; i= i +1 )
System.out.println(i);
if (i%3 == 0){
System.out.println("ÒFizzÓ");
}else if (i % 5 == 0){
System.out.println("ÒBuzzÓ");
}else if (i % 15 == 0){
System.out.println("ÒFizzBuzzÓ");
}
}
Eclipse tells me that "i" cannot be resolved as a variable. This is confusing to me as I thought I already defined "i" as an integer in my for loop? Thanks for taking the time to solve this newbie question :)
Add braces or your loop body ends after the first statement. Also, for your approach you need to test 15 first because it's a multiple of 3 and 5
for(int i = 0; i < 101; i++) { // <-- i++ is short for i = i + 1
System.out.println(i);
if (i % 15 == 0) {
System.out.println("ÒFizzBuzzÓ");
} else if (i % 5 == 0) {
System.out.println("ÒBuzzÓ");
} else if (i % 3 == 0) {
System.out.println("ÒFizzÓ");
}
}
I know a funny story about Apple who lost a few million dollars because a developer updated a code with an if block but... the if statement had only one instruction and no curly brackets and he did not see it. Thus, the code he was willing to add when the condition was met were actually ALWAYS executed.
In your case, you won't lose money but you surely did the same mistake :
for(int i = 0; i < 101; i= i +1 ) {
System.out.println(i);
if (i % 15 == 0){
System.out.println("ÒFizzBuzzÓ");
} else if (i%3 == 0){
System.out.println("ÒFizzÓ");
} else if (i % 5 == 0){
System.out.println("ÒBuzzÓ");
}
}
When Java says something cannot be resolved as a variable, it is usually been used outside the scope it was declared or it was not declared at all.In your case, your braceless for-loop is causing the problem.

Postincrement in java expressions

In this example:
int i = 1;
while(i < 10)
if(i++%2 == 0)
System.out.println(i);
Why is the output 3,5,7,9 and not 2,4,6,8?
The condition is performed on the previous value of i, before it is incremented (which is even), but the output is done on the incremented value of i (which is odd).
The ++ operator applied after a variable returns the value of the variable and increments the variable after the expression is evaluated. The semantic is the same than this:
int i = 1;
while(i < 10) {
boolean cond = i % 2 == 0;
i = i + 1;
if(cond) {
System.out.println(i);
}
}
The post-increment operator, uses the current value of its operand in the expression and then increments it.
We can break this down using a literal value of '2' for example.
Basically this is what your present code is doing:
int i = 2;
if (i % 2 == 0) //true, 2 % 2 = 0
i = i + 1; //i now becomes 3
System.out.println (i);
OR to make it simpler, if we remove the loop and put back your code
int i = 1;
if (i++ % 2 == 0) //1 % 2 != 0
System.out.println (i); //Nothing will print for the if statement
System.out.print i; //Will print 2, because this print statement is outside
//the body of the if-statement
to get the output that you are looking for, you will have to use the prefix-increment operator (++i)
int i = 1;
if ( ++i % 2 == 0)
System.out.println (i);
this is equivalent to
int i = 1
if ( (i + i) % 2 == 0) //++i increments i and then uses it in the expression
System.out.println (i);

I want to print a parttern of X or * using while loop. it should look like

*
*****
*********
*********
*********
*********
here is my code but it is not working
final int WIDTH = 6;
final int HEIGHT = 9;
int i = 0;
int j = 0;
while (i < HEIGHT * 2 && j < WIDTH) {
if ((i + (i % 2) + (WIDTH) / 2) < j // right slope
|| (i + (i % 2) + j) < (WIDTH) / 2)// left slope
{
System.out.print(" ");
}
else {// solid then
System.out.print("*");
}
}
System.out.println();
i+=2;
j++;
Your incrementers i and j are currently outside your while loop closing bracket (}).
Since they are never incremented, the while condition never hits.
Common mistake that is causing an infinite loop.
Also the + (i % 2) part of your if conditional is pointless since you are incrementing i by 2 each time as any even number % 2 is 0.
My suggestion is to trace the code by hand, start with smaller values perhaps, but this will help you understand what is going wrong.
As I mentioned in my comment, you must increment the values in your while() loop if you don't wish it to be infinite. So, perhaps add them in at the bottom!
while (i < HEIGHT) {
if (i == 0) {
System.out.print(" *");
} else if (i == 1) {
System.out.print(" *****");
} else {// solid then
System.out.print("**********");
} System.out.println("");
i++;
}
Edit: I also changed your check conditions to make them simpler. If you only have to print this one shape it should be fine, but if you will be making a variety of sizes you will need to improve your algorithm!

Categories

Resources