day16-java

综合训练题

1.数组随机排列

package com.shifan.base.arrays;

import java.util.Arrays;
import java.util.Random;

/**
 * 数组随机排列
 */
public class Case1 {
    public static void main(String[] args) {
        int[] arr = {23,54,25,78,34};
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            //定义中间变量用于交换数组元素值
            int temp = arr[i];
            //随机索引交换数组元素
            int j = r.nextInt(arr.length);
            arr[i] = arr[j];
            arr[j] = temp;
        }
        //调用Arrays类的toString方法将数组转换为字符串输出
        System.out.println(Arrays.toString(arr));
    }
}

2.反转数组

package com.shifan.base.arrays;

import java.util.Arrays;

/**
 * 请补充代码反转数组
 * 例如:数组arr[]={1,2,3},反转后数组为arr[]={3,2,1}
 */
public class Test1 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println("反转前数组为:");
        System.out.println(Arrays.toString(arr));
        for (int i = 0; i < (arr.length - i); i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length - i - 1];
            arr[arr.length - i - 1] = temp;
        }
        System.out.println("反转前数组为:");
        System.out.println(Arrays.toString(arr));
    }
}

3.整形数组转换为整数值

package com.shifan.base.arrays;

/**
 * 现有一个整数数组,数组中的每个元素都是[0-9]之间的数字,
 * 从数组的最大索引位置开始到最小索引位置,
 * 依次表示整数的个位、十位、百位。。。依次类推。
 * 请编写程序计算,这个数组所表示的整数值。例如:
 * 数组:{2, 1, 3, 5, 4}
 * 表示的整数为:21354
 * 注:是整数类型的两万一千三百五十四,不是字符串拼起来的。
 */
public class Test4 {
    public static void main(String[] args) {
        int[] arr = {3, 5, 2, 6, 2, 6};
        int sum = 0, a = 1;
        for (int i = arr.length - 1; i >= 0; i--) {
            sum += arr[i] * a;
            a *= 10;
        }
        System.out.println(sum);
    }
}

4.有序数组插入元素

package com.shifan.base.arrays;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 有一个数组,其中有十个元素从小到大依次排列
 * {12,14,23,45,66,68,70,77,90,91}。
 * 再通过键盘录入一个整数数字。
 * 要求:把数字放入数组序列中,生成一个新的数组,
 * 并且数组的元素依旧是从小到大排列的。执行效果如下:
 * 请输入一个整数数字:
 * 50
 * 生成的新数组是:12 14 23 45 50 66 68 70 77 90
 */
public class Test5 {
    public static void main(String[] args) {
        int[] arr ={12,14,23,45,66,68,70,77,90,91};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int num = sc.nextInt();
        int[] arr1 = new int[arr.length+1];
        int index = 0;
        for (int i = arr.length-1; i >= 0; i--) {
            if (arr[i]>=num){
                arr1[i+1] = arr[i];
                index = i;
            }else {
                arr1[i] = arr[i];
            }
        }
        arr1[index] = num;
        System.out.println(Arrays.toString(arr1));
    }
}

5.打印偶数

package com.shifan.base.struct;

/**
 * 打印1-1000之间所有偶数,二个一行再三个一行
 */
public class Test1 {
    public static void main(String[] args) {
        int j = 0;
        for (int i = 1; i <= 1000; i++) {
            if (i % 2 ==0){
                if (j == 2){
                    System.out.println("\n");
                    System.out.print(i + "\t");
                    j++;
                }else if(j == 5){
                    System.out.println("\n");
                    System.out.print(i + "\t");
                    j=1;
                }
                else{
                    System.out.print(i + "\t");
                    j++;
                }
            }
        }
    }
}

6.随机数组去重反转

package com.shifan.base.struct;

import java.util.Arrays;
import java.util.Random;

/**
 * 产生10个1-100之间的随机数存在数组中
 * 随机数不能重复
 * 反转数组再遍历打印在同一行
 */
public class Test2 {
    public static void main(String[] args) {
        Random r = new Random();
        int[] arr = new int[10];
        for (int i = 0; i < 10; i++) {
                arr[i] = r.nextInt(101)+1;
                //比较去重
            for (int j = i-1; j >= 0; j--) {
                if (arr[j] == arr[i]){
                    i--;
                }
            }
        }
        System.out.println("随机数组为:");
        System.out.println(Arrays.toString(arr));
        System.out.println("\n================================================================");
        for (int j = 0; j < arr.length-j-1; j++) {
            int temp = arr[j];
            arr[j] = arr[arr.length-j-1];
            arr[arr.length-j-1] = temp;
        }
        System.out.println("反转数组为:");
        System.out.println(Arrays.toString(arr));
    }
}

7.字符串比较

package com.shifan.base.string;

/**
 * 字符串比较
 */
public class TestString {
    public static void main(String[] args) {
        String t = new String("hello");
        char c[] = {'h','e','l','l','o'};
        //hello
        System.out.println(c);
        //false
        System.out.println(t.equals(c));
    }
}

8.生成验证码

package com.shifan.base.exercises;

import java.util.Random;

/**
 * 在登录注册页面中,除了用户名和密码外,通常也会包含验证码。
 * 验证码是用来区分用户是计算机还是人,防止恶意破解密码、刷票、灌水等行为。
 * 请查看Random、StringBuilder相关API,
 * 定义方法,获取一个包含4个字符的验证码,每一位字符是随机选择的字母和数字,
 * 可包含a-z,A-Z,0-9。例如:
 */
public class Exercise01 {
    public static void main(String[] args) {
        String s = getVerifyCode();
        System.out.println(s);
    }

    private static String getVerifyCode() {
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        String str = "";
        addChar(sb);
        for (int i = 0; i < 4; i++) {
            char c = sb.charAt(r.nextInt(sb.length()));
            str += c;
        }
        return str;
    }

    private static void addChar(StringBuilder sb) {
        for (char i = 'A'; i <= 'Z'; i++) {
            sb.append(i);
        }
        for (char i = 'a'; i <= 'z'; i++) {
            sb.append(i);
        }
        for (char i = '0'; i <= '9'; i++) {
            sb.append(i);
        }
    }
}

9.判断身份证号码合法性

package com.shifan.base.exercises;

import java.util.Scanner;
/**
 * 我国的居民身份证号码,由由十七位数字本体码和一位数字校验码组成。
 * 请定义方法判断用户输入的身份证号码是否合法,
 * 并在主方法中调用方法测试结果。
 * 规则为:号码为18位,不能以数字0开头,前17位只可以是数字,
 * 最后一位可以是数字或者大写字母X。
 * 注意:startsWith方法的形参类型为字符串
 */
public class Exercise02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入身份证号:");
        String s = sc.nextLine();
        boolean b = judgeLegitimacy(s);
        if (b){
            System.out.println("合法");
        }else{
            System.out.println("不合法");
        }
    }

    private static boolean judgeLegitimacy(String s) {
        boolean a =true;
        //判断长度是否为18位
        if (s.length()!=18){
            a = false;
        }
        //判断是否以数字0开头,调用starsWith方法
        if (s.startsWith("0")){
            a = false;
        }
        for (int i = 0; i < s.length(); i++) {
            if(i == s.length()-1){
                //判断最后一位是否为数字或大写X
                if (s.charAt(i)<'0'||s.charAt(i)>'9'&&s.charAt(i)!='X'){
                    a = false;
                }
            }else {
                //判断中间16位是否为数字
                if (s.charAt(i)<'0'||s.charAt(i)>'9'){
                    a = false;
                }
            }
        }
        return a;
    }
}

10.统计字符串

package com.shifan.base.exercises;

/**
 * int indexOf(String str) :查找参数字符串str在调用方法的字符串中第一次出现的索引,如果不存在,返回-1
 * String substring(int beginIndex):截取字符串,从索引beginIndex(包含)开始到字符串的结尾
 * 现有如下文本:"Java语言是面向对象的,Java语言是健壮的,Java语言是安全的,Java是高性能的,Java语言是跨平台的"。
 * 请编写程序,统计该文本中"Java"一词出现的次数。
 */
public class Exercise03 {
    public static void main(String[] args) {
        String s = "Java语言是面向对象的,Java语言是健壮的,Java语言是安全的,Java是高性能的,Java语言是跨平台的";
        System.out.println(s);
        int count = 0;
        while (true) {
            int index = s.indexOf("Java");
            if (index!=-1){
                count++;
                s = s.substring(index+1);
            }else {
                break;
            }
        }
        System.out.println("Java出现次数为:"+count);
    }
}

11.图书管理系统

package com.shifan.base.exercises;

import com.shifan.domain.Book;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 利用面向对象的思想设计一个图书管理系统。
 * 图书的属性有:编号,书名,作者,价格。要求提供如下功能:
 * 1、提供操作菜单,可以选择要进行的操作。
 * 2、可以添加图书,添加图书时,编号需要唯一,添加成功,返回到菜单。
 * 3、可以查询图书,显示所有图书信息,然后返回到菜单。
 * 4、可以根据书名,查询单本图书信息,显示信息后,返回到菜单。
 * 5、可以删除图书,通过编号删除,删除成功后,返回到菜单。
 * 6、可以修改图书的信息,但编号不可以修改,修改成功后,返回到菜单。
 * 7、可以退出系统,结束程序运行。
 * 提示:
 * 1、所有图书信息由键盘录入
 * 2、图书的价格可以定义为字符串类型,
 * 因为在键盘录入时,不可以先使用nextInt()方法获取整数然后再使用nextLine()方法获取字符串,这样会导致nextLine()方法获取不到数据。
 * @author shifan
 */
public class Exercise04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Book> books = new ArrayList<>();
        lo:while (true){
            System.out.println("==========图书管理系统==========");
            System.out.println("1.添加图书");
            System.out.println("2.查看所有图书");
            System.out.println("3.查询书籍");
            System.out.println("4.删除图书");
            System.out.println("5.修改图书信息");
            System.out.println("6.退出系统");
            System.out.println("请输入数字选择功能:");
            int i = sc.nextInt();
            switch (i){
                case 1:
                    addBook(books);
                    break;
                case 2:
                    showBook(books);
                    break;
                case 3:
                    searchBook(books);
                    break;
                case 4:
                    deleteBook(books);
                    break;
                case 5:
                    modifyBook(books);
                    break;
                case 6:
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("输入错误,请输入数字1-6!");
                    break;
            }
        }
    }

    private static void modifyBook(ArrayList<Book> books) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入编号:");
        String bookNum = sc.nextLine();
        int i = searchBookNum(books, bookNum);
        if(i != -1){
            System.out.println("请输入新书名:");
            String newBookName = sc.nextLine();
            if ("".equals(newBookName)){
                System.out.println("书名不能为空");
                return;
            }
            System.out.println("请输入新作者:");
            String newBookAuthor = sc.nextLine();
            System.out.println("请输入新价格:");
            double newBookPrice = sc.nextDouble();
            Book book = books.get(i);
            book.setName(newBookName);
            book.setAuthor(newBookAuthor);
            book.setPrice(newBookPrice);
            System.out.println("修改成功");
        }else {
            System.out.println("没有这本书");
        }
    }

    private static void deleteBook(ArrayList<Book> books) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入编号:");
        String bookNum = sc.nextLine();
        if(searchBookNum(books,bookNum)!= -1){
            books.remove(searchBookNum(books,bookNum));
            System.out.println("删除成功");
        }else {
            System.out.println("不存在这本书");
        }
    }

    private static void searchBook(ArrayList<Book> books) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名:");
        String bookName = sc.nextLine();
        boolean a = true;
        for (Book book : books) {
            if (bookName.equals(book.getName())){
                book.show();
                a = false;
            }
        }
        if (a){
            System.out.println("未找到这本书");
        }
    }

    private static void showBook(ArrayList<Book> books) {
        if (books.size() == 0){
            System.out.println("没有书籍,请添加后重试");
        }else {
            System.out.println("编号" + "\t" + "书名" + "\t" + "作者" + "\t" + "价格");
            for (Book book : books) {
                System.out.print(book.getNum()+"\t");
                System.out.print(book.getName()+"\t");
                System.out.print(book.getAuthor()+"\t");
                System.out.print(book.getPrice()+"\n");
            }
        }
    }

    private static void addBook(ArrayList<Book> books) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入编号:");
        String num = sc.nextLine();
        if ("".equals(num)){
            System.out.println("编号不能为空");
            return;
        }
        if (searchBookNum(books,num)!=-1) {
            System.out.println("编号已存在");
            return;
        }
        System.out.println("请输入书名");
        String name = sc.nextLine();
        if ("".equals(name)){
            System.out.println("书名不能为空");
            return;
        }
        System.out.println("请输入作者");
        String author = sc.nextLine();
        System.out.println("请输入价格");
        Double price = sc.nextDouble();
        Book book = new Book(num, name, author, price);
        books.add(book);
        System.out.println("添加成功");
    }

    private static int searchBookNum(ArrayList<Book> books,String s) {
        int a = -1;
        for (int i = 0;i < books.size();i++) {
            if(s.equals(books.get(i).getNum())){
                a = i;
                break;
            }
        }
        return a;
    }
}

12.机票打折

package com.shifan.base.exercises;

import java.util.Scanner;

/**
 * 需求:用户购买机票时,机票原价会按照淡季、旺季,头等舱还是经济舱的情况进行相应的优惠,
 * 优惠方案如下:5-10月为旺季,头等舱9折,经济舱8.5折;
 * 11月到来年4月为淡季,头等舱7折,经济舱6.5折,
 * 请开发程序计算出用户当前机票的优惠价。
 */
public class Exercise05 {
    public static void main(String[] args) {
        int type;
        int month;
        double price,discountPrice;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入舱位类型(0:经济舱,1:头等舱)");
        type = sc.nextInt();
        System.out.println("请输入月份");
        month = sc.nextInt();
        System.out.println("请输入原价");
        price = sc.nextDouble();
        discountPrice = calculatePrice(type,month,price);
        System.out.println("折扣价为:"+ discountPrice);
    }

    private static double calculatePrice(int  type,int month,double price) {
        if (type == 1){
            if (month >= 5&& month <= 10){
                price*=0.9;
            }else if (month == 1||month == 2|| month ==3||month ==4||month == 11||month ==12){
                price *= 0.7;
            }else {
                System.out.println("月份输入错误");
            }
        }else if(type ==0){
            if (month >= 5&& month <= 10){
                price*=0.85;
            }else if (month == 1||month == 2|| month ==3||month ==4||month == 11||month ==12){
                price *= 0.65;
            }else {
                System.out.println("月份输入错误");
            }
        }else {
            System.out.println("舱位类型输入错误");
        }
        return price;
    }
}

13.密码加密、解密

package com.shifan.base.exercises;

import java.util.Scanner;

/**
 * 某系统的数字密码是一个四位数,如1983,为了安全,需要加密后再传输,
 * 加密规则是:对密码中的每位数,都加5 ,再对10求余,最后将所有数字顺序反转,
 * 得到一串加密后的新数,请设计出满足本需求的加密程序!
 */
public class Exercise06 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入4位密码:");
            //接收密码
            int x = sc.nextInt();
            //判断是否为四位数
            if (x<1000||x>9999){
                System.out.println("输入错误,不是四位数");
                continue;
            }
            System.out.println("加密后:"+encrypt(x));
            x = encrypt(x);
            System.out.println("解密后:"+decode(x));
            break;
        }
    }

    /**
     *对加密的密码进行解密
     * @param code
     * @return 解密后的密码
     */
    private static int decode(int code) {
        int[] arr = new int[4];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (code%10+5)%10;
            code /= 10;
        }
        int count = 1000;
        for (int i : arr) {
            code += i*count;
            count /= 10;
        }
        return code;
    }

    /**
     * 对密码每一位都进行加5、对10取模操作,然后反转密码顺序形成新密码
     * @param x 原始密码
     * @return 返回新密码
     */
    private static int encrypt(int x) {
        int count = 1;
        int[] arr = new int[4];
        //遍历数组加密
        for (int i = 0; i < arr.length; i++) {
            arr[i] = x %10;
            arr[i]+=5;
            arr[i]%=10;
            x/=10;
        }
        for (int i : arr) {
            x += i*count;
            count*=10;
        }
        return x;
    }
}

14.模拟双色球

package com.shifan.base.exercises;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * 双色球彩票,由前区6个红球号码和后区1个蓝球号码组成。
 * 前区6个红球号码是从1~33中选择;后区蓝球号码是从1~16中选择;
 * 而且前区6个红球号码是不能重复的。
 * 编写程序,能够让用户录入一注双色球彩票;也可以随机产生一注双色球彩票
 * @author shifan
 */
public class Exercise07 {
    public static void main(String[] args) {
        int[] userNum = getUserNum();
        int[] systemNum = getSystemNum();
        System.out.println("中奖号码为:"+ Arrays.toString(systemNum));
        judgeNum(userNum, systemNum);
    }

    /**
     * 比较双色球各位号码,判断中奖等级
     * @param userNum 用户双色球号码数组
     * @param systemNum 系统双色球号码数组
     */
    private static void judgeNum(int[] userNum, int[] systemNum) {
        int countRed = 0,countBlue = 0;
        for (int i = 0; i < userNum.length; i++) {
            if (i != userNum.length-1 && systemNum[i] == userNum[i]){
                countRed++;
            }else if (systemNum[i] == userNum[i]){
                countBlue++;
            }
        }
        switch (countBlue + countRed){
            case 0:
                System.out.println("很抱歉,您没有中奖");
                break;
            case 1:
            case 2:
            case 3:
                System.out.println("恭喜您,中奖5元");
                break;
            case 4:
                System.out.println("恭喜您,中奖10元");
                break;
            case 5:
                System.out.println("恭喜您,中奖200元");
                break;
            case 6:
                if (countBlue == 1){
                    System.out.println("恭喜您,中奖3000元");
                }else{
                    System.out.println("恭喜您,中奖500万元");
                }
                break;
            case 7:
                System.out.println("恭喜您,中奖1000万元");
                break;
            default:
                break;
        }
    }

    /**
     * 生成随机双色球号码
     * @return 返回随机号码数组
     */
    private static int[] getSystemNum() {
        Random r = new Random();
        int[] systemNum = new int[7];
        for (int i = 0; i < systemNum.length; i++) {
            if (i <= 5){
                systemNum[i] = r.nextInt(33)+1;
            }else {
                systemNum[i] = r.nextInt(16)+1;
            }
        }
        return systemNum;
    }

    /**
     * 获取用户输入的双色球号码
     * @return 返回用户号码数组
     */
    private static int[] getUserNum() {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[7];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第"+(i+1)+"个双色球号码:");
            int x = sc.nextInt();
            if (i <= 5){
                if (x >= 1 && x <= 33){
                    arr[i] = x;
                }else {
                    System.out.println("红球号码应在1-33范围内!请重新输入");
                    i--;
                    continue;
                }
            }else {
                if (x >= 1 && x <= 16){
                    arr[i] = x;
                }else {
                    System.out.println("蓝球号码应在1-16范围内!请重新输入");
                    i--;
                    continue;
                }
            }
        }
        return arr;
    }
}

15.找素数

package com.shifan.base.exercises;

/**
 * 判断101-200之间有多少个素数
 * 统计个数并输出所有素数
 */
public class Exercise08 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 101; i <= 200; i++) {
            int j;
            for (j = 2; j < i/2; j++) {
                if (i % j ==0){
                    break;
                }
            }
            if (j >= i/2){
                count++;
                System.out.print(i+"\t");
            }
        }
        System.out.println("\n"+count);
    }
}

16.生成随机数组,查找数字位置

package com.shifan.base.exercises;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * 1. 定义一个方法实现获取一个int数组:
 * 键盘录入数组的长度和随机数的范围(范围区间要大于数组长度),
 * 在数组中存储指定范围的随机数,并且随机数不能重复
 * 2. 定义一个方法实现查找索引:键盘录入一个数,
 * 在指定的数组中获取该数在数组中的索引,如果没有则返回-1。
 * 有,有几个索引返回几个索引
 * @author shifan
 */
public class Exercise09 {
    public static void main(String[] args) {
        int[] arr = getArray();
        System.out.println(Arrays.toString(arr));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int i = sc.nextInt();
        int[] index = searchNum(i,arr);
        if (index.length!=0) {
            System.out.println("这个数的索引有:");
            System.out.println(Arrays.toString(index));
        } else {
            System.out.println("没有这个数");
        }
    }

    /**
     * 键盘录入一个数,
     * 在指定的数组中获取该数在数组中的索引,如果没有则返回-1。
     * 有,有几个索引返回几个索引
     * @param num 键盘录入的数
     * @param arr 被查找的数组
     * @return 返回索引数组
     */
    private static int[] searchNum(int num,int[] arr) {
        int index = -1,j = 0;
        int[] ints = new int[arr.length];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = index;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num){
                ints[j] = i;
                j++;
            }
        }
        int[] arr1 = new int[j];
        for (int i = 0,k = 0; i < ints.length; i++) {
            if (ints[i] != -1){
                arr1[k] = ints[i];
                k++;
            }
        }
        return arr1;
    }

    /**
     * 键盘录入数组的长度和随机数的范围(范围区间要大于数组长度),
     * 在数组中存储指定范围的随机数,并且随机数不能重复
     * @return 返回生成的随机数组
     */
    private static int[] getArray() {
        Random r = new Random();
        System.out.println("请输入数组长度:");
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int[] arr = new int[len];
        int bound1,bound2;
        while (true) {
            System.out.println("请输入数组元素范围下限:");
            bound1 = sc.nextInt();
            System.out.println("请输入数组元素范围上限:");
            bound2 = sc.nextInt();
            if ((bound2-bound1) < len){
                System.out.println("该范围要大于数组长度");
            }else {
                break;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt((bound2-bound1)+1)+bound1;
            //去重
            for (int j = i-1; j >= 0; j--) {
                if (arr[j] == arr[i]){
                    i--;
                }
            }
        }
        return arr;
    }
}

17.生成一组随机数打印最小值

package com.shifan.base.exercises;

import java.util.Arrays;
import java.util.Random;

/**
 * 随机生成10个15~85的随机数(包括15和85),打印这些随机数和其中的最小值
 */
public class Exercise10 {
    public static void main(String[] args) {
        Random r = new Random();
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(71)+15;
        }
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]<min){
                min = arr[i];
            }
        }
        System.out.println(Arrays.toString(arr));
        System.out.println(min);
    }
}

知识点

1.nextLine方法不能和nextInt方法一起使用

package com.shifan.base.scanner;

import java.util.Scanner;
/**当nextLine方法与nextInt方法配合使用的时候
因为nextInt方法以Enter键结束
nextLine方法遇到Enter停止接收
推荐使用next方法与nextInt方法配合使用*/
public class Demo05Scanner {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入数字");
        int i = s.nextInt();
        System.out.println("请输入字符串");
        String s1 = s.nextLine();
        System.out.println(i);
        System.out.println(s1);
    }
}

2.常量的定义

package com.shifan.base.data;

/**
 * 常量的定义
 * 在Java语言中,用关键字final来定义一个常量。常量一旦被初始化后不能再更改。
 * 声明格式: final type varName =value;
 * 常量的命名:全用大写字母,单词之间下滑线隔开
 * 为了更好的区分和表述,一般将1、23'a'b’truefalse "helloWorld等称为字符常量,而使用final修饰的PI等称为符号常量。
 */
public class Constant {
    // 静态常量
    public static final double PI = 3.14;
    // 声明成员常量
    final int y = 10;

    public static void main(String[] args) {
        // 声明局部常量
        final double x = 3.3;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值