Hello i just started a coding exercise on hackerrank and i am having a little challenge using scanner class with the skip function. here is what i have tried.
Objective
In this challenge, we're getting started with conditional statements. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given an integer, 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
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.
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();
if (N % 2 == 0 && N >= 2 && N <= 5 && N > 20) {
System.out.println("not weird");
} else if (N % 2 == 0 && N >= 6 && N <= 20) {
System.out.println("weird");
} else {
System.out.println("weird");
}
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
}
}
Please what am i doing wrong.
You're mixing up && and ||, so your first if will never run as mentioned in the comments.
So it looks like the only "Not Weird" print out is 2, 4 and even numbers > 20.
So use this to your advantage to just check for the "Weird" outputs, otherwise print "Not Weird".
if (n % 2 == 0) {
if ((n >= 2 && n <= 5) || (n > 20)) {
return "Not Weird";
}
}
return "Weird";
Online Demo
Having said this, I'm not sure what you want with Scanner::skip
if (N % 2 == 0 && N >= 2 && N <= 5) {
System.out.println("Not Weird");
} else if (N % 2 == 0 && N > 20) {
System.out.println("Not Weird");
} else {
System.out.println("Weird");
}
This is how I simplified it.
if(N % 2 == 0 )
{
if((N >= 2 && N <= 5) || N > 20)
{
System.out.println("Not Weird");
}
else
{
System.out.println("Weird");
}
}
else
{
System.out.println("Weird");
}
See code below
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if ((N % 2 == 0 && N >= 2 && N <= 5) || N > 20) {
System.out.println("Not Weird");
} else if (N % 2 == 0 && N >= 6 && N <= 20) {
System.out.println("Weird");
}
else
{
System.out.println("Weird");
}
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 && n>2 && n<5 || n>20) {
System.out.println("Not Weird");
} else if( n>6 || n<20) {
System.out.println("Weird");
} else {
System.out.println("Weird");
}
scanner.close();
}
}
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 && 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");
}else {
System.out.println("Weird");
}
scanner.close();
}
}
package hudai;
import java.util.Scanner;
public class Hudai {
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();
if(N%2 == 1){
System.out.println("Weird");
}
if(N%2==0 && 2<N && N<5){
System.out.println("Not Weird");
}
if(N%2==0 && 6<N && N<=20){
System.out.println("Weird");
}
if(N%2==0 && 20<N && N<=100){
System.out.println("Not Weird");
}
}
}
if(N % 2 == 0){
if((N >=2 && N <= 5) || (N > 20)){
System.out.println("Not Weird");
}
else if(N >= 6 && N <= 20){
System.out.println("Weird");
}
}
else{
System.out.println("Weird");
}
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
if (n % 2 == 1) {
System.out.println("Weird");
}
if (n % 2 == 0 && 2 < n && n < 5) {
System.out.println("Not Weird");
}
if (n % 2 == 0 && 6 < n && n <= 20) {
System.out.println("Weird");
}
if (n % 2 == 0 && 20 < n && n <= 100) {
System.out.println("Not Weird");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
}
}
}
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();
if (N % 2 == 0 && (N >= 2 && N <= 5) )
{
System.out.println("Not Weird");
}
else if(N % 2==0 && N > 20)
{
System.out.println("Not Weird");
}
else if ((N % 2 == 0) && (N >= 6 && N <= 20)) {
System.out.println("Weird");
}
else
{
System.out.println("Weird");
}
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
}
}
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();
if (N % 2 == 0 && N >= 2 && N <= 5 ) {
System.out.println("Not Weird");
}
else if(N % 2 == 0 && N>20){
System.out.println("Not Weird");
}
else if (N % 2 == 0 && N >= 6 && N <= 20) {
System.out.println("Weird");
}
else {
System.out.println("Weird");
}
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
}
}
Using ternary operation,
String w="Weird";
String nw= "Not Weird";
String s= (N%2!=0)?w:(N%2==0 && (N>=2 && N<=5))?nw:(N%2==0 && (N>=6 && N<=20))?w:nw;
if(n % 2 == 0){
if (n >= 2 && n <= 5 || n > 20) {
System.out.println("Not Weird");
}
if (n >= 6 && n <= 20) {
System.out.println("Weird");
}
} else if(n % 2 != 0){
System.out.println("Weird");
}
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>=2 && N<=5) || (N>20)){
System.out.println("Not Weird");
}
else if (N>=6 && N<=20){
System.out.println("Weird");
}
}
else {
System.out.println("Weird");
}
scanner.close();
}
}
Java 8 functional interface applied on Integer.
int n = 24;
Function<Integer, String> f = x -> x % 2 == 1 || (x >= 6 && x <= 20) ? "Weird" : "Not Weird";
System.out.println(f.apply(n));
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();
}
Related
My do-while loop is supposed to print out numbers from 1 - 106 (including 1 and 106)
whats supposed to happen:
multiples of 3 are supposed to print out Big,
multiples of 5 are supposed to print out Mean,
multiples of 7 are supposed to print out Bugs,
multiples of 3 and 5 are supposed to print out BigMean,
multiples of 3 and 7 are supposed to print out BigBugs,
multiples of 5 and 7 are supposed to print out MeanBugs,
multiples of 3, 5 and 7 are supposed to print out BigMeanBugs.
What actually happens:
my do-while loop only runs the first "if" statement. Any help?
My code:
public static void main(String [] args){
int i = 0;
do{
++i;
if(i %3 == 0){
System.out.print("Big, ");
if(i %5 ==0){
System.out.print("Mean, ");
}
if(i %7 ==0){
System.out.print("Bugs, ");
}
if((i %3 == 0) && (i %5 == 0)){
System.out.print("BigMean, ");
}
if((i %3 == 0) && (i %7 == 0)){
System.out.print("BigBugs, ");
}
}else if((i %5 == 0) && (i %7 == 0)){
System.out.print("MeanBugs, ");
}else if((i %3 == 0) && (i %5 == 0) && (i %7 ==0)){
System.out.print("BigMeanBugs, ");
}else{
System.out.print(i + " ");
}
}while(i<=106);
}//closing main
As I could see your problem is probably that your if-statements are inside of first if-statement, so maybe you want this:
public class BigMeanBugsDoWhileLoop
{
public static void main(String [] args)
{
int i = 0;
do
{
++i;
boolean bug = false;
String result = "";
if(i %3 == 0)
{
result += "Big";
bug = true;
}
if(i %5 ==0)
{
result += "Mean";
bug = true;
}
if(i %7 ==0)
{
result += "Bugs";
bug = true;
}
if(!bug)
{
System.out.println(i + "");
}
else
{
System.out.println(result);
}
}while(i<106);
}//closing main
} // closing class
You don't need other if-statements because you don't use break so other expressions will be evaluated before next iteration.
First, if something is divisible by 3 and 7 it is divisible by 21.
Second, if you reverse the order, you will catch divisible by 3 and 5 before divisible by 3 or 5 separately (assuming that is what you want). If you do it the other way you may print the values prematurely or miss them altogether before checking the other possible factors. This behavior is also dependent on if vs if/else if statements.
I have limited the printout to 25 to avoid a long list. Modify as you see fit.
public class BigMeanBugsDoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
String bugType = "";
if (i % 105 == 0) {
bugType = "BigMeanBugs"; // 3 5 7
} else if (i % 35 == 0) {
bugType = "MeanBugs"; // 5 7
} else if (i % 21 == 0) {
bugType = "BigBugs"; // 3 7
} else if (i % 15 == 0) {
bugType = "BigMean"; // 3 5
} else if (i % 7 == 0) {
bugType = "Bugs"; // 7
} else if (i % 5 == 0) {
bugType = "Mean"; // 5
} else if (i % 3 == 0) {
bugType = "Big"; // 3
}
System.out.println(bugType.isEmpty() ? i : bugType);
i++;
} while (i <= 25);
}// closing main
} // closing class
Prints this.
1
2
Big
4
Mean
Big
Bugs
8
Big
Mean
11
Big
13
Bugs
BigMean
16
17
Big
19
Mean
BigBugs
22
23
Big
Mean
You have to increment the value of i in the end of loop
public static void main(String [] args){
int i = 1;
do{
if(i %3 == 0){
System.out.print("Big, ");
if(i %5 ==0){
System.out.print("Mean, ");
}
if(i %7 ==0){
System.out.print("Bugs, ");
}
if((i %3 == 0) && (i %5 == 0)){
System.out.print("BigMean, ");
}
if((i %3 == 0) && (i %7 == 0)){
System.out.print("BigBugs, ");
}
}else if((i %5 == 0) && (i %7 == 0)){
System.out.print("MeanBugs, ");
}else if((i %3 == 0) && (i %5 == 0) && (i %7 ==0)){
System.out.print("BigMeanBugs, ");
}else{
System.out.print(i + " ");
}
i++;
}while(i<=106);
}//closing main
} // closing class
It shows compilation error:
Compile Message
Solution.java:19: error: unexpected type
if((N%2=0) && (N>=2 && N<=5))
^
required: variable
found: value
1 error
public class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
if (N % 2 != 0) {
System.out.println("Weird");
}
if ((N % 2 = 0) && (N >= 2 && N <= 5)) {
System.out.println("Not Weird");
}
}
}
N % 2 = 0 is an incorrect assignment because N % 2 isn't a variable. Even if it were a correct expression, it wouldn't return a boolean, so the line would never compile.
You need N % 2 == 0.
My problem is with output of my code. When I enter 20, the output must be weird, but I am getting not weird. Same with the value 18.
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 2 && n >= 5){
ans="Not weird";
} else if(n <= 6 && n >= 20){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}
the output must be weird,but i am getting not weird
Because, if(n%2 == 1) return false and fall to else block where
if(n <= 2 && n >= 5) is `false`
and
else if(n <= 6 && n >= 20) is also `false`
So, again falls to else block. You probably change
if(n <= 2 && n >= 5)
to
if(n >= 2 && n <= 5)
and
else if(n <= 6 && n >= 20)
to
else if(n >= 6 && n <= 20)
Otherwise, they will never be true and always falls to else.
In your program last else is being executed. Change && (logical AND) to || (logical OR) which will check if number is less than something OR higher than something, instead of checking if something is less or equal 5 AND higher or equal to 20 in the same time as it doesn't have a possibility to evaluate in any case.
I have come up with two solutions and also i see a flaw:
1. if(n%2 == 1) this code can be altered to if(n%2 == 0)
2. The flaw is **(n <= 2 && n >= 5)** . No number can be <2 and >5 at the same time. Try changing that to (n <= 2 || n >= 5) and same goes for (n <= 6 && n >= 20)
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 6 || n >= 20){
ans="Not weird";
} else if(n <= 2 || n >= 5){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}
I cant get "FizzBuzz". No matter what the input, the "FizzBuzz" code isn't running. What did I do wrong?
public String[] fizzBuzz(int start, int end) {
int diff = end-start;
String[] array = new String[diff];
for (int i = 0; i < diff; i++) {
if (start%3 == 0 && start%5 == 0) array[i] = "FizzBuzz";
if (start%3 == 0 || start%5 == 0) {
if (start%3 == 0) array[i] = "Fizz";
if (start%5 == 0) array[i] = "Buzz";
}
else {
array[i] = String.valueOf(start);
}
start++;
}
return array;
}
Logic in your if statements is a bit busted, using your code as the starting point, you'd have to do something like this.
if (start%3 == 0 && start%5 == 0) {
array[i] = "FizzBuzz";
}
else if (start%3 == 0 || start%5 == 0) {
if (start%3 == 0) array[i] = "Fizz";
if (start%5 == 0) array[i] = "Buzz";
}
else {
array[i] = String.valueOf(start);
}
String s = "" + i;
if ((i % 3) == 0) {
s += " Fizz";
}
if ((i % 5) == 0) {
s+= " Buzz";
}
System.out.println(s);
This code snippet placed in a loop will print Fizz, Buzz and Fizz Buzz on i divisible by 3, 5 and 15 respectively.
you should try this.
class FizzBuzz{
public static void main(String args[]){
int n = 100;
for(int i=0;i<=n;i++){
if((i % 3) == 0 && (i % 5) != 0){
System.out.println("Fizz");
}
else if((i % 5) == 0 && (i % 3) != 0){
System.out.println("Buzz");
}else if((i % 3) == 0 && (i % 5) == 0){
System.out.println("FizzBuzz");
}else{
System.out.println(""+i);
}
}
}
}
I cannot figure out how to make it so it only calls check4 once... this was for a homework assignment last semester and I got 5 points off for calling it multiple times but I would not like to know how to do it (the professor never said how).
I tried moving the check4 to after the if block but it really needs to go in between the last else if and the else which is not possible. The ONLY way the number should print is if all of the steps do not print out a word instead.
public class CheeseCakeFactory_ajh187 {
public static void main(String[] args) {
int counter = 0;
int printNumber = 0; //number that will get changed to one of the terms
while (counter != 15 & printNumber < 210) { //as long as the counter is not 15 and print number is lesss than 210 it will keep looping.
printNumber++;
if (printNumber % 3 ==0 && printNumber % 5 == 0 && printNumber % 7 == 0) {
System.out.print("cheesecakefactory");
}
else if (printNumber % 3 == 0 && printNumber % 5 == 0){
System.out.print("cheesecake");
check4(printNumber);
}
else if (printNumber % 3 == 0 && printNumber % 7 == 0){
System.out.print("cheesefactory");
check4(printNumber);
}
else if (printNumber % 5 == 0 && printNumber % 7 == 0){
System.out.print("factorycake");
check4(printNumber);
}
else if (printNumber % 3 == 0){
System.out.print("cheese");
check4(printNumber);
}
else if (printNumber % 5 ==0){
System.out.print("cake");
check4(printNumber);
}
else if (printNumber % 7 ==0){
System.out.print("factory");
check4(printNumber);
}
else { //if the number is not divisible by any of the other numbers we still have to check for the 4
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
else {
System.out.print(printNumber); //if its not divisible by 4, we just print the number
}
}
System.out.print(" ");
counter++;
if (counter == 15) { //once the counter is 15 we need to put the new items on a new line
System.out.print("\n");
counter = 0; //resets the counter so that we can accomplish this every 15 passes.
}
}
}
public static void check4(int printNumber) {
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
}
}
First, I would update check4 to return a boolean (which you can save). Something like,
public static boolean check4(int printNumber) {
return String.valueOf(printNumber).contains("4");
}
Then you can also save your tests into boolean variables. Something like
boolean mod3 = printNumber % 3 == 0;
boolean mod5 = printNumber % 5 == 0;
boolean mod7 = printNumber % 7 == 0;
if (mod3 || mod5 || mod7) {
if (mod3 && mod5 && mod7) {
System.out.print("cheesecakefactory");
} else {
boolean isCheck4 = check4(printNumber); // <-- call it once
if (mod3 && mod5) {
System.out.print("cheesecake");
} else if (mod3 && mod7) {
System.out.print("cheesefactory");
} else if (mod5 && mod7) {
System.out.print("factorycake");
} else if (mod3) {
System.out.print("cheese");
} else if (mod5) {
System.out.print("cake");
} else if (mod7) {
System.out.print("factory");
} else {
if (!isCheck4) { // <-- it doesn't have a 4, print it.
System.out.print(printNumber);
}
}
if (isCheck4) {
System.out.print("hoho"); // <-- it does have a 4.
}
}
}
I hope I got your problem right so:
you create a global boolean variable named say isMethodCalled witch is false, then in the check4 method you make it true, and simple check if the isMethodCalled is false before calling the method.
boolean isMethodCalled = false;
if(!isMethodCalled) {
check4() // do wathever you need to do to call check4()
}
public static void check4(int printNumber) {
if (Integer.toString(printNumber).contains("4")){
System.out.print("hoho");
}
isMethodCalled = true;
}
Simply use a flag, set it to true initially.
Then, wherever you dont want the check4 to run, set it to false.
after the if-else, check if the flag istrue. if it is, execute 'check4(printNumber)'
public class CheeseCakeFactory_ajh187 {
public static void main(String[] args) {
int counter = 0;
int printNumber = 0; //number that will get changed to one of the terms
int flag=true;
while (counter != 15 & printNumber < 210) { //as long as the counter is not 15 and print number is lesss than 210 it will keep looping.
printNumber++;
if (printNumber % 3 ==0 && printNumber % 5 == 0 && printNumber % 7 == 0) {
System.out.print("cheesecakefactory");
flag=false;
}
else if (printNumber % 3 == 0 && printNumber % 5 == 0){
System.out.print("cheesecake");
}
else if (printNumber % 3 == 0 && printNumber % 7 == 0){
System.out.print("cheesefactory");
}
else if (printNumber % 5 == 0 && printNumber % 7 == 0){
System.out.print("factorycake");
}
else if (printNumber % 3 == 0){
System.out.print("cheese");
}
else if (printNumber % 5 ==0){
System.out.print("cake");
}
else if (printNumber % 7 ==0){
System.out.print("factory");
}
else { //if the number is not divisible by any of the other numbers we still have to check for the 4
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
else {
System.out.print(printNumber); //if its not divisible by 4, we just print the number
}
flag=false;
}
if(flag)
check4(printNumber);
System.out.print(" ");
counter++;
if (counter == 15) { //once the counter is 15 we need to put the new items on a new line
System.out.print("\n");
counter = 0; //resets the counter so that we can accomplish this every 15 passes.
}
}
}
public static void check4(int printNumber) {
if (Integer.toString(printNumber).contains("4")) {
System.out.print("hoho");
}
}
}