【JAVA SE】输入与输出/方法的的使用、重载和签名

while循环

1.break:/循坏提前结束;
2.continue:跳过这次循环,立即进入下次循环;
3.调试代码:打断点,一步一步来;

for循环

1.for(表达式1;布尔表达式2;表达式3){
表达式4}
2.表达式1一般为初始化条件;
3.执行顺序:1243.243.243;
4.输入时fori->回车;

do while循环

1.先执行循环语句,再判断循环条件,循环条件成立则继续执行,否则循环结束;

输出

public static void main(String[] args) {
        System.out.println("输出且换行");
        System.out.print("输出不换行");
        System.out.printf("%s\n", "格式化输出");
    }

输入

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//alt+enter找导包import java.util.Scanner;//导包
        System.out.println("输入姓名:");
        //String name = scanner.nextLine();//读取一行
        String name = scanner.next();//遇到空格就结束
        System.out.println(name);

        System.out.println("输入年龄:");
        int age = scanner.nextInt();
        System.out.println(age);

        System.out.println("输入体重:");
        float g =scanner.nextFloat();
        System.out.println(g);

        scanner.close();//关闭资源

}

循环输入

 public static void main(String[] args) {//import java.util.Scanner;//导包
        Scanner scanner = new Scanner(System.in);
        /*while(scanner.hasNext()){//循环输入整型
            int a= scanner.nextInt();
            System.out.println(a);
        }*/
        while(scanner.hasNext()){//循环输入(多组输入);执行结果:idea->ctrl+d结束;cmd->ctrl+z结束
            String name = scanner.nextLine();
            System.out.println(name);
        }
    }

题目1:猜数游戏

import java.util.Random;//导包
import java.util.Scanner;//导包
public class exercise3{
       public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        //系统生成一个随机数
        Random random = new Random();

        int radnum =random.nextInt(100);//[0-100)
        while(true){
            System.out.println("请输入你要猜的数字:");
            int num = a.nextInt();
            if(num<radnum){
                System.out.println("猜小了");
            }else if(num>radnum){
                System.out.println("猜大了");
            }else{
                System.out.println("猜对了");
                break;
            }
          }
       }
    }

题目2:根据年龄,打印不同内容;

public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        int age = a.nextInt();
        if(age <= 18){
            System.out.println("少年");
        }else if(age >=19 &&age <= 28){
            System.out.println("青年");
        }else if(age >=29 && age <= 55){
            System.out.println("中年");
        }else{
            System.out.println("老年");
        }
    }

题目3:输出九九乘法表(使用for循环的嵌套)

public static void main(String[] args) {

        for (int i = 1; i <= 9; i++) {
            for (int j =1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+(i*j)+" ");
            }
            System.out.println();
        }
    }
//输出结果:
//1*1=1 
//1*2=2 2*2=4 
//1*3=3 2*3=6 3*3=9 
//1*4=4 2*4=8 3*4=12 4*4=16 
//1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
//1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
//1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
//1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
//1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 

题目4:求俩个正整数的最大公约数

public static void main(String[] args) {
       Scanner p =new Scanner(System.in);
       int a = p.nextInt();
       int b = p.nextInt();
       int c = a%b;
       while(c!=0){
           a = b;
           b = c;
           c =a%b;
       }
        System.out.println(b);
    }

题目5:求出0~999之间的所有“水仙花数”并输出(水仙花数是其各位数字的立方和刚好等于该数本身)

public static void main(String[] args) {

        for (int i = 1; i < 999999; i++) {
            int count =0;
            int t = i;
            while(t != 0){//求当前数字是几位数
                count ++;
                t /=10;
            }
            t =i;
            int sum=0;
            while(t!=0){
                sum += Math.pow(t%10,count);//引用math.pow函数
                t /=10;
            }
            if(sum ==i){
                System.out.println(i);
            }
        }
    }

题目6:写一个函数返回参数二进制中1的个数(eg:15的二进制0000 1111,有4个1);

public static void main(String[] args) {
        //n&(n-1)--> n==0
        int n = 7;
        int count = 0;
        while(n != 0){
            count++;
            n = n & (n-1);
        }
        System.out.println(count);
    }

题目7:获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列

public static void main(String[] args) {
         int n=7;
        for (int i = 30; i >=0 ; i-=2) {
            System.out.print(((n>>i)&1)+" ");
        }
        System.out.println();
        for (int i = 31; i >=1; i-=2) {
            System.out.print(((n>>i)&1)+" ");
        }
    }
    //运行结果
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 

方法的使用

1.方法就是函数。
2.public static(修饰符) 返回值 方法名(形式参数列表){
方法体,方法的具体实现内容 ;
}

public static int add(int a,int b) {//实现俩个整数相加
         return a+b;
    }

3.返回值类型:如果方法有返回值,返回值类型必须要与返回的实体类型一致,如果没有返回值,必须写void.
4.方法名:采用小驼峰命名。
5.参数列表:如果方法没有参数,()中什么都不写,如果有参数,需指定参数类型,多个参数之间使用逗号隔开。
6.在java中,方法必须写在类当中,方法不能嵌套定义,没有声明方法的说法。
7.方法的传参:匹配参数的的个数,类型,顺序以及返回值的类型。

 public static int add(int a,int b) {//实现俩个整数相加
         return a+b;
    }
  public static void main(String[] args) {
        int x=1,y=2;//x,y是实参;a,b是形参
       add(x,y);
    }

8.小练习:求1!+2!+3!+4!+5!

 //求某一个数字的阶乘
    public static int fac(int n) {
        int ret =1;
        for (int i = 1; i <=n; i++) {
          ret *=i;
        }
        return ret;
    }
    //求某一个数字的阶乘和
    public static int facSum(int n){
        int sum=0;
        for (int i = 1; i <=n; i++) {
            sum +=fac(i);
        }
        return sum;
    }

    public static void main(String[] args) {
        int ret = facSum(5);
        System.out.println(ret);
    }
}

9.JAVA 中是无法获取到局部变量的地址的(C可以)。(得用类和对象)
10.没有返回值用void,无需return (或者说return代表方法结束,后边的代码不会被执行)。

方法的重载

1.方法名一样。
2.方法的参数列表不一样【个数,数据类型,顺序】(三者一者不一样都可以构成重载)。(类型的次序必须不同)
3.返回值是否一样不影响方法重载。
4.重载与返回值类型是否相同无关。

方法签名

概念:经过编译器编译修改过之后方法最终的名字。
具体方式:方法全路径名+参数列表+返回值类型,构成方法完整的名字。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值