• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
戈瑾
博客园    首页    新随笔    联系   管理    订阅  订阅
Java入门——day39
实验一练习

1.书写一个程序,输入为整数的八进制,输出为其十进制

import java.util.Scanner;
public class Study {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.print("请输入一个八进制的数字:");
        String str=in.nextLine();
        System.out.println(str+"转化为十进制为:"+Integer.parseInt(str,8));
    }
}

 

 知识点:进制转换

(1)十进制转r进制

 

 (2)r进制转十进制

 


 

2.编写代码,实现对圆周率输出的结果以小数点形式表示,显示正号"+",area占用位数为15

import java.util.Scanner;
public class Study {
    public static void main(String[] args) {
        double n,s;
        Scanner in=new Scanner(System.in);
        System.out.print("Input the radius of the circle:");
        n=in.nextDouble();
        s=3.14159*n*n;
        System.out.print("The area of the circle is:");
        System.out.printf("%+15f",s);
    }
}

 

  知识点:Java中的格式化输出

 

"%"表示进行格式bai化输du出,"%"之后的内容为格式的定义。

int i;

  • System.out.printf("%d",i);           //"d"表示输出十进制整数

  • System.out.printf("%x",i);           //"d"表示输出十六进制整数

  • System.out.printf("%o",i);           //"o"表示输出八进制整数

double d;

  • System.out.printf("%f",d);             //"f"表示格式化输出浮点数

  • System.out.printf("%15.3f",d);      //"15.3"中的15表示输出的长度,3表示小数点后的位数

  • System.out.printf("%+15.3f",d);    //"+"表示输出的数带正负号

  • System.out.printf("%015.3f",d);    //"0"表示输出的数指定空位填0

  • System.out.printf("%-15.3f",d);     //"-"表示输出的数左对齐(默认为右对齐)

  • System.out.printf("%+-15.3f",d);   //"+-"表示输出的数带正负号且左对齐

char c;

  • System.out.printf("%c",c);           //"d"表示输出一个字符

String str;

  • System.out.printf("%s",str);           //"o"表示输出一个字符串

 

3.在主函数中输入一个一维数组,调用函数maxAndMin得到数组元素中的最大值与最小值

import java.util.Scanner;
public class Study {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int arr[]=new int[10];
        System.out.print("请输入10个数字:");
        for(int i=0;i<arr.length;i++) {
            arr[i]=in.nextInt();
        }
        Study.maxAndMin(arr);
    }
    public static void maxAndMin(int a[]) {
        int max,min;
        max=min=a[0];
        for(int i=0;i<a.length;i++) {
            if(a[i]>max) {
                max=a[i];
            }
            if(a[i]<min) {
                min=a[i];
            }
        }
        System.out.println("The maxiNum is:"+max);
        System.out.println("The miniNum is:"+min);
    }
}

 


 

 4.文件名与类型的分离

一个完整的文件名字包括文件名与类型的扩展名,例如,a.doc, b.txt, film.rbmv等,文件名与类型的扩展名之间用.分离。请使用string类型,编写一个程序实现文件名与类型扩展名的分离,例如,输入是字符串a.doc,输出是两个字符串a和doc。要求使用string类型实现。

import java.util.Scanner;
public class Study {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入文件名:");
        String str = in.next();
        // 获得字符“.”出现的索引
        int p = str.indexOf(".");
        System.out.println("文件名:" + str.substring(0, p));
        System.out.println("类型扩展名:" + str.substring(p + 1));
    }
}


 

 5.字符串查找判断

输入两个字符串s1和s2,判断s1是否包含s2,给出结论,若包含,还需计算s1中s2的个数。要求使用string类型

import java.util.Scanner;
public class Study {
    public static void main(String[] args) {
        String str1, str2, str3;
        int m, n;
        int count = 0;
        Scanner in = new Scanner(System.in);
        System.out.print("请输入字符串s1:");
        str1 = in.nextLine();
        System.out.print("请输入字符串s2:");
        str2 = in.nextLine();
        // 判断字符串s1是否包含s2
        if ((str1.indexOf(str2)) != -1) {
            System.out.println("s1包含s2");
        } else {
            System.out.println("s1不包含s2");
        }
        m = str1.length();
        n = str2.length();
        // 判断s1中s2的个数
        for (int i = 0; i < m - n + 1; i++) {
            str3 = str1.substring(i, i + n);
            if (str3.equals(str2)) {
                count++;
            }
        }
        System.out.println("包含个数为:" + count);
    }
}

 


 

6.整数排序

编写函数重载,分别将两个整数升序排列后输出、三个整数升序排列后输出、四个整数升序排列后输出

import java.util.Scanner;
public class Study {
    public static void sort(int a[], int n) {
        int temp;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
    //两个整数排序
    public static void sort(int a, int b) {
        int arr[] = { a, b };
        sort(arr, 2);
    }
    //三个整数排序
    public static void sort(int a, int b, int c) {
        int arr[] = { a, b, c };
        sort(arr, 3);
    }
    //四个整数排序
    public static void sort(int a, int b, int c, int d) {
        int arr[] = { a, b, c, d };
        sort(arr, 4);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a1, a2;
        int b1, b2, b3;
        int c1, c2, c3, c4;
        System.out.print("请输入两个整数:");
        a1 = in.nextInt();
        a2 = in.nextInt();
        System.out.print("排序后:");
        sort(a1, a2);
        System.out.print("请输入三个整数:");
        b1 = in.nextInt();
        b2 = in.nextInt();
        b3 = in.nextInt();
        System.out.print("排序后:");
        sort(b1, b2, b3);
        System.out.print("请输入四个整数:");
        c1 = in.nextInt();
        c2 = in.nextInt();
        c3 = in.nextInt();
        c4 = in.nextInt();
        System.out.print("排序后:");
        sort(c1, c2, c3, c4);
    }
}

 

 

posted on 2020-08-13 21:35  戈瑾  阅读(229)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3