I have this assignment that I've tried. But when I enter 1 it should not give any output as 1 is > 0 and 1 is not even but I still get output as:
Enter a +ve number
1
You entered 1
I'd asked for a +ve number :)
.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a +ve number");
int number = input.nextInt();
System.out.println("You entered "+number);
if(number > 0)
if(number %2 == 0)
System.out.println("Number"+number+" is even and +ve");
else
开发者_开发问答 System.out.println("I'd asked for a +ve number :)");
}
}
Your else actually belongs to the 2nd if
not the 1st if
as the indentation shows.
if(cond1)
if(cond2)
else // this else belongs to 2nd if not 1st if.
is same as:
if(cond1) {
if(cond2) {
} else {
}
}
This is because the Java grammar says that an else belongs to the closest unmatched if to which it can possibly belong.
If you want to match the else with first if you need to use parenthesis as:
if(cond1) {
if(cond2) {
}
} else {
}
Check that the code actually follows the logic it ought to - indentation won't help you with flow control, that's what curly brackets {}
are for.
That would probably be because your ifs
are interpreted like this:
if(number > 0)
if(number %2 == 0)
System.out.println("Number"+number+" is even and +ve");
else
System.out.println("I'd asked for a +ve number :)");
How's the computer supposed to know what you mean? You need to use brackets to make it unambiguous:
if(number > 0) {
if(number %2 == 0) {
System.out.println("Number"+number+" is even and +ve");
}
} else {
System.out.println("I'd asked for a +ve number :)");
}
I get the following output when I enter 1:
Enter a +ve number
1
You entered 1
I'd asked for a +ve number :)
Which is reasonable as you first check
if (number > 0)
which is true for number == 1
and then you check
if (number % 2 == 0)
which is false for number == 1
thus the else branch is taken:
System.out.println("I'd asked for a +ve number :)")
Your code should probably look something like:
Scanner input = new Scanner(System.in);
System.out.println("Enter a +ve number");
int number = input.nextInt();
System.out.println("You entered " + number);
if (number > 0) {
if (number % 2 == 0)
System.out.println("Number" + number + " is even and +ve");
} else {
System.out.println("I'd asked for a +ve number :)");
}
first you should try to debug (using eclipse maybe) add paranthesis after if(number > 0) statement to wrap inner if statement.
I threw your code into Netbeans and had it auto format it for readability. Is this your intended logic flow? Because this is how the compiler sees your if statements alignment/nesting.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a +ve number");
int number = input.nextInt();
System.out.println("You entered " + number);
if (number > 0)
{
if (number % 2 == 0)
{
System.out.println("Number" + number + " is even and +ve");
}
else
{
System.out.println("I'd asked for a +ve number :)");
}
}
}
}
1 %2 == 1 so it's clear that it enters on the else branch
its because your else statement is saying if its not even then say "i asked for a +ve number"
move it out of the if statement like this
if(number > 0)
{
if(number %2 == 0)
{
System.out.println("Number"+number+" is even and +ve");
}
}
else
{
System.out.println("I'd asked for a +ve number :)");
}
精彩评论