java—Scanner类

类似于python里的input:

package day7;
import java.util.Scanner;
public class ScannerClass {
    String name;
    int age;
    public void infoInteraction(){//数据交互
        System.out.println("==============in the infoInteraction=============");
        Scanner info = new Scanner(System.in);
        System.out.println("please input your name:");
        name = info.next();
        System.out.println("please input your age:");
        age = info.nextInt();
    }

    public Long sum(){//求和
        System.out.println("================in the sum================");
        Scanner count = new Scanner(System.in);
        System.out.println("please input a:");
        Long a = count.nextLong();
        System.out.println("please input b:");
        Long b = count.nextLong();
        System.out.println("please input c:");
        Long c = count.nextLong();
        System.out.println("the sum is :"+(a+b+c));
        return a+b+c;
    }

    public Long max(){//求最大值
        System.out.println("================in the max================");
        Scanner num = new Scanner(System.in);
        System.out.println("please input a:");
        Long a = num.nextLong();
        System.out.println("please input b:");
        Long b = num.nextLong();
        System.out.println("please input c:");
        Long c = num.nextLong();
        Long max;
        max = a > b ? a : b;
        max = max > c ? max : c;
        System.out.println("the max is :"+max);
        return max;
    }
}



另起一个类:
package day7;

public class Tes {
    public static void main(String[] args) {
        ScannerClass a = new ScannerClass();
        a.infoInteraction();
        System.out.println("your name is :"+a.name+","+"your age is :"+a.age);
        a.sum();
        a.max();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值