when I run this program it get stuck in a loop of asking me to "Enter Value: " and continues to add one to the sum. Although this is exactly what it is supposed to do, if I enter a number that is divisible by 6 or 17 the loop doesn't end. Can explain why?
import java.util.Scanner;
public class DivisibleBy6or17 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter Value: ");
int one = in.nextInt();
int sum=0;
while (one % 6 != 0||one % 17 != 0) {
System.out.print("Enter Value: ");
one = in.nextInt();
sum++;
}
System.out.print("Numbers read: " + sum);
}
}
You should use "&&" instead of "||":
while (one % 6 != 0 && one % 17 != 0) {
Your old condition only stops the loop if the number if divisible by 6 and 17.
In this conditional, you are using an OR; to leave the while loop, you would have to have both one % 6 == 0 and one % 17 == 0. If you enter 102, you should leave the loop.
To fix, use && instead of ||.
The condition has an error, the correct condition is:
while(!(one % 6 == 0 || one % 17 == 0))
or
while(one % 6 != 0 && one % 17 != 0)
I think you will have to use Short-Circuit And instead of OR.
while (one % 6 != 0 && one % 17 != 0)
This is just a suggestion, but why don't you try to create a method that will do the task you are attempting to do rather than put the codes in the main method. The reason for this is to practice re-usability. Here is what I mean:
public static void main(String[] args) {
//Enter code here
//Method Calls here
}
public someMethod here(arguements if needed)
{
//Body here
}
Related
There's one hidden test case that this code doesn't work and I don't know why. [EDITED] Sorry for the lack of information. I'm using an app to learn Java and this is a coding test, there are 4 total test with 4 different inputs, the first 3 one is fine but the last one (hidden so I don't know the input and output) told me that it's wrong but I don't know what's wrong in my code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
int x = 3;
while(number > 3 && x <= number)
{
if(x%3==0 || x%10==3)
{
System.out.println(x);
}
++x;
}
}
}
If number = 3, your while loop does not run, and since 3 is divisible by 3 it should be printed.
You can just remove that part from your while loop, and leave this.
while(x <= number)
In your code you just ignoring when the number is 3. Your condition number > 3 is ignoring when the number == 3.
DO like this while(number >= 3 && x <= number) instead of while(number > 3 && x <= number)
while(number >= 3 && x <= number)
{
if(x%3==0 || x%10==3)
{
System.out.println(x);
}
++x;
}
One thing is x>3 condition is wrong as x==3 is also true.
Other thing is, why would you use an extra condition and increase complexity when you can just do a simple for loop/ while loop from 0-number and check if number %3 or number%10 is true?
i set an if to check if the numbers being sent we're divisible by 3 or 7 or so on..
but it doesn't seem to do that.
i tried changing how it worked which is why it looks like this now but it still doesn't work.
public void primeNumbers() {
System.out.println("Enter the amount of prime numbers you'd like: ");
int numberOfPrimes = reader.nextInt();
int numbersFound = 0;
int foundCount = 0;
while(foundCount < numberOfPrimes) {
if (numbersFound < 2) {
numbersFound++;
}
else if(numbersFound % 3 == 0 || numbersFound % 5 == 0 || numbersFound % 7 == 0 || numbersFound % 11 == 0 || numbersFound == 2) {
System.out.print(numbersFound +" ");
foundCount++;
numbersFound++;
}
else {
numbersFound++;
}
}
}
no errors, it's just the numbers coming out aren't prime.
I figured out why it wasn't working.
I just forgot to set a condition that said that if the number is divisible by 2, then skip.
And instead of making all the numbers that are divisible by 3 7 5 and 11 be printed
i make them not be printed
im pretty sure
Sorry lol
I'm doing a hackernet challenge where n is an int input. The conditions are:
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.
Im sure the code makes logic and dont think theres syntax. It gives the correct responses and hackernet still says its incorrect so ive come here to see if anyone can see what the problem is
public static void main(String[] args)
{
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20)
{
System.out.print("weird");
}
else
{
System.out.print("not weird");
}
}
The problem is the logic in your else condition, which would also catch values of N which are less than 2. Try this version:
if (N % 2 != 0)
{
System.out.print("weird");
}
else if (N >= 2 && N <= 5 || N > 20)
{
System.out.print("not weird");
}
else if (N >= 6 && N <= 20)
{
System.out.print("weird");
}
else
{
// NOTE: if the code still fails, remove this else condition
System.out.print("unexpected value of N");
}
Note: To get your code to pass the Hackernet task, you might have to completely remove the else condition. I added it for completeness, but Hackernet might test N=1 to see if nothing gets printed.
Read this condition :
if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20)
as
if (N % 2 != 0 || (N % 2 == 0 && N >= 6 && N <= 20))
Then see how operator precedence changes the behaviour and yield desired results.
Check the following one
public static void main(String[] args)
{
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2!=0) {
System.out.print("weird");
}else if(N>=2 && N<=5) {
System.out.print("not weird");
}else if(N>=6 && N<=20) {
System.out.print("weird");
}else if(N>20) {
System.out.print("not weird");
}
}
For the technical part: start by reading about
precedence of java operators and then make your code easier to read.
Pushing that many conditions into a single if is not helpful. You see it yourself: you think the code is correct, but probably it isn't. And now you look to other people to explain your overly complex code back to you. And of course, all the other answers do all that for you ... but beyond that:
The "real" answer here is: learn how to test your code.
Instead of having a main that somehow asks for a number, and then makes decisions, write a method boolean isWeird() that takes a number and returns true/false according to your requirements.
And then simply test that method with all reasonable cases. And then check if that result is as expected.
Using JUnit, you could write something like
assertThat(isWeird(1), true);
assertThat(isWeird(21), true);
assertThat(isWeird(22), true);
...
Ideally, you write such tests before you implement that method. And then you implement all the conditions, and any check that fails tells you that you got something wrong.
I feel, In the if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20) condition, you are verifiying the odd and even values at same time using && and || operator. Can you modify the condition into like this if (N % 2 != 0 || (N % 2 == 0 && N >= 6 && N <= 20)) and check? If N is odd weird will be printed or if N is even and it falls under the 6 and 20 inclusive, weird will be printed.
You already have a good few answers here but if you think logically about what you actually need, you can break it down easier.
It looks like the only "Not Weird" print out is 2, 4 and even numbers > 20
So an example could be something like:
if (n % 2 == 0) {
if ((n >= 2 && n <= 5) || (n > 20)) {
return "Not Weird";
}
}
return "Weird";
You can try this
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (n % 2 == 1 || (n >= 6 && n <= 20)) {
System.out.println("Weird");
} else {
System.out.println("Not Weird");
}
scanner.close();
}
Output that I want: 10 20 30 40 50..............
Output that I get: 0
public class HelloWorld
{
public static void main(String[] args)
{
final int n = 50;
int i= 0;
while(i <= n && i % 10 == 0 )
{
System.out.println(i);
i++;
}
}
}
while(i <= n && i % 10 == 0 )
This is the continuation condition which is two expressions connected by logical-and &&.
That means both must be true for the whole thing to be true.
Now work out the two sub-expressions for when i becomes 1. The first will be true but not so the second (1 is not a multiple of 10), meaning the loop will exit at that point. That explains why you're only seeing 0.
To fix it, you need to separate the two sub-expressions since the loop control depends only on the first. However, you still only want printing to happen for multiples of ten (the second).
So, assuming as per your desired output 10 20 30 40 50, you don't want 0 as one of the outputs (despite it being, after all, a multiple of 10), the following pseudo-code will do the trick:
set n to 50, i to 1
while i is less than or equal to n:
if the remainder when i is divided by 10 is 0:
output i
increment i
If you do want 0 included in the output, simply set i to 0 initially, and you'll see 0 10 20 30 40 50.
I've left the code above as pseudo-code on the assumption this is classwork of some description - it should be relatively easy to turn that into any procedural language.
i % 10 == 0
This will evaluate to false on the second loop so the while wont continue. I think you want this...
final int n = 50;
int i= 0;
while(i <= n)
{
if (i % 10 == 0) {
System.out.println(i);
}
i++;
}
This allows i to increment all the way up to n, but it will only print results when i % 10 == 0
You are trying to use the while and both a while and an if. Try
public class HelloWorld {
public static void main(String[] args) {
final int n = 50;
int i = 0;
while (i <= n) {
if (i % 10 == 0) {
System.out.println(i);
}
i++;
}
}
}
Loop run for once only for i=0. That's it.
I got a question about this program, it says: The FizzBuzz Challenge: Display numbers from 1 to x, replacing the word 'fizz' for multiples of 3, 'buzz' for multiples of 5 and 'fizzbuzz' for multiples of both 3 and 5. Th result must be:1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 ...
So my problem is at the time to print the output, I dont know what to do.
public class Multiplos {
public static void main(String args[]) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.print(i + " ");
System.out.print(" fizz ");
}
if (i % 5 == 0) {
System.out.print(" " + i);
System.out.print(" " + "buzz ");
}
if((i % 3 == 0)&&(i % 5 == 0)){
System.out.print(i + " ");
System.out.print(" fizzbuzz ");
}
}
}
}
Here's the pseudocode:
for i in 1 to 100
if(i % 5 == 0) AND (i % 3 == 0) print 'fizzbuzz'
else if(i % 3 == 0) print 'fizz'
else if(i % 5 == 0) print 'buzz'
else print i
I'll leave it as an exercise for you to convert it into Java, as that might help with the understanding as to how this works.
The problem is of course that when (i % 3 == 0)&&(i % 5 == 0) is true, the two preceding conditions are also true, so you get duplicated output. The easiest way to fix that is to check that the other condition is not true in the first two cases. I.e. make the first condition if((i % 3 == 0)&&(i % 5 != 0)) and the same for the second.
The other problem with your code is that you're printing the number when any of the cases is true, but you're supposed to print it when none of them are. You can fix that by making a fourth if-condition which checks that none of the conditions are true and if so, prints i.
Now if you did the above, you'll see that you ended up with some code duplication. If you think about it a bit, you'll see that you can easily fix that, by using if - else if - else if - else, which allows you to assume that the previous conditions were false when the current condition is checked.
Hm, I think I'll only hint:
Think of the correct order: What happens if a number is a multiple of 3, but also of (3 and 5)?
There is an else if statement.
Use else if so that the conditional don't overlap.