while (1) loop with continue and break statements [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need help with a while(1) loop that contains a continue and break statement. It must count from numbers 1 to 20 and for every even number, it must output the values. I have to use a continue after my writeToPage statement and use a break statement when it reaches 20.
This is what I tested out but the file will not even load:
writeToPage("Program 4: Continue and Break");
writeToPage("");
while(1) {
if (i % 2 == 0){
writeToPage(+ i);
continue;
}
if (i >= 20){
break;
}
}
I'm not sure if I'm putting them in the wrong place.

For infinite loop, you need to write while(true) instead of while(1).
If i % 2 != 0 is true, simply increment i by 1 and continue; otherwise, print the value of i and increment i by 1.
Demo:
public class Main {
public static void main(String[] args) {
int i = 1;
while (true) {
if (i % 2 != 0) {
i++;
continue;
} else {
writeToPage(i);
i++;
}
if (i >= 20) {
break;
}
}
}
static void writeToPage(int i) {
System.out.println(i);
}
}
Output:
2
4
6
8
10
12
14
16
18
20

Related

java program to loop and output 2,5,7,10,12 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12

#JAVA - Programme calculates the sum of numbers from 1 to 10,000 (including 1 and 10,000) omitting numbers numbers whose hundred digit is 2 or 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Hello, this is my code. I need to print number 1 to 10.000. But every number with 200 to 399 need to be hide. Exemple : 1269, 1325, 1234...
public static void main(String[] args) {
int nombre = 0;
while (nombre < 10000) {
nombre++;
if (nombre%200 == 0 || nombre - nombre%399 == 0) {
System.out.println("Le nombre est entre 100 - 300");
}
else if (nombre % 3 ==0) {
System.out.println("Le nombre est divisible par 3 ");
}
else
{
System.out.println(nombre);
}
}
}
}````
// Thanks for the help !`
You can use modulus to find the last 3 digits and then check that with your condition.
public printNum() {
int num = 0;
while (num < 10000) {
num++;
int last3Digits = num % 1000;
if (!(last3Digits >= 200 || last3Digits <= 399)){
System.out.println(num);
}
}
}
Hope this solves it

What is my mistake in this problem. Simple Comparison Error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Task
Given an integer, n, perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5 , print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
I have written a code but it is showing error in printing 18 and 20.
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.close();
if(N%2 != 0){
System.out.println("Weird");
}
else if(N%2 ==0 && N>=2||N<=5)
{
System.out.println("Not Weird");
}
else if(N%2 ==0 && N>=6||N<=20)
{
System.out.println("Weird");
}
else if(N%2 ==0 && N>20)
{
System.out.println("Not Weird");
}
}
}
You need to change your or condition || to and condition && to check for the range like
else if(N%2 == 0 && N >= 2 && N <= 5)

My Java code is not running [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
If I am not mistaken, this bit of code should print out all even numbers smaller than or equal to 100. When I run this code, nothing happens. No error message or anything. I'm using Eclipse.
public class Even {
public static void main(String args[]) {
int number = 1;
int remainder = number % 2;
while(number <= 100) {
number++;
if(remainder == 0) {
System.out.println(number);
}
}
}
}
As others stated before, value of remainder is set only once. However, you check it a hundred times expecting it to tell you something else every time. I'd suggest putting it inside a loop, so you get a "fresh" value for every number.
public class Even {
public static void main(String args[]) {
int number = 1;
int remainder;
while(number <= 100) {
number++;
remainder = number % 2;
if(remainder == 0) {
System.out.println(number);
}
}
}
}
The reminder doesn't change as the assignment is prior to while loop. The statement int remainder = number % 2; has to be inside while loop to see the expected result.
for(int i = 0;i<=100; i= i+2)
System.out.println(i);
You do not need remainder :)

How to print this pattern in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
**if argument multiply 3 print "JU"
if argument multiply 5 print "MP"
if argument multiply 3 and 5 print "JUMP"**
ex :
> java myclass 16
OUTPUT:
1
2
JU
4
MP
JU
7
8
JU
MP
11
JU
13
14
JUMP
16
// for each number in the sequence 1..N execute the following
for (int i = 1; i <= N; ++i) {
// print JU if divides by 3
if (i % 3 == 0) {
System.out.print("JU");
}
// print MP if divides by 5
if (i % 5 == 0) {
System.out.print("MP");
}
// note that if it divides by 3 and by 5 (by 15 actually)
// it print JU and MP in a row and it becomes JUMP
// if not dividable - print it itself
if (i % 3 != 0 && i % 5 != 0) {
System.out.print(i);
}
System.out.println();
}
This a variant of FizzBuzz and is a common challenge given to new programmers or in interviews. Why not have a look at the Rosetta Code's Java section on FizzBuzz to see a whole range of ways to do it. You'll obviously need to change Fizz for 'JU' and Buzz for 'MP'.
Try this
public static void getNumber(int upperLimit){
boolean status=true;
int i=1;
while (status) {
if(i%3==0){
System.out.println("JU");
}else if(i%5==0){
System.out.println("MP");
}else if(i%5==0&&i%3==0){
System.out.println("JUMP");
}else {
System.out.println(i);
}
if(i==upperLimit){
status=false;
}
i++;
}
}

Categories