上一篇文章:Java语法疯狂魔鬼练(二)
字符练
'\u十六进制的Unicode值'
转义任意Unicode字符
public class Example {
public static void main(String args[]){
char ChinaCharacter = '好', JapanCharacter = 'よ';
char you = '\u4F60';
int position = 20320;
System.out.println("汉字:"+ChinaCharacter+"的位置:"+(int)ChinaCharacter);
System.out.println("日文:"+JapanCharacter+"的位置:"+(int)JapanCharacter);
System.out.println(position+"位置上的字符是:"+(char)position);
position = 21319;
System.out.println(position+"位置上的字符是:"+(char)position);
System.out.println("you:"+you);
}
}
数据类型转换
public class Example {
public static void main(String args[]){
byte b = 22;
int n = 129;
float f = 123456.6789f;
double d = 123456789.123456789;
System.out.println("b = "+b);
System.out.println("n = "+n);
System.out.println("f = "+f);
System.out.println("d = "+d);
b = (byte)n; //导致精度损失
f = (float)d; //导致精度损失
System.out.println("b = "+b);
System.out.println("f = "+f);
}
}
byte类型存储范围-128~127,129转换byte类型,127+1=-128,-128+1=-127
输入与格式化输出
import java.util.Scanner;
public class Example {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("请输入一个float型数据:");
float f;
f = input.nextFloat();
System.out.printf("格式化输出浮点数用%%f进行占位,最多输出到小数点后6位,%f\n",f);
System.out.println("请输入int型数据:");
int i = input.nextInt();
System.out.printf("格式化输出整型数据用%%d进行占位,%d\n",i);
System.out.println("请输入double型数据:");
double d = input.nextDouble();
System.out.printf("%-+30.10f",d); //占30个长度,输出小数点后10位,靠左输出,且输出符号,末尾没加换行,可以看到长度
System.out.println("请输入double型数据:");
double d2 = input.nextDouble(); //占30个长度,输出小数点后10位,靠右输出,且输出符号
System.out.printf("%+30.10f",d2);
}
}
数组
public class Example {
public static void main(String args[]){
int a[] = {1,2,3,4};
int b[] = {100,200,300};
System.out.println("数组a的元素个数="+a.length);
System.out.println("数组b的元素个数="+b.length);
System.out.println("数组a的引用="+a);
System.out.println("数组b的引用="+b);
a = b;
System.out.println("数组a的元素个数="+a.length);
System.out.println("数组b的元素个数="+b.length);
System.out.println("a[0]="+a[0]+",a[1]="+a[1]+",a[2]="+a[2]);
System.out.print("b[0]="+b[0]+",b[1]="+b[1]+",b[2]="+b[2]);
}
}
这里的引用相当于C语言中的指针或内存地址
选择法排序与折半法查找
折半法查找的前提是数组必须为有序数组,从小到大排列或从大到小排列
import java.util.Scanner;
class Example {
public static void main(String args[]){
int start = 0, end, middle;
int a[] = {3, 7,-1,-9,89,34,77,65,-12,23,17};
int N = a.length;
for(int i = 0; i < N; i++){
for(int j = i + 1; j < N; j++){
if(a[j] < a[i]){
int t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
Scanner input = new Scanner(System.in);
System.out.println("输入整数,程序判断该整数是否在数组中:");
int num = input.nextInt();
int count = 0;
end = N;
middle = N/2;
while(num != a[middle]){
if(num > a[middle])
start = middle;
else if(num < a[middle])
end = middle;
middle = (start + end)/2;
count++;
if(count > N/2)
break;
}
if(count > N/2)
System.out.printf("%d不在数组中。\n",num);
else
System.out.printf("%d在数组中,下标为%d。\n",num, middle);
for(int i = 0; i < N; i++)
System.out.print(a[i]+" ");
}
}
输入一个数,判断是否为素数
以下代码仅针对输入的数为正数
import java.util.Scanner;
public class Example {
public static void main(String args[]){
Scanner IN = new Scanner(System.in);
int num = IN.nextInt();
int c = 0;
for(int i = 1; i <= num/2; i++)
if(num%i == 0)
c++;
if(c == 1)
System.out.println(num+"是素数");
else
System.out.println(num+"不是素数");
}
}
以下代码针对任意整数
import java.util.Scanner;
public class Example {
public static void main(String args[]){
Scanner IN = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num = IN.nextInt();
int i;
if(num >=2){
for(i = 2; i <= num/2; i++)
if(num % i == 0)
break;
if(i > num / 2)
System.out.println(num+"是素数");
else
System.out.println(num+"不是素数");
}
else if(num <= -2){
for(i = 2; i <= num/-2; i++)
if(num % i == 0)
break;
if(i > num / -2)
System.out.println(num+"是素数");
else
System.out.println(num+"不是素数");
}
else
System.out.println(num+"即不是素数也不是合数");
}
}
求100以内的素数
public class Example {
public static void main(String args[]){
int i, j, c = 0;
for(i = 2; i <= 100; i++){
for(j = 2; j <= i/2; j++)
if(i%j==0)
break;
if(j>i/2){
System.out.print(i+"\t");
c++;
if(c == 5){
System.out.println();
c = 0;
}
}
}
}
}
输入一组数,冒泡排序找中位数,求平均数和方差
import java.util.Scanner;
public class Example {
public static void main(String args[]){
Scanner IN = new Scanner(System.in);
System.out.print("请输入数据的个数: ");
int N = IN.nextInt();
int flag;
flag = (N % 2 == 0 ? 1 : 0); //判断输入数据的个数为奇数个还是偶数个
//偶数个flag值为1,奇数个flag值为0
System.out.printf("请输入%d个数据:\n",N);
double a[] = new double[N];
for(int i = 0; i < N; i++)
a[i] = IN.nextDouble();
//冒泡排序
double t = 0;
for(int i = 1; i <= N - 1; i++){
for(int j = 0; j < N - i; j++){
if(a[j] > a[j+1]){
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
//找中位数
System.out.print("中位数为: ");
if(flag == 0) //数据为奇数个
System.out.println(a[N/2]);
else //数据为偶数个
System.out.println(((a[N/2]-1)+a[N/2])/2);
//平均数
double average;
double sum = 0;
for(int i = 0; i < N; i++)
sum += a[i];
average = sum / N;
System.out.println("平均数为: "+average);
double variance = 0;
sum = 0;
for(int i = 0; i < N; i++)
sum += (average - a[i])*(average - a[i]);
variance = sum / N;
System.out.println("方差为: "+variance);
}
}
选择排序和冒泡排序比较
选择排序核心代码:
for(int i = 0; i < N - 1; i++){
//外层循环N-1次,最后一次直接是两个值的交换
for(int j = i + 1; j < N; j++){ //内层循环每次从第i项后面开始
if(a[j] < a[i]){
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
选择排序核心思想:内层循环每次从后面选出一个最小的数放在前面 (用交换值来实现,t 为中间变量)
冒泡排序核心代码:
for(int i = 1; i <= N - 1; i++){
//外层循环N-1次即可,因为最后一次实现交换两个值
for(int j = 0; j < N - i; j++){
//内层循环每次从第0项开始,循环到N-i项且取不到第N-i项
if(a[j] > a[j+1]){ //如果前面的一项大于后面的一项就交换值
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}//内层循环每循环一次就把一个最大项沉到数组末尾
}//外层循环结束时即可排序完毕
冒泡排序核心思想:内层循环每次把一个最大的数推到数组最后面 (多次交换值来实现)