在这里插入代码片
publicstaticvoidmain(String[] args){// TODO Auto-generated method stub//编写加减乘除8个方法,都需要至少两个参数,4个方法将值返回,4个方法直接输出值。\add1(12,13);
System.out.println(add2(12,13));sub1(10,5);
System.out.println(sub2(10,5));mul1(10,1);
System.out.println(mul2(10,1));div1(10,1);
System.out.println(div2(10,1));}publicstaticvoidadd1(int a,int b ){
System.out.println(a+b);}publicstaticint add2 (int a,int b ){return a+b;}publicstaticvoidsub1(int a,int b ){
System.out.println(a-b);}publicstaticintsub2(int a,int b ){return a-b;}publicstaticvoidmul1(int a,int b ){
System.out.println(a*b);}publicstaticintmul2(int a,int b ){return a*b;}publicstaticvoiddiv1(int a,int b ){
System.out.println(a/b);}publicstaticintdiv2(int a,int b ){return a/b;}
在这里插入代码片
publicstaticvoidmain(String[] args){//3.写一个方法,接收一个数字,输出与数字对应的奥利给。注意进行数字合法判断。output();}publicstaticvoidoutput(){
Scanner inputScanner =newScanner(System.in);
System.out.println("输入一个数字");int num = inputScanner.nextInt();if(num >0){for(int i =1; i <=num; i++){
System.out.println("奥里给");}}else{
System.out.println("输入数值不合法,请重新输入");main(null);}}
在这里插入代码片
publicstaticvoidmain(String[] args){name();}publicstaticvoidname(){//定义一个方法,要求传入String一个数组,//存放内容为自己想吃的美食,//在方法内部遍历输出,main方法中调用。
String[] likefood ={"鲍鱼","龙虾","大闸蟹","法国鹅肝","82年拉菲"};for(int i =0; i < likefood.length; i++){
System.out.println(likefood[i]);}}
在这里插入代码片
publicstaticvoidmain(String[] args){// TODO Auto-generated method stub//5.写一个方法,提示用户输入一个数字n,计算1-n数字之和,使用递归实现。
System.out.println("输入一个数字");
Scanner inputScanner =newScanner(System.in);int num =inputScanner.nextInt();
System.out.println(number(num));}publicstaticintnumber(int num){if(num==1){return1;}return num +number(num-1);}