韩顺平老师的Java练习题 大家可以尝试着做一做
package exer;
public class Demo01 {
public static void main(String[] args) {
double total = 100000d;
int count=0;
while(true){
if (total > 50000) {
total = total*0.95;
count++;
}else if (total<=50000){
total = total-1000;
count++;
} else if (total<1000) {
break;
}
}
System.out.println(count);
}
}
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个整数");
String str = null;
do{
int num = scanner.nextInt();
if (num>0){
System.out.println("大于0");
} else if (num<0) {
System.out.println("小于0");
}else {
System.out.println("等于0");
}
System.out.println("no退出/任意字母继续");
str = scanner.next();
}while(!str.equals("no"));
scanner.close();
}
}}
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if((year%4==0&&year%100==0)||(year%400==0)){
System.out.print("是闰年");
}
scanner.close();
}
}
public class Demo03 {
public static void main(String[] args) {
int num = 153;
int x = num%10/1;//个
int y = num%100/10;//十
int z = num/100;//百
if(num==(x*x*x+y*y*y+z*z*z)){
System.out.println("水仙花数");
}
}
}
//作业6
public class Demo04 {
public static void main(String[] args){
int flag = 0;
for (int i =1;i<=100;i++){
if(i%5!=0){
System.out.print(i+"\t");
flag++;
}
if (flag==5){ //flag%5==0也可以
System.out.println();
flag=0;
}
}
}
}
//作业7
public class Demo05 {
public static void main(String[] args) {
for (char c1 = 'a';c1<='z';c1++){
System.out.print(c1);
}
System.out.println();
for(char c2='Z';c2>='A';c2--){
System.out.print(c2);
}
}
}
//作业8
public class Demo06 {
public static void main(String[] args) {
double first=0;
for(int i =1;i<=100;i++){
if(i%2==0){
first=first-(1.0/i);
}else{
first=first+(1.0/i);
}
}
System.out.print(first);
}
}
//作业9
public class Demo07 {
public static void main(String[] args) {
//1 3 6 10 15 21
int front= 0;
int sum = 0;
int total = 0;
for (int i =1;i<=100;i++){
sum = sum+i;
total = sum+front;
front = total;//将前几个合赋给front
}
System.out.print(total);
}
}
第一段代码是我自己的想法,第二段是老师的,老师的比较简洁,还是大佬牛逼
public class Demo10 {
public static void main(String[] args) {
int[] arrays = {10,12,45,90,100,102};
int[] arrays1 = new int[arrays.length+1];
int num =8;
if (num<arrays[0]){
arrays1[0] = num;
for (int i =0;i<arrays.length;i++){
arrays1[i+1]=arrays[i];
}
} else if (num>arrays[arrays.length-1]) {
arrays1[arrays1.length-1] = num;
for (int i =0;i<arrays.length;i++){
arrays1[i]=arrays[i];
}
}else{
int index=-1;
for (int i=0;i<arrays.length;i++){
if(arrays[i]>=num){//找到定位点
index = i;
arrays1[index] = num;
for (int j=index;j<arrays.length;j++){//定位点后的数赋值
arrays1[j+1] =arrays[j];
}
break;
}else {
arrays1[i]=arrays[i];
}
}
}
printArray(arrays1);
}
public static void printArray(int[] arrays){
for (int i=0;i<arrays.length;i++){
System.out.print(arrays[i]+"\t");
}
}
}
public class Demo11 {
public static void main(String[] args) {
int[] arr ={10,12,45,90};
int insertNum = 23;
int index =-1; //作为定位索引
//找定位点
for (int i=0;i<arr.length;i++){
if (arr[i]>=insertNum){
index=i;
break;//找直接跳出即可
}
}
//判断index的值,-1则位找到,加入末尾即可
if(index==-1){
index=arr.length;
}
//扩容
int[] arrNew = new int[arr.length+1];
for (int i=0,j=0;i<arrNew.length;i++){
if(i!=index){
arrNew[i]=arr[j];
j++;//神来之笔
}else {
arrNew[i]=insertNum;
}
}
printArray(arrNew);
}
public static void printArray(int[] arrays){
for (int i=0;i<arrays.length;i++){
System.out.print(arrays[i]+"\t");
}
}
}