Java小题

1.编写程序,输入一个字符,判断它是否为小写字母,如果是,将它转换成大写字母,否则,不转换。

import java.io.IOException;

public class zhifu {

    public static void main(String[] args) throws IOException {

        char ch = (char)System.in.read();

        if (ch >= 'a' && ch <= 'z') {
            ch -= 32;
        }

        System.out.println(ch);
    }
}

2.输入 3 个正数,判断能否构成一个三角形。

import java.util.Scanner;

public class sanjiao {
    public static void main(String[] args) {
        double a; double b; double c;
        System.out.println("请输入三个数");

        Scanner in = new Scanner(System.in);
        a = in.nextDouble();
        b = in.nextDouble();
        c = in.nextDouble();
        if(a<=0 || b<=0 || c<=0){
            System.out.println("请输入正数");
        }
        if((a+b)>c&&(a+c)>b&&(b+c)>a){
            System.out.println("是三角形");
        }
        else{
            System.out.println("不是三角形");
        }
    }
}

4.打印出如下图案(菱形)
    *
   ***
 ******
********
 ******
  ***
   *

import java.util.Scanner;

public class lingxing {
    public static void main(String[] args) {
        int a; int b;
        System.out.println("请输入菱形层数");

        Scanner in = new Scanner(System.in);
        b = in.nextInt();
        a=(b+1)/2;
        for(int i=0;i<a;i++){
            for(int k=a-1;k>i;k--){
                System.out.print(" ");
            }
            for(int j=0;j<2*i+1;j++){
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = 1; i <= a-1; i++) {
            for (int j = 1; j <= i; j++){
                System.out.print(" ");
            }
            for (int k = 2*a-3; k >= 2 * i - 1; k--){
                System.out.print('*');
            }
            System.out.println();
        }
    }
}

5.猴子吃桃问题。猴子第一天摘下若干个桃子,当时就吃了一半,还不过瘾,就又吃了一个。第二天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃前一天剩下的一半零一个。到第 10 天在想吃的时候就剩一个桃子了,求第一天共摘下来多少个桃子?

public class hou {
    public static void main(String[] args) {
        int sum=0; int a = 1;
        for(int i=1;i<10;i++){
            a = (a+1)*2;
            sum = a;
        }
        System.out.println("第一天摘了"+sum+"个桃子");
    }
}

6.将一个给定的整型数组转置输出,
例如:    源数组,1 2 3 4 5 6
        转置之后的数组,6 5 4 3 2 1

import java.util.Scanner;
public class shuzu {
    public static void main(String[] args) {
        int n,i,t;
        System.out.println("输入数组长度");
        Scanner scan=new Scanner(System.in);
        n=scan.nextInt();
        int []a=new int[n];
        for(i=0;i<n;i++)
            a[i]=scan.nextInt();
        for(i=n-1;i>=0;i--)
        {
            System.out.println(a[i]);
        }
    }
}

7.现在有如下的一个数组:
    int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
    要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
        int[] newArr = {1,3,4,5,6,6,5,4,7,6,7,5} ;

import java.util.Scanner;
public class shuzu2 {
    public static void main(String[] args) {
        int n;
        System.out.println("输入数组长度");
        Scanner scan = new Scanner(System.in);
        n=scan.nextInt();
        int []oldArr=new int[n];
        for(int i=0;i<n;i++)
            oldArr[i]=scan.nextInt();
        int[] newArr =new int[oldArr.length];
        int j=0;//标记
        for(int i=0;i<oldArr.length;i++){
            if(oldArr[i]!=0){//判断
                newArr[j]=oldArr[i];//赋值
                j++;
            }
        }
        for(int i=0;i<j;i++){
            System.out.print(newArr[i]+" ");
        }
    }
}

8.现在给出两个数组:
    数组a:"1,7,9,11,13,15,17,19"
    数组b:"2,4,6,8,10"
      两个数组合并为数组c并且排序从小到大输出。

public class shuzu3 {
    public static void main(String[] args) {
        int[] a = {1, 7, 9, 11, 13, 15, 17, 19};
        int[] b = {2, 4, 6, 8, 10};
        int m = a.length + b.length;
        int j = 0,temp;
        int[] c = new int[m];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i];
        }
        for (int i = a.length; i < m; i++) {
            c[i] = b[j];
            j++;
        }

        for(int i=0;i<m-1;i++){
            for(int s=0;s<m-1-i;s++){
                if(c[s]>c[s+1]){
                    temp=c[s];
                    c[s]=c[s+1];
                    c[s+1]=temp;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            System.out.print(c[i] + " ");
        }
    }
}

 

posted @ 2019-04-28 16:11  追HIGH  阅读(322)  评论(1编辑  收藏  举报