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

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)

Related

How to remove the amounts of ifs from the application? [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 last month.
Improve this question
The code I say has to complete these assignments:
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
Complete the stub code provided in your editor to print whether or not N is weird.
My code looked like 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])?");
scanner.close();
int Numberparorimpa = N % 2;
if(N < 2 || Numberparorimpa ==1 || N <=20 && N >=6 ){
System.out.println("Weird");
}else{
if(N >=2 && Numberparorimpa == 0){
System.out.println("Not Weird");
}else{
if(Numberparorimpa == 0 && N >=6 || N<=20){
System.out.println("Weird");
}else{
if(Numberparorimpa== 0 && N> 20){
System.out.println("Not Weird");
}else{
return;
}
}
}
}
}
}
How can I reduce the IFs of this code?
I think an optimize version could be this :
if (N % 2 == 1 || (N >= 6 && N <= 20)) {
System.out.println("Weird");
}
else {
System.out.println("Not Weird");
}
If N is odd or N in range of 6 to 20 it's weird.
Else N is either even or not in the range so it's not weird.
You can reduce the complexity and improve the readability of your code by extracting your logic to method/class etc. Also, nested conditions are hard to read, you should avoid it.
Example:
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
String print = isGivenNumberWeird(n) ? "Weird" : "Not Weird"
System.out.println(print);
}
private static boolean isGivenNumberWeird(int n) {
boolean isOdd = n % 2 == 1;
if (isOdd) {
return true;
}
if (n >= 2 && n <=5) {
return false;
}
if (n >= 6 && n <=20) {
return true;
}
if (n > 20) {
return false;
}
}

Java If-Else in HackerRank, Is my code good or can I get better solution for the problem [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 9 months ago.
Improve this question
Problem Statement
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
Complete the stub code provided in your editor to print whether or not n is weird.
Input Format
A single line containing a positive integer,n.
Constraints
1<=n<=100
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.
Sample Input 0
3
Sample Output 0
Weird
Sample Input 1
24
Sample Output 1
Not Weird
Explanation
Sample Case 0: n=3
n is odd and odd numbers are weird, so we print Weird.
Sample Case 1: n=24
n>20 and n is even, so it isn't weird. Thus, we print Not Weird.
My solution to the problem.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
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==0){
if(N>1){
if(N<6){
System.out.println("Not Weird");
}
else if(N<21){
System.out.println("Weird");
}
else{
System.out.println("Not Weird");
}
}
}
else{
System.out.println("Weird");
}
scanner.close();
}
}
Can I get suggestions regarding the solution. Thank You.
The conditions should be merged: the result is Weird for any number in the range 6 <= n <= 20 OR n is odd, otherwise it is Not Weird:
System.out.println((6 <= n && n <= 20 || n % 2 != 0) ? "Weird" : "Not Weird");
// or
System.out.println((5 < n && n < 21 || n % 2 != 0) ? "Weird" : "Not Weird");
if (~N % 2 == 0 || N <= 20 && N >= 6) {
System.out.println("Weird");
} else if (N % 2 == 0 && N > 20 || (N >= 2 && 5 <= N)) {
System.out.println("not Weird");
}

while (1) loop with continue and break statements [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 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

InputMismatchException when reading float via Scanner.nextFloat() [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
When I use the program with an integer value it works, but when I input a float (such as 47.3) it doesn't. Can you please help me in defining the problem?
the code:
package ex1;
import java.util.Scanner;
public class test {
static Scanner input = new Scanner(System.in);
public static void main (String[] args) {
float n;
System.out.println("enter a number:");
n = input.nextFloat();
if (n > 0 && n % 2 == 0) {
System.out.println("the number is positive and even");
}
else if (n > 0 && n % 2 != 0) {
System.out.println("the number is positive and odd");
}
else if (n < 0 && n % 2 == 0) {
System.out.println("the number is negative and even");
}
else if (n < 0 && n % 2 != 0) {
System.out.println("the number is negative and odd");
}
else if (n == 0) {
System.out.println("the number is zero");
}
}
}
the problem:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextFloat(Scanner.java:2496)
at ex1.test.main(test.java:14)
thanks
A comma is considered as a decimal separator in Java. So to use a Float input you should replace the '.' by ','

HackerRank challenge, what am I doing wrong? [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 6 years ago.
Improve this question
I am trying to do this challenge https://www.hackerrank.com/challenges/java-if-else
I tried doing this:
public class Main {
public static void main(String[] args) {
int x;
x = 34;
if ((x % 2) != 0) {
System.out.println("Weird");
} else if (((x % 2 == 0) & ((x >= 2) & (5 >= x)))) {
System.out.println("Not Weird");
} else if (((x % 2 == 0) & ((x >= 6) & (20 >= x)))) {
System.out.println("Weird");
} else if ((x % 2 == 0) & (x > 20)) {
System.out.println("Not Weird");
}
}
}
I ran this in Intellij and it works fine, but here, I only get three test cases right. What am I doing wrong? I was overwhelmed by the scanner stuff, as I have not even covered that stuff yet in my own reading.
HackerRank challenge, what am I doing wrong?
You are not reading the number from Standard Input, so the tests are all checking the output for the number 34.
Replace
int x;
x = 34;
With this (which is what the test started with)
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
Do that, your tests pass fine.
Alternate solution
boolean even = x % 2 == 0;
boolean weird = !even || (even && (6 <= x && x <= 20));
System.out.println(weird ? "Weird" : "Not Weird");
You should be using && as a conditional operator. Also you don't need to have the if statement check if it is even, as the first if statement provided in their code already checks if it's odd. If it isn't (only other option is even) it goes to the else statement. Elegant solution listed below.
The scanner simply is just what is passed in, don't worry about it, just use the value n, they will pass the value into the code.
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String ans="";
if(n%2==1){ // only if it's odd
ans = "Weird";
}
// only enters else if the value is even
else{
if (n >= 6 && n <= 20) { // between 6 and 20
ans = "Weird";
} else { // only other option is greater than 20
// or below 6 which includes 2 through 5
ans = "Not Weird";
}
}
System.out.println(ans);
}
EDIT: Saw that his ranges are in a non-traditional order, so it is fine. Not sure what was wrong prior.
The above answer is better^^
I cannot comment (rep too low), but you don't need to check if it is even each time after the first check. You can assume that if it is not odd, then it is even.
An easier/simpler way of writing your code is:
String answer = "";
if ((x % 2) != 0) {//if odd
answer = "Weird";
}
else { //if even
if ((x > 1) && (x < 6)) {
answer = "Not Weird";
}
else if ((x > 5) && (x < 21)) {
answer = "Weird";
}
else if (x > 20) {
answer = "Not Weird";
}
System.out.println(answer);
By not using "equal to" operators and changing your order in the "less than" side you could solve your problem.
Your code is OK, has passed HackerRank tests, inside their else:
if ((n % 2 == 0) & ((n >= 2) & (5 >= n)))
System.out.println("Not Weird");
if ((n % 2 == 0) & ((n >= 6) & (20 >= n)))
System.out.println("Weird");
if ((n % 2 == 0) & (n > 20))
System.out.println("Not Weird");
Just changed to "else if" to "if".

Categories