1.输入学生的姓名,将学生姓名按照字母顺序进行排序
注意:有问题
package com.jredu.ch09;
import java.util.Arrays;
import java.util.Scanner;
public class Order {
/**
* 获取排序之后的数组
* @return
*/
public String[] getSortArray() {
//排序前的数组
System.out.println("请输入五个学生姓名");
String[] stu=new String[5];
Scanner in=new Scanner(System.in);
for(int i=0;i<stu.length;i++) {
stu[i]=in.next();
}
//排序后的数组
Arrays.sort(stu);
return stu;
}
public static void main(String[] args) {
Order ch=new Order();
ch.getSortArray();
System.out.println("\t排序以后");
System.out.println("********************");
System.out.println(Arrays.toString(ch.getSortArray()));
}
}
2.n个人围成一圈,顺序排序,从第一个人开始报数,从1-3开始报数,凡报到三的人退出圈子, 问最后剩下的是原来第几号的那位
package com.jredu.ch09;
import java.util.Arrays;
import java.util.Scanner;
public class Round {
/**
* 2:2
* 3:2
* 4:1
* 5:4
* 7:4
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用数组存储数据
// int[] person={1,2,0,4,5,6,7};
// {1,2,0,4,5,6,7}3
// {1,2,0,4,5,0,7}6
// {1,0,0,4,5,0,7}2
// {1,0,0,4,5,0,0}7
// {1,0,0,4,0,0,0}5
// {0,0,0,4,0,0,0}1
System.out.print("请输入人数");
Scanner sc=new Scanner(System.in);
int count=sc.nextInt();
int[] person=new int[count];
for(int i=0;i<count;i++) {
person[i]=i+1;
}
int num=0;
int length=count;
for(int i=0;i<count;i++) {
// num++;
//如果num是3,
if(person[i]!=0) {
num++;
}
if(num==3) {
//并且当前数字不是0,就把当前数字变成0
if(person[i]!=0) {
// System.out.println(person[i]);
person[i]=0;
num=0;
length--;
// System.out.println(Arrays.toString(person));
// System.out.println(i+1);
} else {
continue;
}
}
if(i==count-1) {
i=-1;
}
if(length==1) {
for(int j:person) {
if(j!=0) {
System.out.println("最后剩下的是"+j);
}
}
return;
}
}
}
}
3.输入一个数字,插入到一个有序数组中,并排序
package com.jredu.ch09;
import java.util.Arrays;
import java.util.Scanner;
public class Sort {
public static void main(String[] args) {
int[] a={2,3,6,7,18};
Scanner in=new Scanner(System.in);
System.out.print("请输入数字");
int num=in.nextInt();
int[] b=Arrays.copyOf(a, a.length+1);
b[a.length]=num;
a=null;
a=b;
b=null;
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
4.创建两个类:以对象作为方法的参数进行传递
学生类:
package com.jredu.ch09;
public class Stu {
public int java;
public int c;
public int oracle;
public Stu(int java, int c, int oracle) {
super();
this.java = java;
this.c = c;
this.oracle = oracle;
}
}
学生服务类:
package com.jredu.ch09;
public class StuService {
public int getAvg(Stu stu) {
return (stu.java+stu.c+stu.oracle)/3;
}
public static void main(String[] args) {
Stu wm=new Stu(80, 80, 80);
Stu xgx=new Stu(85, 85, 85);
StuService service=new StuService();
// int a=10;
int avg=service.getAvg(wm);
int avg1=service.getAvg(xgx);
System.out.println("平均成绩是"+avg);
System.out.println("平均成绩是"+avg1);
}
}
5.
Student类:
package com.jredu.ch10;
/**
* 学生类
* @author Administrator
*
*/
public class Student {
public String name;
public int age;
public double height;
public Student(double height) {
super();
this.height = height;
}
}
StudentManager类
package com.jredu.ch10;
import java.util.Scanner;
public class StudentManager {
/**
* 计算学生平均身高
* @param array
* @return
*/
public double getHeightAvg(Student[] array) {
double sum=0;
for(Student stu:array) {
// stu.height 每个学生的身高
sum+=stu.height;
}
return sum/array.length;
}
public static void main(String[] args) {
//得到所有的学生
Student[] array=new Student[5];
Scanner in=new Scanner(System.in);
for(int i=0;i<array.length;i++) {
System.out.print("请输入第"+(i+1)+"个学生的身高(cm):");
//根据身高,定义学生对象
// Student stu=new Student(in.nextDouble());
array[i]=new Student(in.nextDouble());
}
StudentManager manager=new StudentManager();
double avg=manager.getHeightAvg(array);
System.out.println("***这"+array.length+"名学生的平均身高是:"+avg+"cm***");
}
}
HeightManager类
package com.jredu.ch10;
public class HeightManager {
/**
* 求平均值
* @param array
* @return
*/
public double avg(double[] array) {
int sum=0;
for(int i=0;i<array.length;i++) {
sum+=array[i];
}
return sum/array.length;
}
public double stuAvg(Student[] array) {
Student stu=array[0];
double sum=0;
for(int i=0;i<array.length;i++) {
sum+=array[i].height;
}
return sum/array.length;
}
public static void main(String[] args) {
double [] array={180,190,173,163,158};
// double a=180;
// Student stu=new Student(180);
String str="";
String str2=new String("");
Student [] arr2={new Student(180),new Student(180),
new Student(180),new Student(180),new Student(180)};
//求平均值
HeightManager manager=new HeightManager();
System.out.println("平均值是"+manager.avg(array));
}
}
6. 并且通过会员编号查找会员积分
Vip类
package com.jredu.ch09;
public class Vip {
public int num;
public int score;
public Vip() {
super();
this.num = num;
this.score = score;
}
public int getNum(){
return num;
}
public float getScore(){
return score;
}
}
VipOprate类
package com.jredu.ch09;
import java.util.Scanner;
public class VipOprate {
public void addVip(Vip[] vip) {
System.out.println("*****会员列表*****");
System.out.println("编号\t积分");
for(int i=0;i<vip.length;i++){
System.out.print("\n"+vip[i].num+"\t"+vip[i].score);
}
}
public int searchScore(Vip[] vip,int searchNum){
int newScore=0;
for(int i=0;i<vip.length;i++){
if(searchNum==vip[i].num){
newScore=vip[i].score;
break;
}
}
return newScore;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Vip[] vip =new Vip[5];
VipOprate oprate = new VipOprate();
for(int i=0;i<vip.length;i++){
vip[i]=new Vip();
System.out.print("请输入会员编号:");
vip[i].num = in.nextInt();
System.out.print("请输入会员积分:");
vip[i].score = in.nextInt();
}
oprate.addVip(vip);
System.out.print("请输入要查找的编号:");
int n=in.nextInt();
int newScore=oprate.searchScore(vip, n);
System.out.println("该会员的积分为:"+newScore);
}
}
老师讲的
user类
package com.jredu.ch10;
public class User {
public int no;//会员编号
public int score;//会员积分
public User(int no, int score) {
super();
this.no = no;
this.score = score;
}
}
usermanager类
package com.jredu.ch10;
import java.util.Scanner;
public class UserManager {
public User[] users=new User[100];//会员数组
public int index=0;
/**
* 添加会员
* @param user
*/
public void addUser(User user) {
users[index]=user;
index++;
}
public void show() {
System.out.println("***会员列表***");
System.out.println("编号\t积分");
for(User u:users) {
if(u!=null) {
//打印会员信息
System.out.println(u.no+"\t"+u.score);
} else {
break;
}
}
}
public User search(int no){
//遍历所有的会员信息,查找是否存在该用户
for(User u:users){
if(u.no==no){
//找到了
return u;
}
}
return null;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
UserManager manager=new UserManager();
while(true) {
System.out.print("请输入会员编号");
int no=in.nextInt();
if(no>0) {
System.out.print("请输入会员积分");
int score=in.nextInt();
// User user=new User(no,score);
manager.addUser(new User(no,score));
} else {
break;
}
}
manager.show();
//查找
System.out.print("请输入要查找的会员编号:");
User user=manager.search(in.nextInt());
if(user!=null){
System.out.println("该会员积分是:"+user.score);
}else{
System.out.println("不存在该会员");
}
}
}