Quantcast
Channel: Java not reading "&&" statements properly? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Java not reading "&&" statements properly?

$
0
0

I'm having a problem with my Java code. Specifically, one of my if statements containing an && is not returning True for certain inputs like I expect it to.

The snippet in question:

if (num%2==1 && num < 0) {       //why is not reading this statement?    negodd ++;}

Sample inputs and outputs vs expected outputs:

 Enter any number to continue. Enter 0 to stop : 1 2-2-1 0   // not counted as a number since it is a stop function.Output of my code.                     What it should be.You Entered 4 numbers :               You Entered 4 numbers :1 negative even                         1 negative even1 positive even                         1 positive even                         0 negative odd                          1 negative odd  <--should read the 11 positive odd                          1 positive odd

The full code in case that helps:

 import java.util.Scanner; public class stupid {     public static void main(String[] args) {         Scanner x = new Scanner(System.in);         int num = 0;         int negodd = 0, count = 0, posseven = 0;         int possodd = 0; int negeven=0;         System.out.println("Enter any number to continue. Enter 0 to stop : ");         num = x.nextInt();         if(num==0){             System.out.print("You immediately stop");             System.exit(0);         }         while (num != 0) {             count ++;             if (num%2==1 && num > 0) {                 possodd ++;             }              if (num%2==1 && num < 0) {       //why is not reading this statement?                 negodd ++;             }             if (num%2==0 && num > 0) {                 posseven ++;             }             if (num%2==0 && num < 0) {                 negeven++;             }             num = x.nextInt();         }         System.out.printf("You Entered %d numbers\n",count);         System.out.printf("%d negative even \n",negeven);         System.out.printf("%d positive even\n",posseven);         System.out.printf("%d negative odd\n",negodd);         System.out.printf("%d positive odd\n",possodd);    }}

Thanks in advance!


Viewing all articles
Browse latest Browse all 4

Trending Articles