Java基础题型归纳

case 1 AA制
case 2 当前时间

  • 已知:System.currentTimeMillis()返回1970-1-1零点至今的毫秒数,long型
  • 输出系统当前时间,格式为 时:分:秒
public static void main(String[] args) {
		long m = System.currentTimeMillis();
		long totalSecond = m/1000;//所有秒
		long Second = totalSecond % 60;
		long totalMinute = totalSecond /60;//所有分
		long Minute = totalMinute % 60;
		long totalHour = totalMinute /60;//所有小时
		long Hour = totalHour % 24;
		System.out.println("系统当前时间为"+Hour+":"+Minute+":"+Second);
}

case 3 任意范围随机数

  • 已知:Math.random()随机返回 [0,1) 之间的一个double型浮点数
  • Math在lang包下,因此不必显示import
  • 有办法得到任意范围内的随机整数和随机浮点数吗?
public static void main(String[] args) {
	int a = 1;
	int b = 100;
	int m = a+(int)(Math.random()*(b-a+1));	
	System.out.println(m);
}

case 4-5 接收控制台输入
case 4:

  • 已知:圆面积为3.14159*r^2
  • 需求:提示用户输入一个圆的半径(浮点数),计算出圆的面积并输出
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("请输入圆的半径:");
	Scanner in = new Scanner(System.in);
	double r = in.nextDouble();
	double mianJi = Math.PI*r*r;
	System.out.println(mianJi);
}

}

case 5:

  • 需求:提示用户输入两个整数,代表一个范围,随机输出这个范围内的一个整数
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("请输入两个整数作为范围:");
	Scanner in = new Scanner(System.in);
	int a = in.nextInt();
	int b = in.nextInt();
	int m = a + (int)(Math.random()*(b-a+1));
	System.out.println("在("+a+","+b+")这个范围里随机生成一个整数:"+m);
}

}

case 6 整数转16进制表示
case 7 猜生日
case 8 “读出”金额
case 9-10 if-else使用示例
case 11 十六进制
case 12:个税计算器

  • 已知:
    • 工资个税的计算公式为: 应纳税额 = ( 工资薪金所得 - “五险一金”个人负担部分 - 扣除数 )* 适用税率 - 速算扣除数
    • 在这里插入图片描述
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("请按照页面提示输入!\n(起征点为3500)");
	Scanner in = new Scanner(System.in); 
	System.out.println("税前工资:");
	int shuiQian = in.nextInt();
	System.out.println("各项社会保险费:");
	int baoXian = in.nextInt();
	int yingNaShui = shuiQian - 3500;
	double shuiLv = 0;
	int suSuan = 0;
	if ( shuiQian <= 3500) {
		System.out.println("起征点为3500元,您无需交税!");
	}else {
		if( yingNaShui <= 1500 ) {
			shuiLv = 0.03;
			suSuan = 0;
		}
		if( 1500 < yingNaShui && yingNaShui <=4500 ) {
			shuiLv = 0.1;
			suSuan = 105;
		}
		if( 4500 < yingNaShui && yingNaShui <=9000 ) {
			shuiLv = 0.2;
			suSuan = 555;
		}
		if( 9000 < yingNaShui && yingNaShui <=35000 ) {
			shuiLv = 0.25;
			suSuan = 1005;
		}
		if( 35000 < yingNaShui && yingNaShui <=55000 ) {
			shuiLv = 0.3;
			suSuan = 2755;
		}
		if( 55000 < yingNaShui && yingNaShui <=80000 ) {
			shuiLv = 0.35;
			suSuan = 5505;
		}
		if( 80000 < yingNaShui ) {
			shuiLv = 0.45;
			suSuan = 13505;
		}
		double shui = ( yingNaShui - baoXian ) * shuiLv - suSuan;
		double shiFa = shuiQian - shui;
		System.out.println(
				"应纳税所得额:"+yingNaShui+"\n"+
				"适用税率:"+(int)(shuiLv*100)+"%"+"\n"+
				"速算扣除数:"+suSuan+"\n"+
				"应缴税款:"+shui+"\n"+
				"实发工资:"+shiFa+"\n"+
				"谢谢惠顾!"
				);
	}
}

}
  • 还有一个好玩的东西:import java.text.NumberFormat;
  • 可以控制小数点后输出几位
double a = 1.123;
	NumberFormat nFormat = NumberFormat.getNumberInstance();
	nFormat.setMaximumFractionDigits(2);//设置小数点后面是2
	System.out.println(nFormat.format(a));

case 13:简单算术计算器

  • 需求
    • 编写程序,读取用户输入的操作数与符号,计算结果并输出
    • 输入样例:4 + 3
    • 输出样例:4 + 3 = 7
    • 支持 + - * / %
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	double a = in.nextDouble();
	String m = in.next();
	double b = in.nextDouble();
	double r = 0;
	switch(m) {
	case "+":
		r = a + b;
		break;
	case "-":
		r = a - b;
		break;
	case "*":
		r = a * b;
		break;
	case "/":
		r = a / b;
		break;
	case "%":
		r = a % b;
		break;
	}
	System.out.println("%.2f %s %.2f = %.2f",a,m,b,r);
}
}

case 14 while让语句反复执行
case 15 :设计一个简易的聊天程序

import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	while(true) {
		System.out.println("Speak");
		String text = in.nextLine();
		if ( text.equals("Good night")) {
			break;
		}
		switch (text) { 
		case ("u are fat"):
			System.out.println("Go out");
			break;
		case ("u are so beautiful"):
			System.out.println("oh i see");
			break;
		}
	}
	
}
}
  • 有一个好玩的东西:两个字符串比较是否相同,返回一个布尔值
if ( text.equals("Good night")) {
			break;
		}

case 16-17 十进制改十六进制或二进制
case 18 嵌套循环

  • Math.pow(double a, double b)
    • a的b次方

case 19-20 循环示例
case 21 :给我一个随机字符串

  • 已知字符范围是 \u0000-\uFFFF
  • 需求
    • 用户给定一个长度
    • 程序生成该长度的字符串,每个字符都是随机的
    • 输出该字符串
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("请输入一个长度:");
	Scanner in = new Scanner(System.in);
	int number = in.nextInt();
	String result = "";
	for( int i =0; i < number; i++ ) {
		char c = (char)(Math.random()*(0xffff+1));
		if((c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')){
			result+=c;
		}else {
			i--;//废弃当前这次循环的成果
		}
	}
	System.out.println("随机字符串是:"+result);
	
}
}

case 22 : 求圆周率的近似值

  • 已知
  • 在这里插入图片描述
  • 编写程序,显示当 i = 10000,20000,100000时的近似值
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int number = in.nextInt();
	double sum = 0;
	double a = 0;
	for ( int i = 1; i <= number ; i++ ) {
		if ( (i+1) % 2 == 0) {
			sum = 1.0 / ( 2*i - 1 );
			a += sum;
		}else {
			sum = 1.0 / ( 2*i - 1 );
			a -= sum;
		}
	}
	double result = a * 4;
	System.out.println("当i="+number+"时,结果值为:"+result);
}
}
  • 一个非常重要的知识点!
    • 在Java语言中,整数之间的除法运算,不管你的小数点有多少位,小数是几,统统舍弃。只返回整数位数字!
    • 如果我们想要返回完整的运算结果,我们就需要将其中一个的数据类型转换成double类型或者float类型,而不是只改变接收运算结果的数据类型!!!

case 23 : 回文串的判断

  • 如果一个字符串从前往后,以及从后往前是一样的,那么它就是一个回文。例如," mom " 、" dad " ,以及 " noon ",都是回文
  • 编写一个程序,提示用户输入一个字符串,然后给出该字符串是否是回文。
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	String text = in.next();
	int sum = text.length() - 1;
	int a = 0;
	int b =0;
	for ( a=0, b=sum; a<=(sum/2)&&b>(sum/2); a++,b--) {
		if ( text.charAt(a) != text.charAt(b)) {
			System.out.println("no");
			break;
		}
	}
	if ( a - b == 1 || a == b ) {
		System.out.println("yes");
	}
	
}
}
  • 一些好玩的东西:
    • 输出字符串的第几个字符: .charAt(i)
    • char 转换为 String:String s = String.valueOf(‘c’); //效率最高的方法
    • 跳出循环嵌套的方法:标签,内部条件(if判断)
    • 在进行两个字符串获取字符串与数字进行运算的时候,要先将字符串转换为对应的大数字BigDecimal,比如下面的例子:
String str1="111";
String str2="222";
BigDecimal num1 = new BigDecimal(str1);
BigDecimal num2 = new BigDecimal(str2);
然后通过BigDecimal的加减乘除方法,进行运算:
加法:
BigDecimal result = num1.add(num2);
减法:
result = num1.subtract(num2);
除法:
result = num1.divide(num2);
乘法:
result = num1.multiply(num2);

case 24: 九九乘法表

public class CaoGao {

public static void main(String[] args) {
	for ( int i =1; i <=9; i++ ) {
		for ( int j = 1; j <= i; j++ ) {
			System.out.print(j+"*"+i+"="+(i*j)+" ");
		}
		System.out.println("\t");
	}
}
}
  • 转义字符:
    • \b 退格符
    • \t 水平制表符
    • \n 换行符
    • \f 换页符
    • \r 回车符

case 25 :最大公约数的几种解法

  • 两个整数 4 和 2 的最大公约数是 2
  • 两个整数 16 和 24 的最大公约数是 8
import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("输入两个整数:");
	Scanner in = new Scanner(System.in);
	int m = in.nextInt();
	int n = in.nextInt();
	int r = 0;
	do {
		r = m % n;
		m = n;
		n = r;
		if ( n == 0 ) {
			System.out.println("最大公约数为:"+m);
			break;
		}
	}while( n != 0 );
	
}
}

case 26 改写小写金额变成大写金额
case 27 封装关于十六进制的工具类
case 28 封装一个关于随机串的工具类

数组常见用法举例:

1、求数组平均数

import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("请输入三个数值:\n"+"它们分别是长度,最小值和最大值。");
	Scanner in = new Scanner(System.in);
	int length = in.nextInt();
	int min = in.nextInt();
	int max = in.nextInt();
	int total = 0;
	int [] myList = new int[length];
	for ( int i = 0; i < myList.length; i++) {
		myList[i] = (int)(Math.random()*(max-min+1)+min);
		System.out.print(myList[i]+" ");
		total += myList[i];
	}
	double average = total / myList.length;
	System.out.println("\n"+average);
	
}
} 

2、最大元素及其下标

import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	System.out.println("请输入三个数值:\n"+"它们分别是长度,最小值和最大值。");
	Scanner in = new Scanner(System.in);
	int length = in.nextInt();
	int min = in.nextInt();
	int max = in.nextInt();
	int [] myList = new int[length];
	for ( int i = 0; i < myList.length; i++) {
		myList[i] = (int)(Math.random()*(max-min+1)+min);
		System.out.print(myList[i]+" ");
	}
	//最大值
	//假定0号是最大的
	int theMax = myList[0];
	int xiaBiao = 0;
	//假定0号最大后,在和后面依次比较
	for ( int i = 1; i < myList.length; ) {
		if ( myList[xiaBiao] > myList[i] ) {
			i++;
		}else {
			theMax = myList[i];
			xiaBiao = i;
			i++;		
		}
	}
	System.out.println("\n"+"元素中最大值是:"+theMax+",元素下标为:"+xiaBiao);
	
}
} 

3、随机打乱数组

import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	//设定数组
	System.out.println("请输入三个数值:\n"+"它们分别是长度,最小值和最大值。");
	Scanner in = new Scanner(System.in);
	int length = in.nextInt();
	int min = in.nextInt();
	int max = in.nextInt();
	int [] myList = new int[length];
	for ( int i = 0; i < myList.length; i++) {
		myList[i] = (int)(Math.random()*(max-min+1)+min);
		System.out.print(myList[i]+" ");
	}
	
	//随机打乱数组
	System.out.println("\n");//窜开行
	for ( int i = 0; i <= length -1; i++ ) {
		int a = (int)(Math.random()*(length-1-i-1)+i+1);
		int x = myList[i];
		int y = myList[a];
		myList[i] = y;
		myList[a] = x;
		System.out.print(myList[i]+" ");
	}
	
	
}
} 

4、向前移动数组

import java.util.Scanner;

public class CaoGao {

public static void main(String[] args) {
	//设定数组
	System.out.println("请输入三个数值:\n"+"它们分别是长度,最小值和最大值。");
	Scanner in = new Scanner(System.in);
	int length = in.nextInt();
	int min = in.nextInt();
	int max = in.nextInt();
	int [] myList = new int[length];
	System.out.print("数组为:");
	for ( int i = 0; i < myList.length; i++) {
		myList[i] = (int)(Math.random()*(max-min+1)+min);
		System.out.print(myList[i]+" ");
	}
	
	//向前依次移动
	System.out.print("\r向前移动简便算法:");
	int temp = myList[0];
	for ( int i = 1; i < myList.length; i++) {
		myList[i-1] = myList[i];
	}
	myList[myList.length-1] = temp;
	for ( int e : myList ) {
		System.out.print(e+" ");
	}
}
} 

好东西:foreach

  • 读作“对myList中每个元素e进行以下操作”
  • 当需要以其他顺序遍历数组或改变数组中的元素时,还是必须使用下标变量
for ( double e : myList ){
	System.out.println(e);
}

arraycopy

  • arraycopy( 原数组, 原数组位置, 目标数组, 目标数组位置, 长度 )
  • arraycopy方法不会给目标数组分配内存空间。复制前必须创建目标数组以及给它分配内存空间。复制完成后,原数组和目标数组具有相同的内容,但占有独立的生存空间。

if…if 和 else if 的区别

  • if无论是否满足条件都会向下执行,知道程序结束,else if 满足一个条件就会停止执行。

冒泡排序

public static void main(String[] args) {
	//升序
	int [] list = {2,7,3,5,8,4,6,1};
	int length  = 8;
	for ( int j = 0; j <length; j++) {
		for ( int i = 0; i < length-1; i++) {
			if ( list[i] > list[i+1] ) {
				int temp = list[i];
				list[i] = list[i+1];
				list[i+1] = temp;
			}
		}
	}
	for (int i : list) {
		System.out.print(i+" ");
	}
}

二分法

//二分查找
	int number = (int)(Math.random()*(10-1+1)+1);
	System.out.println(number);
	int list[] = {1,3,4,5,6,7,8,9};
	int length = 8;
	int begin = list[0];
	int end = list[7];
	int mid = (begin+end) / 2;
	for (;;) {
		if ( number < list[mid] ) {
			end = mid;
			mid = (begin+end) / 2;
		}
		if ( number > list[mid] ) {
			begin = mid;
			mid = (begin+end) / 2;
		}
		if ( begin == mid || end == mid ) {
			System.out.println("这个数列中没有您要找的数字");
			break;
		}
		if ( number == list[mid] ) {
			System.out.println("这个数字的所在下标为:"+mid);
			break;
		}
	}

选择排序

//选择排序
	int [] list = {5,7,1,3,4,8,6,9};
	int length = 8;
	for ( int j = length-1; j > 0; ) {
		for ( int i = 0; i < j; i++ ) {
			if ( list[i] > list[i+1] ) {
				
			}
			if ( list[i] < list[i+1] ) {
				int temp = list[i];
				list[i] = list[i+1];
				list[i+1] = temp;
			}
		}
		j--;
	}
	for (int i : list) {
		System.out.print(i+" ");
	}

插入排序

//插入排序
	int [] list  = new int [5];
	int length = 5;
	list[0] = 2;
	list[1] = 4;	
	list[2] = 5;	
	list[3] = 10;
	int number = 7;
	System.out.println(number);
	for ( int i = 0; i < length-1; i++ ) {
		if ( number > list[i] && number < list[i+1]) {
			for ( int j = length-1; j > i+1; j--) {
				list[j] = list[j-1];
			}
			list[i+1] = number;
			break;
		}
	}
	for (int i : list) {
		System.out.print(i+" ");
	}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值