JavaSE编码试题强化练习3
1.给20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐。
public class TestCirculation {
public static void main(String[] args) {
int money = 20;
int price = 3;
/**
* temp为每一次可以买的可乐的瓶数
*/
int temp = 0;
/**
* change表示每次购买完可乐剩下的钱
*/
int change = 0;
/**
* sum表示总的可乐瓶数
*/
int sum = 0;
/**
* 判断条件:如果钱数大于可乐价格
*/
while (money >= price){
/**
* 购买可乐
*/
temp = money / price;
/**
* 可乐总瓶数增加
*/
sum += temp;
/**
* 计算剩下的钱
*/
change = money % price;
/**
* 兑换可乐瓶子,计算剩余的总钱数
*/
money = change + temp;
}
/**
* 输出结果
*/
System.out.println(sum);
}
}
运行结果:

2.写一个方法对任意数据类型数组进行排序。
public class TestSortArr {
public static void main(String[] args) {
String [] str = {"D","C","B","A"};
sortArr(str);
}
public static void sortArr(Object [] arr){
/**
* 输出排序前的数组
*/
System.out.print("排序前:");
for (int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
/**
* 大循环,一共n个元素,达到最终有序,至多n - 1趟循环
*/
Object temp;
for (int i = 0; i < arr.length - 1;i++) {
/**
* 定义一个符号量,没发生交换,有序
*/
boolean flag = true;
for (int j = 0; j < arr.length - 1 - i; j++) {
/**
* 小循环
*/
Comparable c1 = (Comparable)arr[j];
Comparable c2 = (Comparable)arr[j + 1];
if (c1.compareTo(c2) > 0){
/**
* 交换
*/
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
/**
* 修改符号量,交换过,无序
*/
flag = false;
}
}
/**
* 判断一趟小循环下来数组是否有序
*/
if (flag){
/**
* 中断循环
*/
break;
}
}
/**
* 输出排序后的数组
*/
System.out.print("排序后:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
运行结果:

3. 实现List和Map数据的转换。
思路:
功能1:定义方法public static void listToMap( ){ }将List中Student元素封装到Map中
1) 使用构造方法Student(int stuId, String name, int age, double score)创建多个学生信息并加入List
2) 遍历List,输出每个Student信息
3) 将List中数据放入Map,使用Student的stuId属性作为key,使用Student对象信息作为value
4) 遍历Map,输出每个Entry的key和value
功能2:定义方法public static void mapToList( ){ }将Map中Student映射信息封装到List
1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息
2) 使用构造方法Student(int stuId, String name, int age, double score)创建多个学生信息,并使用Student的stuId属性作为key,存入Map
3) 创建List对象,每个元素类型是StudentEntry
4) 将Map中每个Entry信息放入List对象,遍历List输出key和value
/**
* 创建学生类,实现Comparable接口
*/
public class Student {
/**
* 私有属性:学号,姓名,年龄,分数
*/
private int stuId;
private String name;
private int age;
private double score;
/**
*构造方法
*/
public Student() {
} public Student(int stuId, String name, int age, double score) {
this.stuId = stuId;
this.name = name;
this.age = age;
this.score = score;
}
/**
* getter和setter方法
*/
public int getStuId() {
return stuId;
} public void setStuId(int stuId) {
this.stuId = stuId;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getScore() {
return score;
} public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
public class TestListToMap {
public static void main(String[] args) {
listToMap();
}
public static void listToMap(){
/**
* 1.创建多个学生信息
*/
Student stu1 = new Student(1101,"张三",22,98.0);
Student stu2 = new Student(1102,"李思",23,78.0);
Student stu3 = new Student(1103,"王武",22,92.0);
Student stu4 = new Student(1104,"孙琦",21,98.0);
/**
* 2.加入List
*/
List<Student> list = new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
/**
* 3.遍历List,输出每个学生的信息
*/
Iterator it1 = list.iterator();
while (it1.hasNext()){
Student stu = (Student) it1.next();
System.out.println(stu);
}
System.out.println(
"=============================================================");
/**
* 4.将List中数据存入Map,用Student的stuId属性作为key
*/
Map<Integer,Student> map = new HashMap<Integer,Student>();
/*map.put(stu1.getStuId(),stu1);
map.put(stu2.getStuId(),stu2);
map.put(stu3.getStuId(),stu3);
map.put(stu4.getStuId(),stu4);*/
Iterator it2 = list.iterator();
while (it2.hasNext()){
Student stu = (Student) it2.next();
map.put(stu.getStuId(),stu);
}
/**
* 5.遍历Map,输出每个Entry的key和value
*/
Set<Map.Entry<Integer,Student>> entrySet = map.entrySet();
for (Map.Entry<Integer,Student> entry : entrySet){
System.out.println(entry.getKey()+"---->"+entry.getValue());
}
}
}
运行结果:

/**
* 创建StudentEntry存放key和value的映射关系
*/
public class StudentEntry {
/**
* key-->stuId
* value-->stu
*/
private int key;
private Student stu;
/**
* getter setter方法
*/
public void setKey(int key) {
this.key = key;
}
public void setStu(Student stu) {
this.stu = stu;
} public int getKey() {
return key;
} public Student getStu() {
return stu;
}
}
public class TestMapToList {
public static void main(String[] args) {
mapToList();
}
public static void mapToList(){
/**
* 创建多个学生信息
*/
Student stu1 = new Student(1101,"张三",22,98.0);
Student stu2 = new Student(1102,"李思",23,78.0);
Student stu3 = new Student(1103,"王武",22,92.0);
Student stu4 = new Student(1104,"孙琦",21,98.0);
/**
* 使用Student的id属性作为key,存入Map
*/
Map<Integer,Student> map = new HashMap<Integer,Student>();
map.put(stu1.getStuId(),stu1);
map.put(stu2.getStuId(),stu2);
map.put(stu3.getStuId(),stu3);
map.put(stu4.getStuId(),stu4);
/**
* 遍历Map
*/
Set<Entry<Integer,Student>> entrySet = map.entrySet();
for (Entry<Integer,Student> entry : entrySet){
System.out.println(entry.getKey()+"---->"+entry.getValue());
}
System.out.println(
"============================================================");
/**
* 创建List对象,每个元素的类型都是StudentEntry
* 需创建StudentEntry实体类包含key--stuId和value--stu
*/
List<StudentEntry> list = new ArrayList<StudentEntry>();
/**
* 将Map对象转化为List集合
*/
for (Entry<Integer,Student> entry : map.entrySet()){
StudentEntry studentEntry = new StudentEntry();
/**
* 将Map中的一个映射关系,封装为一个StudentEntry对象
*/
studentEntry.setKey(entry.getKey());
studentEntry.setStu(entry.getValue());
/**
* 将Student对象List集合
*/
list.add(studentEntry);
}
/**
* 遍历List
*/
Iterator<StudentEntry> it = list.iterator();
while (it.hasNext()){
StudentEntry se = it.next();
System.out.println(se.getStu());
}
}
}
运行结果:

JavaSE编码试题强化练习3的更多相关文章
- JavaSE编码试题强化练习1
1. 编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 定义父类--圆类 */ public class Circle { / ...
- JavaSE编码试题强化练习7
1.编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 圆类 */ public class Circle { /** * 类属 ...
- JavaSE编码试题强化练习6
1.写出选择排序的代码实现,对一个int数组进行排序 public class TestSelectSort { public static void main(String[] args) { in ...
- JavaSE编码试题强化练习5
1.不使用函数实现字符串的翻转 /** * 1.不使用函数实现字符串的翻转 */ public class TestStringReverse { public static void main(St ...
- JavaSE编码试题强化练习4
1.编写一个Worker类,为Worker类添加相应的代码,使得Worker对象能正确放入TreeSet中.并编写相应的测试代码. /** * Worker类 */ public class Work ...
- JavaSE编码试题强化练习2
1.编写递归算法程序:一列数的规则如下: 0.1.1.2.3.5.8.13.21.34...... 求数列的第40位数是多少. public class TestRecursion { public ...
- JavaSE面试题
JavaSE面试题 欢迎到我的Git仓库去提交您觉得优秀的内容! 1.是否可以从一个static方法内部发出对非static方法的调用? 不可以.当一个static方法被调用时,可能还没有创建任何实例 ...
- [002] - JavaSE面试题(二):基本数据类型与访问修饰符
第一期:Java面试 - 100题,梳理各大网站优秀面试题.大家可以跟着我一起来刷刷Java理论知识 [002] - JavaSE面试题(二):基本数据类型与访问修饰符 第1问:Java的数据类型有哪 ...
- JavaSE 面试题: 类初始化和实例初始化等
JavaSE 面试题 类初始化和实例初始化等 class Father { private int i = test(); private static int j = method(); stati ...
随机推荐
- PAT Basic 1015 德才论 (25 分)
宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人 ...
- L3-015. 球队“食物链”
某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从中找出一条包含所有球队的“食物链 ...
- socket tcp clinet最简单测试程序
// testsocketclient.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <winsock2.h> ...
- 刷PTA这一周的感悟
在慕课上报名了浙江大学的<数据结构>这门课,主讲人陈越老师,何钦铭老师,两位老师讲的很好,课后有配套的PTA可以用来做题,练习. 最近在PTA上刷题,发现自己代码实现能力是真的差劲,一开始 ...
- 【Heaven Cow与God Bull】题解
题目 Description __int64 ago,there's a heaven cow called sjy... A god bull named wzc fell in love with ...
- 【leetcode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings
题目如下: A string is a valid parentheses string (denoted VPS) if and only if it consists of "(&quo ...
- 关于Vuex的actions传入多个参数的方法:
1.在state中: state={ obj:{ name:'state中的数据' } } 2.在actions定义的方法中: ...
- #1122 JSP动作元素
JSP动作元素 与JSP指令元素不同的是,JSP动作元素在请求处理阶段起作用.JSP动作元素是用XML语法写成的. 利用JSP动作可以动态地插入文件.重用JavaBean组件.把用户重定向到另外的页面 ...
- [CF1082G]Petya and Graph:最小割
分析 发这篇博客的目的就是要让你们知道博主到底有多菜. 类似于[NOI2006]最大获利.(明明就是一模一样好吧!) 不知道怎么了,半秒就想到用网络流,却没想出怎么建图. 连这么简单的题都没做出来,我 ...
- E. Boxers
E. Boxers 给定N个数字,每个数字可以加一或者减一 使得结果集合中不同数字个数最多 贪心 用桶装数 假如相同的数字$i$超过三个,则上面$i+1$,下面$i-1$都可以分一个 如果相同数字$i ...