Java教程

java—scanner类

本文主要是介绍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();
    }
}

这篇关于java—scanner类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!