作业
package javaseday02;
import java.util.Scanner;
import org.junit.Test;
public class HomeWork {
输入一个数字,判断是一个奇数还是偶数。
@Test
public void myTest01() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("输入数字:" + n);
if (n % 2 == 1) {
System.out.println("数字为奇数");
} else {
System.out.println("数字为偶数");
}
}
判断一个数字是否能被5和6同时整除(打印能被5和6整除),
或只能被5整除(打印能被5整除),或只能被6整除,
(打印能被6整除),不能被5或6整除,(打印不能被5或6整除
@Test
public void myTest02() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("输入数字:" + n);
if (n % 5 == 0) {
if (n % 6 == 0) {
System.out.println("数字能被5和6同时整除");
} else {
System.out.println("数字只能被5整除");
}
} else {
if (n % 6 == 0) {
System.out.println("数字只能被6整除");
} else {
System.out.println("数字不能被5或6整除");
}
}
}
输入三个整数x,y,z,请把这三个数由小到大输出.
@Test
public void myTest03() {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
System.out.println("输入数字 x:" + x);
System.out.println("输入数字 y:" + y);
System.out.println("输入数字 z:" + z);
int s;
if (x > y) {
s = x;
x = y;
y = s;
}
if (x > z) {
s = x;
x = z;
z = s;
}
if (y > z) {
s = y;
y = z;
z = s;
}
System.out.println(x + " " + y + " " + z);
}
输出0-9之间的数,但是不包括5。
@Test
public void myTest04() {
for (int i = 0; i < 10; i++) {
if (i != 5)
System.out.println(i);
}
}
编写一个程序,求整数n的阶乘,例如5的阶乘是12345。
@Test
public void myTest05() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 1;
for (int i = 1; i <= n; i++) {
sum = sum * i;
System.out.print(i);
if (i != n)
System.out.print("*");
}
System.out.println("=" + sum);
}
由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321
@Test
public void myTest06() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("输入4位整数:"+n);
int a1,a2,a3,a4;
a1=n/1000%10;
a2=n/100%10;
a3=n/10%10;
a4=n/1%10;
n=a4*1000+a3*100+a2*10+a1;
System.out.println("反转后的数为:"+n);
}
}