java编程辅导、辅导java CheckOddEven 程序、辅导java语言

- 首页 >> Java编程

Exercise CheckOddEven (if-else): Write a program called CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or “Even Number” otherwise. The program shall always print “BYE!” before exiting.


Hints: n is an even number if (n % 2) is 0; otherwise, it is an odd number.


/*

* Trying if-else statement and modulus (%) operator.

*/

public class CheckOddEven {   // Save as "CheckOddEven.java"

  public static void main(String[] args) {  // Program entry point

     int number = 49;       // Set the value of "number" here!

     System.out.println("The number is " + number);

     if ( ...... ) {

        System.out.println( ...... );

     } else {

        System.out.println( ...... );

     }

     System.out.println( ...... );

  }

}

Exercise CheckPassFail (if-else): Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise. The program shall always print “DONE” before exiting.


Hints:


/*

* Trying if-else statement.

*/

public class CheckPassFail {  // Save as "CheckPassFail.java"

  public static void main(String[] args) {  // Program entry point

     int mark = 49;  // Set the value of "mark" here!

     System.out.println("The mark is " + mark);

     if ( ...... ) {

        System.out.println( ...... );

     } else {

        System.out.println( ...... );

     }

     System.out.println( ...... );

  }

}

Take note of the source-code indentation!!! Whenever you open a block with '{', indent all the statements inside the block by 3 or 4 spaces. When the block ends, un-indent the closing '}' to align with the opening statement.


站长地图