java函数(方法)练习:
A:求两个数据之和
B:判断两个数据是否相等
C:获取两个数中较大的值
D:打印m行n列的星形矩形
E:打印nn乘法表
A:程序如下:
package function;
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("数据1:");
int x=sc.nextInt();
System.out.println("数据2:");
int y=sc.nextInt();
//System.out.println(sum (x,y));
sum(x,y);
}
public static int sum(int a,int b) {
int sum=a+b;
System.out.println("两数之和:"+sum);
return sum;
}
}
B:程序如下:
package function;
import java.util.Scanner;
public class Boolean {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("数据1:");
int x=sc.nextInt();
System.out.println("数据2:");
int y=sc.nextInt();
boolean flag=xiangDeng(x,y);
System.out.println("输入两数据是否相等:"+flag);
}
public static boolean xiangDeng(int a,int b){
return (a==b)?true:false;
}
}
C:程序如下:
package function;
import java.util.Scanner;
public class Max {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入数据一:");
int x=sc.nextInt();
System.out.println("请输入数据一:");
int y=sc.nextInt();
System.out.println("最大值为:");
System.out.println(max(x,y));
}
public static int max(int a,int b){
int c=(a>b)?a:b;
return c;
}
}
D:程序如下:
package function;
import java.util.Scanner;
public class Xing {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入行数:");
int x=sc.nextInt();
System.out.println("请输入列数:");
int y=sc.nextInt();
System.out.println("输出结果:");
xing(x,y);
}
public static void xing(int a,int b){
for(int i=0;i<=a;i++) {
for(int j=0;j<=b;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
E:程序如下:
package function;
import java.util.Scanner;
public class Chenfa {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入行数:");
int x=sc.nextInt();
chenfa(x);
}
public static void chenfa(int a){
for(int i=1;i<=a;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j+""+i+"="+ij+'\t');
}
System.out.println();
}
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章