package FirstBook;
import java.util.Scanner;
public class a {
public static void main(String[] args) {
//一串已经排好序的数组(没排序先排序)
int scores[] = new int[]{99,85,82,63,60,0};
//保存新录入的成绩位置
int index =scores.length;
System.out.println("请输入插入的成绩:");
Scanner in = new Scanner(System.in);
int input = in.nextInt();
//找位置
for (int i = 0; i < scores.length; i++) {
if(input>scores[i]){
index=i;
break;
}
}
//错位
for (int j =scores.length-1 ; j > index; j--) {
scores[j] = scores[j-1];
}
//插入
scores[index]=input;
//显示
for (int k = 0; k < scores.length; k++) {
System.out.print(scores[k]+" ");
}
}
}
歌曲排序
package FirstBook;
import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//插入前的数组(排好序 留空位)
String music[] = new String[]{"Island","Ocean","Pretty","Sun",""};
int index = 0 ;
System.out.print("插入前的顺序:");
for (int i = 0; i < music.length; i++) {
System.out.print(music[i]+" ");
}
//插入的歌曲
System.out.print("\n请输入新插入的歌名:");
String newMusic = in.next();
//找到插入位置
for (int i = 0; i < music.length; i++) {
if (music[i].compareTo(newMusic)>0) {
index = i;
break;
}
}
//错位
for (int i = music.length-1; i > index; i--) {
music[i] = music[i-1];
}
//赋值
music[index] = newMusic;
//插入后的顺序
System.out.print("插入后的顺序:");
for (int i = 0; i < music.length; i++) {
System.out.print(music[i]+" ");
}
}
}
阶乘
package FirstBook;
import java.util.Scanner;
public class c{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入数据1~100");
long a = in.nextLong(),total = 1;//累乘total
int i = 0 ;
if (a<=100) {
System.out.print(a+"!="); //等号左边的
for (i = 1; i < a; i++) {
total *= i; //进行累乘
System.out.print(i+"x"); //等号中间部分
}
System.out.print(i+"="+total*i);//补充最后一个数,因为i为a时,无法累乘a,所以最后要补一个
}else {
System.out.println("无效数据");
}
in.close();
}
}
每四个换行
package FirstBook;
public class 每四个换行 {
public static void main(String[] args) {
System.out.println("100内不可以被7整除的有");
int total = 0; //累计总和
for (int i = 0 , a = 1 ; i <= 100; i++ , a++) {
if (!(i%7 == 0)) {
total += i;
System.out.print(i+"\t");
}else{
a--;
}
/*
*判断是不是7的倍数
*不是的话累加起来,并每一行显示4个
*是的话,就给分行判断a的值减去1
*/
if (a==4) {
System.out.println();
a = 0;
//当a=4时,就分行。
}
}
System.out.println("\n100内不可以被7整除的和为:"+total);
}
}
敲出来个菱形
package FirstBook;
import java.util.Scanner;
public class 敲出来个菱形 {
//思路 四个三角形 空格倒三角 *正三角 空格正三角 *倒三角
public static void main(String[] args) {
System.out.print("请输入行数:");
Scanner in = new Scanner(System.in);
int line = in.nextInt();
int save = line; //这条用来保存行数
/*
* 上面的大三角形
*/
for (int i = 0; i < line; i++) {
for (int j = save-1; j > 0; j--) {
System.out.print(" ");
}
save--;
for (int j = 0; j < 2*i+1; j++) {
System.out.print("*");
}
System.out.print("\n");
}
/*
* 下面的小三角形
*/
save = 1;
for (int i = 0; i < line-1; i++) {
for (int j = 0; j < save; j++) {
System.out.print(" ");
}
save++;
for (int j = 2*(line-1)-1; j >= 2*i+1 ; j--) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
敲出来个菱形2
package FirstBook;
import java.util.Scanner;
public class 敲出来个菱形2 {
//思路 四个三角形 空格倒三角 *正三角 空格正三角 *倒三角
public static void main(String[] args) {
System.out.print("请输入行数:");
Scanner in = new Scanner(System.in);
int rows = in.nextInt();
while (rows%2==0) {
System.out.println("请输入奇数:");
rows = in.nextInt();
}
int n = (rows+1)/2; //这条用来打印上半部分
/*
* 上面的大三角形
*/
for (int i = 1; i <= n; i++) {
for (int j = 1; j <=n-i ; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2*i-1; k++) {
System.out.print("*");
}
System.out.print("\n");
}
/*
* 下面的小三角形
* 画出来就会打了
*/
n = (rows-1)/2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 2*n-1; j >= 2*i-1 ; j--) {
System.out.print("*");
}
System.out.print("\n");
}
}
}