java训练
新手训练,望大家多多指教
练习1

代码
public class demo01 {
public static void main(String[] args) {
String[] strings = new String[]{"Nike背包","Adidas运动衫","李宁运动鞋","Kappa外套","361°腰包"};
for (int i = 0; i < strings.length; i++) {
System.out.println("第"+i+"号索引元素为"+strings[i]);
}
}
}
练习2

代码
package lianxi;
import java.util.Scanner;
public class demo02 {
public static void main(String[] args) {
double moneys[]=new double[6];
double moneySum=0;//总钱数
Scanner sc=new Scanner(System.in);
System.out.println("请输入会员本月的消费记录");
for (int i = 1; i < 6; i++) {
System.out.println("请输入第"+i+"笔购物金额");
double money = sc.nextDouble();
moneys[i-1] = money;
moneySum = money+moneySum;//总钱数
}
//总额放进去
moneys[5] = moneySum;
System.out.println();
System.out.println("序号 金额(元)");
for (int i = 0; i < moneys.length; i++) {
if (i == 5 ){
System.out.println("总金额 "+moneys[5]);
continue;
}
System.out.println(i+1+" "+moneys[i]);
}
}
}
练习3

代码
package lianxi;
import java.util.Arrays;
import java.util.Scanner;
public class demo03 {
public static void main(String[] args) {
int[] sorces = new int[5];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("请输入"+(i+1)+"号的成绩");
int sorce = scanner.nextInt();
sorces[i] = sorce;//赋值
}
Arrays.sort(sorces);
System.out.print("排序后为");
for (int i = 0; i < sorces.length; i++) {
if (i == sorces.length-1){
System.out.println(sorces[i]);
continue;
}
System.out.print(sorces[i]+",");
}
}
}

代码
package lianxi;
import java.nio.charset.Charset;
import java.util.Arrays;
public class demo04 {
public static void main(String[] args) {
char[] chars = new char[]{'a', 'c', 'u', 'b', 'e', 'p', 'f', 'z'};
System.out.print("原字符序列:");
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i] + "");
}
System.out.println();
Arrays.sort(chars);
System.out.print("升序排序后:");
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i] + "");
}
System.out.println();
System.out.print("降序排序后:");
//逆序 创建一个新数组,遍历填充
/* char[] chars1 = new char[chars.length];
for (int i = 0, j = chars1.length - 1; i < chars.length; i++, j--) {
chars1[j] = chars[i];
}
for (int i = 0; i < chars1.length; i++) {
System.out.print(chars1[i] + "");
}
*/
//冒泡排序
char temp;
for (int i = 1; i < chars.length; i++) {//需要排序几趟
for (int j = 0; j< chars.length-i; j++) {//这一趟需要排序几次
if(chars[j]<chars[j+1]){
temp= chars[j];
chars[j] = chars[j+1];
chars[j+1] = temp;
}
}
}
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]+"");
}
//一半颠倒顺序
/* char temp;
for (int i = 0, j = chars.length - 1; i < chars.length / 2; i++, j--) {
temp = chars[j];
chars[j] = chars[i];
chars[i] = temp;