4 Replies - 290 Views - Last Post: 18 February 2013 - 05:09 AM Rate Topic: -----

#1 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

[HELP]Only one result displaying

Posted 18 February 2013 - 03:57 AM

import java.util.Scanner;
 public class Divisible{
  public static void main(String[] args){
   
   //Create Scanner
   Scanner input=new Scanner(System.in);
   
   //Prompts user to enter an integer
   System.out.print("Enter an integer: ");
   int Integer=input.nextInt();
   
   //Display results
   if (Integer%5==0 && Integer%7==0)
   System.out.println("Is "+Integer+" divisible by 5 and 7? "+(Integer%5==0 && Integer%7==0));
   if (Integer%5==0 || Integer%7==0)
   System.out.println("Is "+Integer+" divisible by 5 or 7? "+(Integer%5==0 || Integer%7==0));
   
 }
}


This is my code. I want it to display for example I input 10, so the results will be:
Is it divisible by 5 and 7? false
Is it divisible by 5 or 7? true

But my program is only displaying 1 result. Can someone explain please.

I think i've figured it out, I removed the 2 if's statements and it worked:
import java.util.Scanner;
 public class Divisible{
  public static void main(String[] args){
   
   //Create Scanner
   Scanner input=new Scanner(System.in);
   
   //Prompts user to enter an integer
   System.out.print("Enter an integer: ");
   int Integer=input.nextInt();
   
   //Display results
   
   System.out.println("Is "+Integer+" divisible by 5 and 7? "+(Integer%5==0 && Integer%7==0));
   
   System.out.println("Is "+Integer+" divisible by 5 or 7? "+(Integer%5==0 || Integer%7==0));
   
 }
}
  


Is the code correct now or is there another way to make it work with the if's statements?

This post has been edited by magicm00n: 18 February 2013 - 03:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [HELP]Only one result displaying

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • Posts: 2,533
  • Joined: 29-July 11

Re: [HELP]Only one result displaying

Posted 18 February 2013 - 04:08 AM

If a number is divisible by 5 and 7, it is divisible by 5 or 7. ;)
Was This Post Helpful? 1
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: [HELP]Only one result displaying

Posted 18 February 2013 - 04:08 AM

Well, what is happening in your if condition is that it will only be true if both conditions are true

if(TRUE && FALSE)
{
  //this block will be skipped.
}


That is because the && operator only returns true if both the operands are true.

regards,
Raghav

This post has been edited by raghav.naganathan: 18 February 2013 - 04:09 AM

Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: [HELP]Only one result displaying

Posted 18 February 2013 - 04:12 AM

Please don't name your integer Integer :surrender:
Was This Post Helpful? 1
  • +
  • -

#5 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

Re: [HELP]Only one result displaying

Posted 18 February 2013 - 05:09 AM

Okay noted. Thanks guys!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1