zoukankan      html  css  js  c++  java
  • java 获取用户输入

    import java.util.Scanner;
    
    
    public class Sample {
    
        public static void main(String[] args) {
            int num;
            Scanner ip = new Scanner(System.in);
            System.out.print("Enter a number: ");
            num = ip.nextInt();
            System.out.println("The input number is " + num);
            ip.close(); // necessary to avoid memory leaks
        }
    }
    
    
    
    OUTPUT:
    Enter a number: 35
    The input number is 35

     获取多个整数输入

    import java.util.Scanner;
    
    public class Sample {
    
        public static void main(String[] args) {
            int num1, num2;
            Scanner ip = new Scanner(System.in);
            System.out.print("Enter num1: ");
            num1 = ip.nextInt();
            System.out.print("Enter num2: ");
            num2 = ip.nextInt();
            System.out.println("The input number is " + num1);
            System.out.println("The input number is " + num2);
            ip.close();
        }
    }
    
    
    
    OUTPUT:
    Enter num1: 10
    Enter num2: 50
    The input number is 10
    The input number is 50

    获取字符串

    import java.util.Scanner;
    
    public class Sample {
    
        public static void main(String[] args) {
            String str;
            Scanner ip = new Scanner(System.in);
            System.out.print("Enter a string: ");
            str = ip.nextLine();
            System.out.println("The input string is " + str);
            ip.close();
        }
    }
    
    
    
    OUTPUT:
    Enter a string: I love dragon ball z
    The input string is I love dragon ball z

    获取多个字符串

    import java.util.Scanner;
    
    public class Sample {
    
        public static void main(String[] args) {
            String str1, str2, str3;
            Scanner ip = new Scanner(System.in);
            System.out.print("Enter string1: ");
            str1 = ip.nextLine();
            System.out.print("Enter string2: ");
            str2 = ip.nextLine();
            System.out.print("Enter string3: ");
            str3 = ip.nextLine();
            System.out.println("str1 is " + str1);
            System.out.println("str2 is " + str2);
            System.out.println("str3 is " + str3);
            ip.close();
        }
    }
    
    
    
    OUTPUT:
    Enter string1: Goku
    Enter string2: Gohan
    Enter string3: Vegeta
    str1 is Goku
    str2 is Gohan
    str3 is Vegeta
  • 相关阅读:
    [Debug] Make python2.7 use the right version of opencv
    路线图 | 学习OpenCV路线图
    学习笔记 | Princeton
    书单 | 2017年阅读书单
    路线图 | 摄影师成长路线
    学习笔记 | Morvan
    如何在pycharm中进入shell脚本调试代码
    python3运行报错:TypeError: Object of type 'type' is not JSON serializable解决方法(详细)
    动态规划的引入 P1616 疯狂的采药【完全背包】
    动态规划的引入 P1048 采药【01背包】
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12066099.html
Copyright © 2011-2022 走看看