+ 1
Do while challenge
The challenge asked to make this in a do-while loop. I was not able to do this with a do-while loop but I was able to achieve this with a while and if loop. I was wondering if anyone could tell me what I was doing wrong or not understanding. Here was the problem Write a program that takes N numbers as input and outputs the numbers from N to 0, skipping the ones that are multiple of 3. Sample Input 7 Sample Output 7 5 4 2 1 0 https://code.sololearn.com/cJN9n08f7H5W/?ref=app
11 Antworten
+ 4
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
do{
if(number == 0){
System.out.println(number);
}else if(number%3==0){
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
}else{
System.out.println(number);
}
number--;
}while(number >= 0);
}
}
+ 2
Your code works with do while loop:
https://code.sololearn.com/c86OR3p6ImJK/?ref=app
+ 2
This program using For loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
//your code goes here
for (int i = number; i >= 0; i--){
if (i%3==0){
continue;
}else{
System.out.println(i);
}
}
System.out.println(0);
}
}
+ 1
Simple interchange condition in reverse
do{
if ( i ==3)
i--;
System.out.println(i);
i--;
if (i%3 == 0 && i != 0)
i--;
continue;
} while (i >=0 );
Syntax :
while(condition) {
//body
}
do{
//body
} while(condition) ;
+ 1
Dan Carney cuz 0/3 is also 0, so it doesnt get printed out
0
I'm struggling with this one, when I run the code it gives the output correctly except it will not return a value of 0 and I don't understand why.
When number decreases to value 1, it will go through the while loop, print 1, decrease to 0. Now number equals 0, the condition says that when number is equal to or greater than 0 to run the while loop. I expect that it would go through the while loop, and print.
What am I missing? Any help is appreciated.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
//your code goes here
while (number >= 0) {
if (number % 3 == 0) {
number--;
continue;
}
System.out.println(number);
number--;
}
}
}
0
I quite don't understand why "--i:" it is used at the end of every single block. WHy don't use it only one time in loop?
0
I was struggling too, that program doesn't print 0, but you need only to add:
if(number%3 != 0 || number == 0)
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
//your code goes here
do{
if(number % 3 != 0 || number == 0){
System.out.println(number);
}
number--;
}while(number >= 0);
}
}
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number;
//your code goes here
do{
number = read.nextInt();
for(number = number; number >=0; number--) {
if (number%3!=0 || number==0){
System.out.println(number);
}
}
}
while(number>0);
}
}
0
i used a for loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
for (int x = number; x >= 0; x--) {
if (x % 3 == 0 && x != 0) {
continue;
}
System.out.println(x);
}
}
}