:函数的练习:
A:求两个数据之和
B:判断两个数据是否相等
C:获取两个数中较大的值
D:打印m行n列的星形矩形
E:打印nn乘法表
import java.util.Scanner; //导入 Scanner 类
class Demo_HomeWork07{
public static void main(String[] args) {
//声明 Scanner 对象,作用是可以在控制台输入
Scanner sc = new Scanner(System.in);
//循环
while (true) {
//提示用户输入数据
System.out.println("请输入\n1:求两个数据之和"+
"\n2:判断两个数据是否相等"+
"\n3:获取两个数中较大的值"+
"\n4:打印m行n列的星形矩形"+
"\n5:打印nn乘法表"+
"\n6:退出循环"+ "\n~~~~~~~~~~~~~~~~~~~~~~~");
//获得用户输入的数据
int x = sc.nextInt();
//判断用户输入的数据
if (x >= 1 && x < 7) {
if (x == 5) {
//提示用户输入数据
System.out.println("请输入第一个数据...");
int a = sc.nextInt();
chengFaBiao(a);
}else if (x == 6) {
return;
}else {
//提示用户输入数据
System.out.println("请输入第一个数据...");
int a = sc.nextInt();
System.out.println("请输入第二个数据...");
int b = sc.nextInt();
//switch选择功能
switch (x) {
case 1:
qiuHe(a,b);
break;
case 2:
xiangDeng(a,b);
break;
case 3:
biJiao(a,b);
break;
case 4:
juXing(a,b);
break;
}
}
continue;
}else {
System.out.println("输入有误,请重新输入...");
System.out.println("========================");
continue;
}
}
}
//求和
public static void qiuHe(int a,int b) {
System.out.println("两个数据的和是:" + (a + b) + "\n============================");
}
//是否相等
public static void xiangDeng(int a,int b) {
if (a == b) {
System.out.println("a = " + a + " ,b = " + b + "\n两个数相等"+ "\n============================");
}else {
System.out.println("a = " + a + " ,b = " + b + "\n两个数不等"+ "\n============================");
}
}
//获取最大值
public static void biJiao(int a,int b) {
int max = a > b ? a : b;
System.out.println("a = " + a + " ,b = " + b + "\n两个数最大的数是:" + max+ "\n============================");
}
//矩形
public static void juXing(int a,int b) {
for (int i =1;i <=a ;i++ ) {
for (int j =1;j <= b ;j++ ) {
System.out.print(" * ");
}
System.out.println();
}
System.out.println("==========================");
}
//乘法表
public static void chengFaBiao(int a) {
for (int i = 1; i <= a ;i++ ) {
for (int j = 1;j <= i ;j++ ) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
System.out.println("========================");
}
}
|
|