(一)基础编程题01
* 编程实现:根据以下函数关系,对输入的X值计算输出对应的y值。
*
* x的值 对应y的值
* x<0 0
* 0<=x<10 x
* 10<=x<20 0.5*x+18
* x>=20 100
public class Test01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入X值:");
double x = scanner.nextDouble();
double y = 0 ;
//判断
if (x < 0){
y = 0;
}else if ( 0 <= x && x <10 ){
y = x;
}else if (10 <= x && x < 20){
y = 0.5 * x +18;
}else if ( x >= 20){
y = 100;
}
System.out.println("y对应的值为:" + y);
scanner.close();
}
}
* 打印九九乘法表
public class Test02 {
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 + "=" + j * i + " ");
}
System.out.println("");
}
}
}
* 编写程序计算1!+2!+3!+…+n!,并输出计算结果
public class Test03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个整数:");
if (scanner.hasNextInt()){
//判断是否是整数
int x = scanner.nextInt();
System.out.println("您输入的整数为:" + x);
int sum = 0;
for (int i = 1; i <= x ; i++) {
int n =1;
for (int j = 1; j <= i ; j++) {
n *= j;
}
sum += n;
}
System.out.println(x + "!" + "=" + sum);
}else {
System.out.println("请输入有效值");
}
scanner.close();
}
}
* 编写三个方法,分别得出一个数组的最大值,最小值,平均值。
public class Test04 {
public static void main(String[] args) {
int[] array = {
7, 2, 8, 3, 10};
int max = numMax(array);
int min = numMin(array);
double avg = numAvg(array);
System.out.println("最大值:" + max + " " + "最小值:" + min + " "+ "平均值:" + avg);
}
public static int numMax(int[] array){
int max = array[1];
for (int i = 0; i < array.length ; i++) {
if (max < array[i]){
max = array[i];
}
}
return max;
}
public static int numMin(int[] array){
int min = array[1];
for (int i = 0; i < array.length ; i++) {
if (min > array[i]){
min = array[i];
}
}
return min;
}
public static double numAvg(int[] array){
double avg;
double sum =0;
for (int i = 0; i < array.length ; i++) {
sum += array[i];
}
avg = sum / array.length;
return avg;
}
}
* 接收用户输入的5门功课并且,计算平均分。
*
* 给用户评级60-80良,81-90好,91-100优秀。
public class Test05 {
public static void main(String[] args) {
System.out.println("请输入5门功课");
Scanner sc = new Scanner(System.in);
double arr[] = new double[5];
double avg;
double sum = 0;
if (sc.hasNextDouble()){
//判断是否为数值
//输入成绩
for (int i = 0; i < 5; i++) {
System.out.print("第" + (i + 1) + "门功课:");
arr[i] = sc.nextDouble();
sum += arr[i];
}
avg = sum / arr.length;
String rank = (91 <= avg ? "优秀" : (81 <= avg ? "好" : (60 <= avg ? "良" : "")));
System.out.println("五门科目的平均分是:" + avg + " 评级为:" + rank);
sc.close();
}else {
System.out.println("请输入0-100之内的数值");
}
}
}
(二)基础编程题02
* 从键盘输入一行字符串(以换行符结束),要求分别统计里面英文字符的
* 总个数和数字的总个数,并分别输出.
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] arr = sc.nextLine().toCharArray();
int sum, word = 0;
for (int i = 0; i < arr.length ; i++) {
int x = arr[i];
if (x >= a & x >= z| x >= A & x>= Z ){
word++;
}else if (x >= 0 | x >= 9){
sum++;
}
}
System.out.println("英文个数为:" + word);
System.out.println("数字个数为:" + sum);
sc.close();
}
* 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
* 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,
* 问每个月的兔子对数为多少?(输出第10个月的兔子对数)
public class Test02 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
int num = getNum(i);
System.out.println("第" + i + "个月后,兔子对数为:" + num);
}
}
public static int getNum(int i){
if (i == 1 || i == 2){
return 1;
}else {
return getNum(i - 1) + getNum(i - 2);
}
}
}
* 编写万用表程序
* 1 电压挡 2 电流挡 3 电阻档 4 其他档位 5 退出
public class Test03 {
public static void main(String[] args) {
System.out.println("欢迎使用电能表");
System.out.println("1 电压挡 2 电流挡 3 电阻档 4 其他档位 5 退出");
System.out.println("请输入你想使用的挡位:");
Scanner sc = new Scanner(System.in);
String input = sc.next();
//判断选择
while (!input.equals("1") && !input.equals("2") && !input.equals("3") &&
!input.equals("3") && !input.equals("4") && !input.equals("5")){
System.out.println("请选择有效选项");
input = sc.next();
}
//挡位选择
switch (input){
case "1":
System.out.println("电压挡");
break;
case "2":
System.out.println("电流挡");
break;
case "3":
System.out.println("电阻档");
break;
case "4":
System.out.println("其他档位");
break;
case "5":
System.out.println("退出");
break;
}
sc.close();
}
}
public class Test04 {
* 判断101-200之间有多少个素数,并输出所有素数
public static void main(String[] args) {
int count = 0;
for (int i = 101; i <= 200 ; i++) {
boolean b = true;
for (int j = 2; j < i ; j++) {
if (i % j == 0){
b = false;
break;
}else {
b = true;
}
}
if (b){
System.out.println(i);
count++;
}
}
System.out.println("个数为:" + count);
}
}
* 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,
* 其各位数字立方和等于该数本身。
* a = i / 100; //取百位的那个数
* b = i/10%10; //取十位的那个数
* c = i % 10; //取个位的那个数
public class Test05 {
public static void main(String[] args) {
int a, b, c;
for (int i = 100; i <= 999; i++){
a = i / 100;
b = i / 10 % 10;
c = i % 10;
if (i == a * a * a + b * b * b + c * c * c){
System.out.print(i + " ");
}
}
}
}
(三)基础编程题03
* 利用条件运算符的嵌套来完成此题:
* 学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示
public class Test01 {
public static void main(String[] args) {
String a = "A" , b = "B" , c = "C";
System.out.println("请输入成绩: ");
Scanner sc = new Scanner(System.in);
double num = sc.nextInt();
if (num > 100 || num < 0){
System.out.println("成绩无效");
}else {
System.out.println("成绩为: " + ((num >= 60) ? ((num >= 90) ? a : b) : c));
}
sc.close();
}
}
* 求s=a+aa+aaa+aaaa+aa…a的值,
* 其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
public class Test02 {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
Scanner c = new Scanner(System.in);
if (a.hasNextInt() && c.hasNextInt()){
int b = a.nextInt();
int d = c.nextInt();
System.out.println(num(b, d) + add(b, d));
}else {
System.out.println("请输入数字");
}
a.close();
c.close();
}
public static String num(int b, int d){
StringBuffer num1 = new StringBuffer();
StringBuffer num2 = new StringBuffer();
for (int i = 1; i <= d ; i++) {
num1 = num1.append(b);
num2 = num2.append(num1);
if (i < d){
num2.append("+");
}
}
num2.append("=");
return num2.toString();
}
public static long add(int b, int d){
long sum1 = 0;
long sum2 = 0;
for (int i = 1; i <= d ; i++) {
sum1 = sum1 * 10 + b;
sum2 += sum1;
}
return sum2;
}
}
* 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?并分别打印出这些数
public class Test03 {
public static void main(String[] args) {
int i, j, k ;
int sum = 0;
int count = 0;
for ( i = 1; i <= 4; i++) {
for (j = 1; j <= 4; j++){
if (i == j){
continue;
}
for (k = 1; k <=4; k++){
if (j == k || i == k){
continue;
}
int n = i * 100;
int m = j * 10;
sum = n + m + k;
System.out.print(sum + " ");
if ((++count) % 10 == 0){
System.out.println("
");
}
}
}
}
System.out.println("能组成" + count + "个");
}
}
* 从键盘上输入2019年的”month"和“day”, 要求通过程序输出输入的日期为2019的第几天。
public class Test04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份:");
int month = sc.nextInt();
System.out.println("请输入日");
int day = sc.nextInt();
int sum = 0;
switch (month){
case 12:
sum += 31;
case 11:
sum += 28;
case 10:
sum += 31;
case 9:
sum += 30;
case 8:
sum += 31;
case 7:
sum += 30;
case 6:
sum += 31;
case 5:
sum += 31;
case 4:
sum += 30;
case 3:
sum += 31;
case 2:
sum += 30;
case 1:
sum += day;
}
System.out.println("2019年" +month + "月" + day + "日" );
System.out.println("2019的第" + sum + "天" );
sc.close();
}
}
* 对同学成绩大于60分的 ,输出”合格“。低于60分的,输出”不及格“
public class Test05 {
public static void main(String[] args) {
System.out.print("请输入学生成绩:");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if (input >= 0 && input < 100){
switch (input /60){
case 0:
System.out.print("不及格");
break;
case 1:
System.out.print("及格");
break;
}
}else {
System.out.println("请输入正确数值");
}
}
}
(四)基础编程题04
* 使用switch把小写类型的char型转为大写。只转换 a,b,c,d,e,其它的输出“other
public class Test01{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
char c = input.charAt(0);
switch (c){
case a:
System.out.println("A");
break;
case b:
System.out.println("B");
break;
case c:
System.out.println("C");
break;
case d:
System.out.println("D");
break;
case e:
System.out.println("E");
break;
default:
System.out.println("outher");
}
sc.close();
}
}
* 根据用于指定月份,打印该月份所属的季节
* 3、4、5 春季 6、7、8 夏季 9、10、11 秋季 12、1、2 冬季
public class Test02{
public static void main(String[] args) {
System.out.println("请输入你要查询月份");
Scanner sc = new Scanner(System.in);
int sum = sc.nextInt();
switch (sum){
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
}
}
}
* 从键盘分别输入年、月、日,判断这一天是当年的第几天
* 注:判断一年是否是闰年的标准:
* 1、可以被4整除,但不可被100整除
* 2、可以被100整除
public class Test03{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入年份:");
int year = scanner.nextInt();
System.out.println("输入月份:");
int mouth = scanner.nextInt();
System.out.println("输入日期");
int day = scanner.nextInt();
int sum = 0;
switch (mouth){
case 1:
sum += day;
case 2:
if ((year / 4 == 0 && year / 100 !=0) || year / 100 == 0){
sum += 28;
}else {
sum +=29;
}
case 3:
sum += 31;
case 4:
sum += 30;
case 5:
sum += 31;
case 6:
sum += 30;
case 7:
sum += 31;
case 8:
sum += 31;
case 9:
sum += 30;
case 10:
sum += 31;
case 11:
sum += 30;
case 12:
sum += 31;
}
System.out.println(year + "年" + mouth + "月" + day + "日");
System.out.println("这一天是当年的第" + sum + "天");
scanner.close();
}
}
* 我家的狗5岁了,5岁的狗相当于人类多大呢?
* 其实,狗的前两年每一年相当于人类的10.5岁,之后每增加一年就增加四岁,
* 那么5岁的狗相当于人类多少年龄呢? 应该是:10.5+10.5+4+4+4=33岁。
*通过程序显示其相当于人类的年龄。如果用户输入负数,请显示一个提示信息。
public class Test04 {
public static void main(String[] args) {
double dog = 5;
if ( dog <= 2 && dog > 0){
dog *= 10.5;
System.out.println("相当于人类的年龄:" + dog);
}else if (dog > 2){
dog = 10.5 * 2 + (dog - 2) * 4;
System.out.println("相当于人类的年龄:" + dog);
}else {
System.out.println("请输入有效年龄");
}
}
}
*假设你想开发一个玩彩票的游戏,程序随机地产生一个两位数的彩票,
* 提示用户输入一个两位数,然后按照下面的规则判定用户是否能赢。
* 1)如果用户输入的数匹配彩票的实际顺序,奖金10 000美元。
* 2)如果用户输入的所有数字匹配彩票的所有数字,但顺序不一致,奖金3000美元。
* 3)如果用户输入的一个数字仅满足顺序情况下匹配彩票的一个数字,奖金1 000美元。
* 4)如果用户输入的一个数字仅满足非顺序情况下匹配彩票的一个数字,奖金500美元。
* 5)如果用户输入的数字没有匹配任何一个数字,则彩票作废。
public class Test05 {
public static void main(String[] args) {
Random random = new Random();
int num = random.nextInt(90)+10;
System.out.println(num);
System.out.println("请输入你要选择的两个数字");
Scanner scanner = new Scanner(System.in);
int numPaly = scanner.nextInt();
if (numPaly == num ){
System.out.println("奖金10 000美元");
}else if (numPaly % 10 == num /10 && numPaly /10 == num % 10){
System.out.println("奖金3000美元");
}else if (numPaly % 10 == num % 10 || numPaly / 10 == num / 10){
System.out.println("奖金1 000美元");
}else if (numPaly % 10 == num /10 || numPaly /10 == num % 10){
System.out.println("奖金500美元");
}else {
System.out.println("彩票作废");
}
scanner.close();
}
}
(五)基础编程题05
* 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
public class Test01{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int max = (m > n) ? m : n;
for (int i = max; i >= 2; i--) {
if (m % i == 0 && n % i == 0){
System.out.println(i);
break;
}
int min = (m > n) ? m : n;
for (int j = min; j < m * n ; j++) {
if (j % m == 0 && j % n == 0){
System.out.println(j);
break;
}
}
}
}
}
* 打印1-100之间所有奇数和
public class Test02{
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0){
sum += i;
}
}
System.out.println(sum);
}
}
* 打印1~100之间所有是7的倍数的整数的个数及总和(体会设置计数黑的思想)
public class Test03{
public static void main(String[] args) {
int sum = 0;
int count = 0;
for (int i = 1; i <= 100 ; i++) {
if (i % 7 != 0){
sum += i;
count++;
System.out.println(i);
}
}
System.out.println(count + " " + sum);
}
}
* 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,
* 输入为O时结束程序。|
public class Test04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int just = 0;
int load = 0;
while (true){
int input = sc.nextInt();
if (input > 0){
just++;
}else if (input < 0){
load++;
}else {
break;
}
}
System.out.println(just + " " + load);
sc.close();
}
}
* 100以内的所有质数的输出。
* 质数:素数,只能被1和它本身整除的自然数。-->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
public class Test05 {
public static void main(String[] args) {
boolean is =true;
for (int i = 2; i <= 100 ; i++) {
for (int j = 2; j <= Math.sqrt(i) ; j++) {
if (i % 2 == 0){
is = false;
break;
}
}
if (is == true){
System.out.println(i);
}
is =true;
}
}
}
(六)基础编程题06
* 3.一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3。
* 编程找出1000以内的所有完数。(因子:除去这个数本身的其它约数)
public class Test01{
public static void main(String[] args) {
for (int i = 2; i <= 1000 ; i++) {
int sum = 0;
for (int j = 1; j < Math.sqrt(i) ; j++) {
if (i % j == 0){
sum += j;
}
}
if (i == sum){
System.out.println(i);
}
}
}
}
* 2.从键盘读入学生成绩,找出最高分,并输出学生成绩等级。
* 成绩>=最高分-10 等级为A
* 成绩>=最高分-20等级为B
* 成绩>=最高分-30等级为c
* 其余等级为D’
* 提示:.先读入学生人数,根据人数创建int委组,存放学生成绩。
public class Test02{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生人数:");
int input = sc.nextInt();
int[] sum = new int[input];
System.out.println("请输入学生成绩:");
for (int i = 0; i < sum.length ; i++) {
sum[i] = sc.nextInt();
}
//最大值
int max = 0;
for (int i = 0; i < sum.length ; i++) {
if ( max < sum[i]){
max = sum[i];
}
}
//等级
char level;
for (int i = 0; i < sum.length ; i++) {
if (max - 10 <= sum[i]){
level = A;
}else if (max - 20 <= sum[i]){
level = B;
}else if (max - 30 <= sum[i]){
level = C;
}else {
level = D;
}
System.out.println("学生:" + i + " 学生成绩:" + sum[i] +
" 学生等级:" + level);
}
sc.close();
}
}
*题目:输入三个整数x,y,z,请把这三个数由小到大输出。
public class Test03{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个整数");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int max = 0;
int equal = 0;
int min = 0;
if (x > y && x > z){
max = x;
if (y > z){
equal = y;
min = z;
}else {
equal = z;
min = y;
}
}else if (y > x && y > z){
max = y;
if (x > z){
equal = x;
min = z;
}else {
equal = z;
min = x;
}
}else if (z > x && z > y){
max = z ;
if (x > y){
equal = x;
min = y;
}else {
equal = y;
min = x;
}
}
System.out.println(max + " " + equal + " " + min );
sc.close();
}
}
* 上半部分
* * 第一行:四个空格一个星星
* *** 第二行:三个空格三个星星
* ***** 第三行:二个空格五个星星
* ******* 第四行:一个空格七个星星
* ********* 第五行:零个空格九个星星
* ******* 空格数:总行数-行号 星星数:2*行号-1
* ***** 行:外层for循环 内层for循环 内层for循环
* ***
* *
public class Test04{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5 - i; j++){
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1 ; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
* 题目:对10个数进行排序
* 程序分析:利用冒泡法,通过邻位的交换,把当前剩余最大数赶到右边,
* 然后依次从右到左,确定各位的数。
* 输入:31, 42, 21, 50, 12, 60, 81, 74, 101, 93
public class Test05{
public static void main(String[] args) {
int[] sum = new int[10];
int temp = 0;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10 ; i++) {
sum[i] = sc.nextInt();
}
for (int i = 0; i < 10 ; i++) {
for (int j = 0; j < sum.length - i - 1 ; j++) {
if (sum[j] > sum[j + 1]){
temp = sum[j];
sum[j] = sum[j + 1];
sum[j + 1] = temp;
}
}
}
for (int i = 0; i <sum.length ; i++) {
System.out.println(sum[i] + " ");
}
sc.close();
}
}
(七)基础编程题07
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
public class Test01{
public static void main(String[] args) {
double n1 = 1, n2 = 1, temp = 0, sum = 0;
for (int i = 1; i <= 20 ; i++) {
double m1 = n1;
double m2 = n2;
n1 = m1 + m2;
n2 = n1;
temp = n1 / n2;
sum += temp;
}
System.out.println(sum);
}
}
* 题目:利用递归方法求5!。
public class Test02{
public static void main(String[] args) {
int n = 5;
System.out.println("5!=" + getSum(n));
}
public static int getSum(int n){
if (n == 1){
return 1;
}else {
return n * getSum( n - 1);
}
}
}
* 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字
public class Test03{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入不多于5位的正整数:");
String sum = sc.next();
if (sum.length() > 5){
System.out.println("请输入不多于5位的正整数");
}else {
System.out.println(sum + "是" + sum.length() + "位数");
System.out.print("逆序输出位:");
for (int i = sum.length() - 1; i >= 0 ; i--) {
System.out.print(sum.charAt(i) + " ");
}
}
sc.close();
}
}
* 题目:打印出杨辉三角形(要求打印出10行如下图)
public class Test04{
public static void main(String[] args) {
int[][] yanghui = new int[10][];
for (int i = 0; i < yanghui.length; i++) {
yanghui[i] = new int[i + 1];
yanghui[i][0] = yanghui[i][i] = 1;
for (int j = 1; j < yanghui[i].length - 1; j++) {
yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j];
}
}
for (int i = 0; i < yanghui.length; i++) {
for (int j = 0; j < yanghui[i].length ; j++) {
System.out.print(yanghui[i][j] + " ");
}
System.out.println();
}
}
}
* 海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,
* 这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,
* 又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样
* 做的,问海滩上原来最少有多少个桃子?
public class Test05{
public static void main(String[] args) {
int n;
n = getSum(0);
System.out.println("原来有" + getSum(0) + "个桃子");
}
public static int getSum(int i){
if (i == 5){
return 1;
}else {
return getSum(i + 1) * 5 + 1;
}
}
}
(八)基础编程题08
* 1.请编写一个 Application 实现如下功能:接受命令行中给出的一个字母串,
* 先将该串原样输出,然后判断该串的第一个字母是否为大写,
* 若是大写则统计该串中大写字母的个数,
* 并将所有大写字母输出;否则输出信息串”第一个字母不是大写字母!”。
public class Test01{
public static void main(String[] args) {
int count = 0;
if (args.length != 0){
System.out.println(args[0]);
if ((int)args[0].charAt(0) >= 65 && (int)args[0].charAt(0) <= 90){
for (int i = 0; i < args[0].length(); i++) {
if ((int)args[0].charAt(i) >= 65 && (int)args[0].charAt(i) <= 90){
System.out.println(args[0].charAt(i));
count++;
}
}
System.out.println("共有" + count + "个大写字母");
}else {
System.out.println("第一个字母不是大写字母!");
}
}else {
System.out.println("请给出命令行参数!");
}
}
}
* 2.一个应用程序,接受用户输入的一行字符串,统计字符个数,然后反序输出。
public class Test02{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
System.out.println("字符个数为:" + input.length());
for (int i = input.length() - 1; i >= 0; i--){
System.out.print(input.charAt(i) + " ");
}
sc.close();
}
}
* 6. 将一个字符串进行反转。将字符串中指定部分进行反转。比如将“abcdefg”反转为”abfedcg”
*
public class Test03{
public static void main(String[] args) {
String src = ("abcdefg");
for (int i = src.length() - 1; i >= 0; i--){
System.out.print(src.charAt(i));
}
}
}
* 4.编写一个Java应用程序,从键盘读取用户输入两个字符串,
* 并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。
* 要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。
public class Test04{
public static void main(String[] args) {
String s1 = null, s2 = null, ss = null, si = null, sf = null;
int i1 = 0, i2 = 0;
float f1 = 0.0f, f2 = 0.0f;
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("输入第一个字符串:");
s1 = strin.readLine();
System.out.print("输入第二个字符串:");
s2 = strin.readLine();
} catch (Exception e) {
System.out.println(e.getMessage());
}
i1 = Integer.parseInt(s1);
i2 = Integer.parseInt(s2);
f1 = Float.parseFloat(s1);
f2 = Float.parseFloat(s2);
ss = strAdd(s1, s2);
si = strAdd(i1, i2);
sf = strAdd(f1, f2);
System.out.println("输入的二个字符串相加结果为:" + ss);
System.out.println("输入字符串转换为整数相加结果为:" + si);
System.out.println("输入字符串转换为浮点数相加结果为:" + sf);
}
static String strAdd(String str1, String str2) {
return str1 + str2;
}
static String strAdd(int int1, int int2) {
return String.valueOf(int1 + int2);
}
static String strAdd(float flt1, float flt2) {
return String.valueOf(flt1 + flt2);
}
}
* 5. 模拟一个trim方法,去除字符串两端的空格。
public class Test05{
public static void main(String[] args) {
myTrim("word");
}
public static String myTrim(String str){
int start = 0;
int end = str.length() - 1;
while(start < end && str.charAt(start) == ){
start++;
}
while(start < end && str.charAt(end) == ){
end--;
}
if(start == end){
return "";
}
return str.substring(start, end + 1);
}
}
(九)基础编程题09
* 7. 获取一个字符串在另一个字符串中出现的次数。判断str2在str1中出现的次数
public class Test01{
public static void main(String[] args) {
String str1 = "abcdsssde";
String str2 = "s";
System.out.println("str2在str1中出现次数:" + getCount(str1, str2));
}
public static int getCount(String str1, String str2){
int count = 0;
int len;
while ((len = str1.indexOf(str2)) != -1 ){
count++;
str1 = str1.substring(len + str2.length());
}
return count;
}
}
* 8. 获取两个字符串中最大相同子串。
public class Test02{
public static void main(String[] args) {
String str1 = ("hello word11");
String str2 = ("hello word");
System.out.println(getMaxSubString("两个字符串中最大相同子串:" + str1, str2));
}
public static List<String> getMaxSubString(String str1, String str2){
String maxStr = (str1.length() > str2.length()) ? str1 : str2;
String minStr = (str1.length() < str2.length()) ? str1 : str2;
int len = minStr.length();
List<String> list = new ArrayList<>();
for(int i = 0; i < len; i++){
for(int x = 0, y = len - i; y <= len; x++,y++){
String str = minStr.substring(x, y);
if(maxStr.contains(str)){
list.add(str);
}
}
if(list.size() != 0){
return list;
}
}
return null;
}
}
* 9. 对字符串中字符进行自然顺序排序
public class Test03{
public static void main(String[] args) {
String str = ("547157");
System.out.println(sort(str));
}
public static String sort(String str){
char[] c = str.toCharArray();
Arrays.sort(c);
return new String(c);
}
}
* 10. 中国有句俗语叫“三天打鱼两天晒网”。
* 如果从1990年1月1日起开始执行“三天打鱼两天晒网”。
* 如何判断在以后的某一天中是“打鱼”还是“晒网”?
public class Test04{
public static void main(String[] args) {
String date1 = "1990/1/1";
String date2 = "1990/1/10";
long day = getQuot(date1, date2);
if (day % 5 == 0 || day % 5 == 4) {
System.out.println("今天是休息日,可以晒晒网");
} else {
System.out.println("今天要工作,打鱼了!");
}
}
public static long getQuot(String time1, String time2) {
long dayDistance = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
try {
Date date1 = ft.parse(time1);
Date date2 = ft.parse(time2);
dayDistance = date2.getTime() - date1.getTime();
dayDistance = dayDistance / 1000 / 60 / 60 / 24 + 1;
} catch (ParseException e) {
e.printStackTrace();
}
return dayDistance;
}
}
* 任意给定的一串字母,统计字符串里面的大写字母和小写字母的个数。
public class Test05{
public static void main(String[] args) {
int bigWord = 0;
int smallWord = 0;
String word = "dasaFASD";
char[] arr = word.toCharArray();
for (int i = 0; i < arr.length; i++) {
int x = arr[i];
if (x >= 97 & x <= 122){
smallWord++;
}else if (x >= 65 & x <= 90){
bigWord++;
}
}
System.out.println("大写字母个数为:" + bigWord + " " + "小写字母个数为:" + smallWord);
}
}
* 16.编写java程序,随便输入两个单词,两个单词之间以空格隔开,
*输出时每个单词的首字母变为大写。如输入:“hello java”,输出为“Hello Java”
public class Test02{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word = sc.nextLine();
System.out.println(test(word));
sc.close();
}
private static String test(String input){
String output = "";
String[] s = input.split("\s");
for (String s1 : s){
output += s1.substring(0,1).toUpperCase() + s1.substring(1) + " ";
}
return output;
}
}