作业
一、写Test.java,要求如下:
- 传入一个int数,要求清除该数的低8位,高24位不变,将结果以十进制形式输出
- 传入一个int数,要求将该数的低16位置1,高16位不变,将结果以十进制形式输出
- 实现两个int类型变量值的交换,要求不使用中间变量
public class Test {
public static void main(String[] args) {
int a = 3;
int b = 4;
new Test().b1(a);
new Test().b2(a);
new Test().exchange(a,b);
}
public static void b1(int a) {
/*
传入一个int数,要求清除该数的低8位,高24位不变,将结果以十进制形式输出
*/
System.out.println(a & 0xffffff00);
}
public static void b2(int a) {
/*
传入一个int数,要求将该数的低16位置1,高16位不变,将结果以十进制形式输出
*/
System.out.println(a | 0xffff);
}
/*
实现两个int类型变量值的交换,要求不使用中间变量
*/
public static void exchange(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
System.out.println("a="+a+",b="+b);
}
}
运行截图:
二、以二进制形式输出一个十进制数
public class C {
public static void main(String[] args) {
int a = 98;
String s ="";
for(int i = a;i != 0; i/=2 ) {
s = i%2 + s;
}
System.out.println(s);
}
}
运行截图:
三、写LoopTest.java,分别使用while/do/for循环实现1x2x…x10。
public class LoopTest{
public static void main(String[] args) {
int a = 1;
new LoopTest().While(a);
new LoopTest().doWhile(a);
new LoopTest().forTest();
}
public static void While(int a) {
int s = 1;
while(a <= 10) {
s = s*a;
a++;
}
System.out.println("1.1x2x...x10 = "+s);
}
public static void doWhile(int a) {
int s = 1;
do {
s = s*a;
a++;
}while(a <= 10);
System.out.println("2.1x2x...x10 = "+s);
}
public static void forTest() {
int s = 1;
for(int i = 1;i < 11; i++) {
s *= i;
}
System.out.println("3.1x2x...x10 = "+s);
}
}
运行截图:
四、创建一个阶乘应用程序Factor.java
功能:一个数X 的阶乘(通常记作X!)等于X*(X-1)(X-2)…*1。例如4!等于4×3×2×1=24。
import java.util.Scanner;
public class Factor{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入一个X值:");
int n = scanner.nextInt();
int a = 1;
for(int i = n;i > 0; i--) {
a *= i;
}
System.out.println(n+"! = "+a);
}
}
运行截图:
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
可以自己从屏幕输入数据,不用写死
五、打印出四种形式的九九乘法表
提示:
1、System.out.println()的功能为输出+换行
System.out.print()的功能为输出
2、在适当的位置可以使用’\t’进行对齐操作
public class Nine {
public static void main(String[] args) {
new Nine().jiuJiu();
System.out.println();
new Nine().daoJiu();
new Nine().zhongJiu();
System.out.println();
new Nine().daoZhongJiu();
}
public static void jiuJiu() {
for(int i = 1; i < 10;i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"= "+(i*j)+" ");
}
System.out.println();
}
}
public static void daoJiu() {
for(int i = 9; i > 0;i--) {
for(int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"= "+(i*j)+" ");
}
System.out.println();
}
}
/*
* 8 行:1 列:1
* * 7 行:2 列:2
* * * 6 行:3 列:3
* * * * 5
* * * * * 4
* * * * * * 3
* * * * * * * 2
* * * * * * * * 1个
* * * * * * * * * 0个空格
*/
public static void zhongJiu() {
for(int i = 1; i < 10;i++) {
for(int k = 1;k<(10-i);k++) {
// 五个空格
System.out.print(" ");
}
for(int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"= "+(i*j)+" ");
}
System.out.println();
}
}
public static void daoZhongJiu() {
for(int i = 9; i > 0;i--) {
for(int k = 1;k<(10-i);k++) {
// 五个空格
System.out.print(" ");
}
for(int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"= "+(i*j)+" ");
}
System.out.println();
}
}
}
运行截图: