short age;//定义一个装年龄的变量,变量名应做到 “见名知意”
package com.company;
import java.util.Scanner;//必须导入此包才可使用输入
public class inputData {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);//new一个可输入的对象scanner1
System.out.println("请输入您的年龄");
short age = scanner1.nextShort();//接收用户输入值
//注意:若short age这儿改为int age,那么nextShort也应该改变为nextInt()
System.out.println("您的年龄是" + age);//输出用户的年龄
}
}
2.基本用法(这里只做定义变量举例)
例如我要装年龄,年龄一定是整数,所以我选择整型,也许有人活过byte类型的最大值127岁,那么我选择short类型,