Exercise PrintNumberInWord (nested-if, switch-case): Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "nested-if" statement; (b) a "switch-case" statement.
Hints:
/*
* Trying nested-if and switch-case statements.
*/
public class PrintNumberInWord { // Save as "PrintNumberInWord.java"
public static void main(String[] args) {
int number = 5; // Set the value of "number" here!
// Using nested-if
if (number == 1) {
System.out.println( ...... );
} else if ( ...... ) {
......
} else if ( ...... ) {
......
......
} else {
......
}
// Using switch-case
switch(number) {
case 1: System.out.println( ...... ); break; // Don't forget "break"
case 2: System.out.println( ...... ); break;
......
......
default: System.out.println( ...... );
}
}
}
Exercise PrintDayInWord (nested-if, switch-case): Write a program called PrintDayInWord which prints “Sunday”, “Monday”, ... “Saturday” if the int variable "day" is 0, 1, ..., 6, respectively. Otherwise, it shall print “Not a valid day”. Use (a) a "nested-if" statement; (b) a "switch-case" statement.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。