目录
非循环打印数组元素值
/*
* substring()从指定的字符串索引出开头
* replaceAll()替换元素
*/String str = Arrays.toString(shops).substring(1);
String sti = str.substring(0, str.length()-1).replaceAll(",", "\n");
package com.day5;
import java.util.Arrays;
public class ArrayPrintShop {
public static void main(String[] args) {
String [] shops = {"Nike背包","Adidas运动衫","李宁运动鞋","Kappa外套","361°腰包"};
System.out.println("本次特价商品有:");
// for (int i = 0; i < shops.length; i++) {
// System.out.println(shops[i]);
// }
/*
* substring()从指定的字符串索引出开头
* replaceAll()替换元素
*/
String str = Arrays.toString(shops).substring(1);
String sti = str.substring(0, str.length()-1).replaceAll(",", "\n");
System.out.println(sti);
}
}
ATM模拟 数组存储输入次数
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
public class ArraysATM {
public static void main(String[] args) {
/*
* 简单ATM机取款过程模拟
1.保存用户输入密码
2.取款金额
3.用户密码
4.记录密码输入次数
5.密码是否通过验证
*/
String [] pwd = new String [3]; //用户只能输入三次密码
System.out.println("请输入取款密码(只能输入3次):");
Scanner sc = new Scanner(System.in);
int count = 0; //记录用户输入的密码次数
for (int i = 0; i < pwd.length; i++) {
pwd[i] = sc.nextLine();
count++;
if(pwd[i].equals("12345")) {
//密码成功进行取款
System.out.println("输入取款金额:");
int money = sc.nextInt();
System.out.println("您好,您取款金额为:"+money);
break;
}
if(!(pwd[i].equals("12345"))) {
System.out.println("抱歉密码输入错误,请重新输入");
}
}
System.out.println("您好,这是您输入过的密码:"+Arrays.toString(pwd));
}
}
非循环输出字符数组
给定数组 原序输出 升序输出 逆序输出
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
public class ArraysCharPrint {
public static void main(String[] args) {
/*
*给定一组字符 ,排序 输出
*升序 输出
*逆序输出
*/
char ch [] = {'a','c','u','b','e','p','f','z'};
//数组排序
// for (int i = 0; i < ch.length; i++) {
// System.out.print(ch[i]+" ");
// }
//非循环打印数组
String str = Arrays.toString(ch).substring(1);
String strch = str.substring(0,str.length()-1).replaceAll(",", "");
System.out.print("原字符序列:"+strch);
System.out.println();
Arrays.sort(ch);
//非循环打印升序后的数组
String sortStr = Arrays.toString(ch).substring(1);
String sortStrch = sortStr.substring(0,sortStr.length()-1).replaceAll(",", "");
System.out.print("升序排序后:"+sortStrch);
// for(int i=0;i<ch.length;i++) {
// System.out.print(ch[i]+" ");
// }
/*
System.out.print("\n"+"逆序输出为:");
// for (int i = ch.length-1; i >=0; i--) {
// System.out.print(ch[i]+" ");
// }
StringBuffer strb = new StringBuffer(strch);
System.out.println(strb.reverse());
*/
char ch1 = 'm';
System.out.print("\n待插入的数据为:"+ch1);
System.out.println();
int flag = Arrays.binarySearch(ch, ch1);
int index = -flag-1;
char s_ch[] = new char[ch.length+1];
//拷贝字符数组
System.arraycopy(ch, 0, s_ch, 0, index);
s_ch[index] = ch1;
System.arraycopy(ch, index, s_ch, index+1, ch.length-index);
System.out.print("插入字符的下标为:"+index);
System.out.println();
/*
* 非循环打印插入后的字符序列
*
*/
//1.定义一个字符串 Arrays.toString(s_ch)
String inserts_ch = Arrays.toString(s_ch).substring(1);
//2.字符串截取和替换
String insertch = inserts_ch.substring(0,inserts_ch.length()-1).replaceAll(",", "");
System.out.print("插入后的字符序列为:"+insertch);
// for(int i=0;i<s_ch.length;i++) {
// System.out.print(s_ch[i]+" ");
// }
}
}
插入数据,并保存有序(**********)
学员成绩:99,85,82,63,60
按升序排列,增加学员成绩,并插入一个学员的成绩,保持升序Arrays 类 用法
Arrays.sort(); 排序
Arrays.toString() 打印数组
Arrays.binarySearch(数组名,要查找的值)
数组拷贝:
System.arraycopy(源数组, 索引, 目标数组, 索引, 要复制的数组元素的数量); //数组拷贝
String用法:substring(int beginIndex,int endIndex) 从指定下标返回一个字符串
replaceAll(" "," "); 字符串替换 将指定内容替换
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
/*
* 重点学习一下 arrays 工具类的用法
*
*/
public class ArraysInsert {
public static void main(String[] args) {
/*
* 学员成绩:99,85,82,63,60
* 按升序排列,增加学员成绩,并插入一个学员的成绩,保持升序
*/
//1.定义数组存放学员成绩
int scores[] = {99,85,82,63,60};
int s_scores[] = new int [scores.length+1];
//2.将数组排序
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
int flag = 0;
System.out.println("请输入新增成绩");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
flag = Arrays.binarySearch(scores, score); //适用于有序排列数组
int index = -flag-1; //获取要插入的索引
System.arraycopy(scores, 0, s_scores, 0, index);
s_scores[index] = score;
System.arraycopy(scores, index, s_scores, index+1, scores.length-index); //数组拷贝
System.out.println(Arrays.toString(scores));
//System.out.println(Arrays.toString(s_scores));
System.out.println("插入成绩的下标为:"+index);
/*
* 通过非循环打印数组信息
*/
String str = Arrays.toString(s_scores).substring(1);
String s_score = str.substring(0,str.length()-1).replaceAll(",", " ");
System.out.println("插入的成绩信息是:"+s_score);
// for(int i=0;i<s_scores.length;i++) {
// System.out.print(s_scores[i]+"\t");
// }
}
}
为用户提供注册登录抽奖功能(********)
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
public class ArraysMenuCyclePrint {
//定义一个静态保存会员卡号的变量
static int staticCardNum ;
//定义变量 存放 注册的用户名和密码
static String re_userName ; //注册成功的用户名
static String re_password; //注册成功的密码
//Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String flag = "";
while (true) {
menu();
System.out.print("请选择菜单:");
//Scanner sc = new Scanner(System.in);
int id[] = new int[3];
for (int i = 0; i < id.length; i++) {
id[i] = sc.nextInt();
if (id[i] == 1) {
System.out.println("[奖客富翁系统 >\t注册]");
register();
break;
} else if (id[i] == 2) {
System.out.println("[奖客富翁系统 >\t登录]");
login();
break;
} else if (id[i] == 3) {
System.out.println("[奖客富翁系统 >\t抽奖]");
prizeDraw();
break;
} else {
System.out.println("您的输入有误");
break;
}
}
System.out.println("继续吗?(y/n)");
flag = sc.next();
if (flag.equals("y")) {
menu();
}
if (flag.equals("n")) {
System.out.println("系统退出,谢谢使用!");
break;
}
}
}
public static void menu() {
/*
* 抽奖菜单显示 输出菜单 显示菜单编号,选择编号 如果编号错误,输出:“您的输入有误!”
*
*/
System.out.println("*****欢迎进入奖客富翁系统*****");
System.out.println("\t\t1.注册");
System.out.println("\t\t2.登录");
System.out.println("\t\t3.抽奖");
System.out.println("****************************");
System.out.println();
}
/*
* 注册功能
*/
public static void register() {
// 实现注册功能
System.out.println("请填写个人注册信息");
Scanner sc = new Scanner(System.in);
System.out.print("用户名:");
String userName = sc.nextLine();
System.out.print("密码:");
String password = sc.nextLine();
boolean flag = false;
System.out.println("\n注册成功,请记好您的会员卡号");
/*
* 将注册成功的用户名和密码赋值给上面定义的变量
*/
re_userName = userName;
re_password = password;
flag = true;
// 生成会员卡号
int max = 9999, min = 1000;
int cardNumber = (int) (Math.random() * (max - min)) + min;
if(flag) {
staticCardNum = cardNumber;
}
System.out.println("用户名\t密码\t会员卡号");
System.out.println(userName + "\t" + password + "\t" + cardNumber);
}
/*
* 登录功能
*/
public static void login() {
// 实现登录功能
Scanner sc = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = sc.nextLine();
System.out.print( "请输入密码:");
String password = sc.nextLine();
/*
* 用户输入错误提示用户继续输入,最多三次输入机会
*/
//int count = 0; //计算累计 大于3
for (int i = 0; i < 3; i++) {
if(userName.equals(re_userName)&&password.equals(re_password)) {
System.out.println("\n欢迎您" + userName);
break;
}
if(!userName.equals(re_userName)) {
System.out.println("用户名输入错误,请重新输入");
userName = sc.nextLine();
if(userName.equals(re_userName)&&password.equals(re_password)) {
System.out.println("\n欢迎您" + userName);
break;
}
} if(!password.equals(re_password)) {
System.out.println("密码输入错误,请重新输入");
password = sc.nextLine();
if(userName.equals(re_userName)&&password.equals(re_password)) {
System.out.println("\n欢迎您" + userName);
break;
}
}
}
System.out.println("输入次数超过三次");
}
/*
* 抽奖功能
*/
public static void prizeDraw() {
System.out.println("请输入您的卡号");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int [] numbers = {9402,5668,8042,8512,4213}; //存放幸运卡号数字
for (int i = 0; i < numbers.length; i++) {
if(numbers[i] == number) {
System.out.println("恭喜您成为我们的幸运会员");
break;
}else {
System.out.println("抱歉,您不是本日的幸运会员");
break;
}
}
System.out.print("本日的幸运数字为:");
for(int i=0;i<numbers.length;i++) {
System.out.print(numbers[i]+" ");
}
System.out.println();
}
}
菜单显示
package com.day5;
import java.util.Scanner;
public class ArraysMenuPrint {
public static void main(String[] args) {
/*
* 抽奖菜单显示
* 输出菜单
* 显示菜单编号,选择编号
* 如果编号错误,输出:“您的输入有误!”
*
*/
System.out.println("*****欢迎进入奖客富翁系统*****");
System.out.println("\t\t1.注册");
System.out.println("\t\t2.登录");
System.out.println("\t\t3.抽奖");
System.out.println("****************************");
System.out.print("请选择菜单:");
Scanner sc = new Scanner(System.in);
int id[] = new int[3];
for (int i = 0; i < id.length; i++) {
id[i] = sc.nextInt();
if(id[i]==1) {
System.out.println("[奖客富翁系统 >\t注册]");
break;
}else if(id[i]==2) {
System.out.println("[奖客富翁系统 >\t登录]");
break;
}else if(id[i]==3) {
System.out.println("[奖客富翁系统 >\t抽奖]");
break;
}else {
System.out.println("您的输入有误");
break;
}
}
}
}
逆序输出元素
package com.day5;
import java.util.Arrays;
public class ArraysPrint {
public static void main(String[] args) {
/*
* 逆序输出原数组,并将小于0的元素按0存储
*/
//1.定义数组存放元素
int [] nums = {1,3,-1,5,-2};
//2.输出数组内容
// System.out.println("原数组为:");
// for (int i = 0; i < nums.length; i++) {
// System.out.print(nums[i]+" ");
// }
/*
* 非循环输入原数组
*/
String strnum = Arrays.toString(nums).substring(1);
String reversenum = strnum.substring(0,strnum.length()-1).replaceAll(",", "");
System.out.println("原数组为:\n"+reversenum);
System.out.println("逆序并处理后的数组为:");
//3.逆序输出数组
for (int i = nums.length-1; i >= 0; i--) {
//4.判断数字是否小于0 如果小于0,则为0
if(nums[i]<0) {
nums[i] =0;
}
System.out.print(nums[i]+" ");
}
}
}
猜数游戏(********)
Arrays类中asList() 数组转集合
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
public class dataPrint {
public static void main(String[] args) {
//给定数列:8,4,2,1,23,344,12
/*
* 1.循环输出数列值
* 2.求数列中所有数列值的和
* 3.猜数游戏:从键盘输入任意一个数据,判断数列中是否包含此数
*/
Integer data [] = {8,4,2,1,23,344,12};
double sum = 0;
//定义输入值
System.out.println("猜数游戏:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.print("数列值各值为:");
for(int i = 0; i <data.length;i++) {
//1.输出数列值
System.out.print(data[i]+" ");
//2.求数列值总和
sum += data[i];
}
System.out.println();
System.out.println("数列值总和:"+sum);
//3.猜数游戏
if(Arrays.asList(data).contains(num)) {
System.out.println("真聪明这里面有你猜中的数字");
}else {
System.out.println("猜错啦");
}
}
}
求最低积分
/*
* 输出最低积分及下标
* 1.定义存放积分的数组
* 2.数组元素进行比较
* 输出最低积分及下标
*/
package com.day5;
public class PrintIntegral {
public static void main(String[] args) {
/*
* 输出最低积分及下标
* 1.定义存放积分的数组
* 2.数组元素进行比较
* 输出最低积分及下标
*/
//1.定义数组,存放积分
int integral [] = {15,32,31,30,2,3,43};
//定义一个存放最低积分的变量和所在下标
int min = integral[0];
int flag = 0;
for (int i = 0; i < integral.length; i++) {
if(min>integral[i]) {
min = integral[i];
flag = i;
}
}
System.out.println("最低积分为:"+min);
System.out.println("最低积分所在下标为:"+flag);
}
}
逆序输出5句话
StringBuffer类:
insert() 方法用法
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
public class PrintStatement {
public static void main(String[] args) {
/*逆序输出5句话
*1.输入5句话
*2.逆序打印
*
*/
//1.循环输入5句话
System.out.println("请输入5句话:");
Scanner sc = new Scanner(System.in);
//定义数组存放5句话
String [] str = new String[5];
StringBuffer stb = new StringBuffer();
for(int i=1;i<=str.length;i++) {
System.out.print("第"+i+"句话");
String word = sc.nextLine();
stb.insert(0, "\n"+word); //
}
System.out.println("逆序输出5句话");
System.out.println(stb.toString());
}
}
输出消费金额(********)
/*
* 表格形式输出5笔购物金额和总金额
* 1.创建一个长度为5的double类型数组存储金额
* 2.循环输入5笔购物金额
* 3.利用循环输入,最后求总消费
*/
package com.day5;
import java.util.Arrays;
import java.util.Scanner;
public class ShopSum {
public static void main(String[] args) {
/*
* 表格形式输出5笔购物金额和总金额
* 1.创建一个长度为5的double类型数组存储金额
* 2.循环输入5笔购物金额
* 3.利用循环输入,最后求总消费
*/
//1.定义数组
double moneys [] = new double[5]; //数组长度为5
//2.循环输入金额
double money = 0 ;
//定义求和变量
double sum = 0;
System.out.println("请输入会员本月的消费记录:");
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= moneys.length; i++) {
System.out.print("请输入第"+i+"笔购物金额");
moneys[i-1] = sc.nextDouble();
sum+=moneys[i-1];
}
System.out.println("序号\t\t金额(元)");
//打印输入的数据
for(int j=1;j<=moneys.length;j++) {
System.out.println(j+"\t\t"+moneys[j-1]);
}
System.out.println("总金额:"+sum);
}
}