package com.phone.week3.day1;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double []prices = new double[5]; //声明一个浮点型的长度为5的数组
int len = prices.length;
for (int i = 0; i <len; i++) {
System.out.print("请输入第"+(i+1)+"笔购物金额:");
prices[i]=in.nextDouble(); //从键盘接收购物金额
}
double sum = 0; //求总金额的
System.out.println("序号\t金额(元)");
for (int i = 0; i < len; i++) {
System.out.println((i+1)+"\t"+prices[i]);
sum+=prices[i];
}
System.out.println("总金额\t"+sum);
}
}
package com.phone.week3.day1;
import java.util.Arrays;
public class Demo4 {
public static void main(String[] args) {
String[] words = { "c", "b", "a", "f", "d", "e" };
// 没排序的时候输出
for (int i = 0; i < words.length; i++) {
System.out.print(words[i] + "\t");
}
Arrays.sort(words); // 升序排
System.out.println();
// 升序输出
for (int i = 0; i < words.length; i++) {
System.out.print(words[i] + "\t");
}
System.out.println();
// 逆序输出
for (int i = words.length - 1; i >= 0; i--) {
System.out.print(words[i] + "\t");
}
}
}
package com.phone.week3.day1;
import java.util.Arrays;
public class Demo10 {
public static void main(String[] args) {
int nums[]={3,2,5,8,7,6,1};
int num[] ={3,2,5,8,7,6,1};
for (int i : nums) {
System.out.print(i+"\t");
}
System.out.println();
//Arrays工具类里特有的方法来排序
Arrays.sort(nums);
Arrays.sort(num);
for (int i : nums) {
System.out.print(i+"\t");
}
System.out.println();
System.out.println(Arrays.equals(nums, num));
//把num数组里值全变成0
Arrays.fill(num, 0);
for (int i : num) {
System.out.print(i+"\t");
}
System.out.println();
int newArr[] = Arrays.copyOf(nums, 5);
for (int i : newArr) {
System.out.print(i+"\t");
}
System.out.println();
System.out.println(Arrays.toString(newArr));
}
//二分法查找
private static void binarySearch(int[] nums) {
//查找7
boubbleSort(nums); //二分法查找一定是在排好序的基础上
int min = 0; //最小的下标
int max = nums.length-1; //最大下标
int mid = (min+max)/2; //找到中间下标
//如果你要找的数不是中间下标所对应的数,就继承找
while(nums[mid]!=5){
//如果中间下标所对应的值小于你要找的值
if(nums[mid]<5){
min = mid+1; //就改变最小下标的值
}
//如果中间下标所对应的值大于你要找的值
else if(nums[mid]>5){
max = mid-1; //就改变最大下标的值
}
mid = (min+max)/2; //继续找中间下标值
}
System.out.println();
System.out.println("5所在的下标位是:"+mid);
}
//插入排序法
private static void insertSort(int[] nums) {
//插入排序
for (int i =1; i < nums.length; i++) {
for (int j = i; j >0; j--) {
if(nums[j-1]>nums[j]){
int temp = nums[j-1];
nums[j-1]=nums[j];
nums[j]=temp;
}
}
}
System.out.println();
for (int i : nums) {
System.out.print(i+"\t");
}
}
//选择排序
private static void change(int[] nums) {
for (int i : nums) {
System.out.print(i+"\t");
}
System.out.println();
//选择排序
for (int i = 0; i < nums.length-1; i++) {
for (int j = i+1; j<nums.length; j++) {
if(nums[i]>nums[j]){
int temp = nums[i];
nums[i] = nums[j];
nums[j]=temp;
}
}
}
for (int i : nums) {
System.out.print(i+"\t");
}
}
/*
* 冒泡排序
*/
private static void boubbleSort(int[] nums) {
for (int i : nums) {
System.out.print(i+"\t");
}
//输出的时候按照升序输出
//N个数字来排队,外层循环n-1,内层循环n-i-1
for (int i = 0; i < nums.length-1; i++) {
for (int j = 0; j < nums.length-i-1; j++) {
if(nums[j]>nums[j+1]){
int temp = nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
//System.out.println();
for (int i : nums) {
System.out.print(i+"\t");
}
}
}
package com.phone.week3.day1;
import java.util.Scanner;
/**
*
* @author Administrator
*
*/
public class Demo1 {
public static void main(String[] args) {
//声明数组的语法
//数据类型 数组名[] = new 数据类型 [6];
//声明数组的数据类型可以是任何数据类型,包括基本数据类型和引用数据类型,一旦分配好了指定类型的数组
//里面只能放同一种数据类型的数据
int score[] = new int[5]; //这里面只能放整型
String names[] = new String[35]; //这里面只能放字符串类型的数据
//声明数组时,这个中括号可以放在变量名的前面也可以放在变量名的后面
double []money = new double[12];
//给数组赋值有两种方式:
//1.静态赋值:声明数组和赋值一步完成
int sc[] = {1,2,4,6};//能过直接赋值的方式决定数组的长度
String nn[] = new String[]{"a","b","c","d"}; //还是根据赋值的方式来决定数组的长度,后面的中括号不能给数组的长度
//2.动态赋值:声明数组和赋值是分开完成的
//我们知道数组一旦分配了,在内存中就会连续的开辟一块空间,并且这些连接的空间都会被加上一个编号,编号从0开始,我们把这个编号称为下标
//我们就可以通过个这下标来给空间赋值或取值
score[1] = 90;
score[0] = 100;
//可以通过循环的方式动态赋值,从键盘接收5个分数,并存放在score这个数组里
Scanner in = new Scanner(System.in);
int sum = 0; //请总和的变量
for (int i = 0; i < 5; i++) {
System.out.println("请输入第"+(i+1)+"个分数:");
score[i] = in.nextInt(); //接收成绩
sum+=score[i]; //进行成绩累加
}
int avg = sum/5;//求平均分
System.out.println("5门课程的平均分是:"+avg);
//想查看这5门课程的成绩
for (int i = 0; i < 5; i++) {
System.out.println(score[i]);
}
}
}
package com.phone.week3.day1;
public class Demo5 {
public static void main(String[] args) {
//装5名学生
Student[] stus = new Student[5]; //声明一个学生类型的数组长度为5
stus[0] = new Student("张三", 30, "深圳", "1509");
stus[1] = new Student("李四", 20, "湖南", "1509");
stus[2] = new Student("王五", 34, "湖北", "1509");
stus[3] = new Student("赵六", 28, "安徽", "1509");
stus[4] = new Student("田七", 26, "北京", "1509");
for (int i = 0; i < stus.length; i++) {
Student stu = stus[i];
System.out.println(stu.getName()+"\t"+stu.getAddress());
}
}
}
package com.phone.week3.day1;
public class Demo8 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
if(args[i]!=null&&!args[i].equals("")){
if(args[i].equals("add")){
System.out.println("增加用户");
}else if(args[i].equals("update")){
System.out.println("修改用户");
}else{
System.out.println("输入错误");
}
}
}
}
}
package com.phone.week3.day1;
public class Demo7 {
public static void main(String[] args) {
String name[] = { "a", "b", "d", "e", "c" };
for (int i = 0; i < name.length; i++) {
System.out.print(name[i] + "\t");
}
System.out.println();
// 增强版的for循环
for (String s : name) {
System.out.print(s + "\t");
}
System.out.println();
// 装5名学生
Student[] stus = new Student[5]; // 声明一个学生类型的数组长度为5
stus[0] = new Student("张三", 30, "深圳", "1509");
stus[1] = new Student("李四", 20, "湖南", "1509");
stus[2] = new Student("王五", 34, "湖北", "1509");
stus[3] = new Student("赵六", 28, "安徽", "1509");
stus[4] = new Student("田七", 26, "北京", "1509");
for(Student s : stus){
System.out.print(s.getName()+"\t"+s.getAge());
System.out.println();
}
for (int i = 0; i < stus.length; i++) {
Student s = stus[i];
System.out.print(s.getName()+"\t"+s.getAge());
System.out.println();
}
int j[] = new int[]{1,2,3,4,5,6};
int ss[]={1,2,3};
test("rose",ss);
}
//可变参数
public static void test(String name,int ... arg){
System.out.println(name);
for (int i = 0; i < arg.length; i++) {
System.out.println(arg[i]);
}
}
}
package com.phone.week3.day1;
public class Student {
private String name;
private int age;
private String address;
private String num;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Student(String name, int age, String address, String num) {
super();
this.name = name;
this.age = age;
this.address = address;
this.num = num;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", address="
+ address + ", num=" + num + "]";
}
}