2.3.4章课后习题

2018-10-24  本文已影响0人  金厚琦

第二章课后习题:

1、 已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)

public class Exercise_02_01 {
    public static void main(String[] args) {
        int a = 100;
        int b = 200;
        int tmp;
        //进行数据交换
        tmp = a;
        a = b;
        b = tmp;
        System.out.println("交换后的a:"+ a + ",b:" + b);
    }

}

2、 给定一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12注:分解数字既可以先除后模也可以先模后除(知识点:变量和运算符综合应用)

public class Exercise_02_02 {
    public static void main(String[] args) {
        //Scanner类是java.util包中提供的一个操作类,使用此类可以方便的完成输入流的输入操作。
        Scanner scan = new Scanner(System.in);
        //此行代码会阻塞,等待用户从键盘输入int类型数据,并接受数据赋值给变量i。
        int i = scan.nextInt();
        int x1 = i / 1000;
        int x2 = i / 100 % 10;
        int x3 = i / 10 % 10;
        int x4 = i % 10; 
        System.out.println(x1 + x2 + x3 + x4);
    }
}

3、 华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏度转成华氏度,直接乘以9,除以5,再加上32即行。

public class Exercise_02_03 {
    public static void main(String[] args) {
        float c = 45.0f;
    float f = 113.0f;
    float cTof = (f-32) * 5/9;
    float fToc = c*9/5+32;
    System.out.println("摄氏转华氏 = "+cTof );
    System.out.println("华氏转摄氏  = "+fToc );

    }
}

4、 给定一个任意的大写字母A~Z,转换为小写字母。

public class Exercise_02_04 {
    public static void main(String[] args) {
        char c = 'A';
        //加32即小写对应字母
        System.out.println((char) (c + 32) );


    }
}

第三章课后习题

1.企业发放的奖金根据利润提成。利润低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数?

public class Answer {
    public static void main(String[] args) {
     double x = 0,y = 0;
     System.out.print("输入当月利润(万):");
     Scanner s = new Scanner(System.in);
     x = s.nextInt();
     if(x > 0 && x <= 10) {
     y = x * 0.1;
     } else if(x > 10 && x <= 20) {
      y = 10 * 0.1 + (x - 10) * 0.075;
     } else if(x > 20 && x <= 40) {
      y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;
     } else if(x > 40 && x <= 60) {
      y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;
     } else if(x > 60 && x <= 100) {
      y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015; 
     } else if(x > 100) {
      y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;
     }
     System.out.println("应该提取的奖金是 " + y + "万");
    }
}

2、 给定一个成绩a,使用switch结构求出a的等级。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59(知识点:条件语句switch)

public class Exercise_03_02 {
    public static void main(String[] args) {
        int x;
        int grade = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("请输入一个成绩: ");
        x = s.nextInt();
                
        if (x > 0 && x <= 100) {//判断成绩是否合法,如果合法,进行比较
            grade = x/10;
            switch(grade){
                case 10:
                case 9:System.out.println("等级为A");break;
                case 8:System.out.println("等级为B");break;
                case 7:System.out.println("等级为C");break;
                case 6:System.out.println("等级为D");break;
                default:System.out.println("等级为E");break;
                
            }
                        
        } else {//判断成绩是否合法,如果非法,进行提示用户
            System.out.println("输入的成绩必须在0-100之间" );
        }
        
        
        
    }
}

3. 输入一个数字,判断是一个奇数还是偶数

    int i = new Scanner(System.in).nextInt();
if(i!=0){
            System.out.println("0");
        }
        else if(i%2==1){
            System.out.println("奇数");
        }else if(i%2==0){
            System.out.println("偶数");
        }
}

4. 编写程序, 判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出x=10,除了以上几个值,都输出x=none。(答案SwitchDemo.java)

    int x=1;

switch(x)
{
case 1:
{
System.out.println("x=1");
break;
}
case 5:
{
System.out.println("x=5");
break;
}
case 10:
{
System.out.println("x=10");
break;
}
default:
{
System.out.println("none");
break;
}
}

5. 判断一个随机整数是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)

    int value = new Random().nextInt(100);
    if (value % 5 == 0 && value % 6 == 0) {
     System.out.println("输入的数字" + value + "能被5和6整除");
    } else if (value % 5 == 0) {
     System.out.println("输入的数字" + value + "能被5整除");
    } else if (value % 6 == 0) {
     System.out.println("输入的数字" + value + "能被6整除");
    } else {
     System.out.println("输入的数字不能被5或者6整除");
    }

6.输入一个年份,判断这个年份是否是闰年

    int year = new Scanner(System.in).nextInt();
    if(year%4==0&&year%100!=0||year%400==0){
        System.out.println("闰年"); 
    }else{
        System.out.println("不是闰年");
    } 

7.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印A,B,C,D,E

    int score = 999;
    if(score<=100&&score>=90)
        System.out.println("A"); 
    else if(score<90&&score>=80)
        System.out.println("B");
    else if(score<80&&score>=70)
        System.out.println("C");
    else if(score<70&&score>=60)
        System.out.println("D");
    else if(score<=70&&score>60)    
        System.out.println("E");
    else
        System.out.println("分数无效");

8.试写一个三位数,从小到大排列,然后再从大到小排列。

import java.util.Scanner;

public class Answer {
    public static void main(String[] args) {
     input fnc = new input();
     int x=0, y=0, z=0;
     System.out.print("输入第一个数字:");
      x = fnc.input();
     System.out.print("输入第二个数字:");
      y = fnc.input();
     System.out.print("输入第三个数字:");
      z = fnc.input();   
    if(x > y) {
      int t = x;
      x = y;
      y = t;
     }
    if(x > z) {
      int t = x;
      x = z;
      z = t;
     }
    if(y > z) {
      int t = y;
      y = z;
      z = t;
     }
    System.out.println( "三个数字由小到大排列为: "+x + " " + y + " " + z);
    }
    }
    class input{
    public int input() {
     int value = 0;
     Scanner s = new Scanner(System.in);
     value = s.nextInt();
     return value;
    }

}

9.有一个不多于5位的正整数,求它是几位数,分别打印出每一位数字。

import java.util.Scanner;
public class Answer {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
// TODO code application logic here
//int num=12345;
System.out.println("请输入一个不多于五位的正整数:");
int num = input();
String str = String.valueOf(num);
System.out.println(num + "的位数为:" + str.length());
System.out.println("它的各位数分别为:");
for (int i = 0; i < str.length(); i++) {
//System.out.println(str.charAt(i));
System.out.print(str.charAt(i) + " ");
}
System.out.println();
System.out.println("它的各位数逆序分别为:");
for (int i = str.length() - 1; i >= 0; i--) {
//System.out.println(str.charAt(i));
System.out.print(str.charAt(i) + " ");
}
System.out.println();
}

private static int input() {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
return n;
}
}

1.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。

double nianxin=30000;
        long sum = 0;
        
        for(int i=1;i<=10;i++){
            nianxin = nianxin*(1+0.06);
            sum+=nianxin;
        }
        System.out.println("年薪为"+nianxin+"总工资为"+sum);

2.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。程序分析:采取逆向思维的方法,从后往前推断。

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


}

3. 编写一个程序,计算邮局汇款的汇费。如果汇款金额小于100元,汇费为一元,如果金额在100元与5000元之间,按1%收取汇费,如果金额大于5000元,汇费为50元。汇款金额由命令行输入。

public class Answer {

    public static void main(String[] args) {
        double A = Integer.parseInt(args[0]);
        double a = 0;
        if (A > 5000) {
            a = 50;
        } else if (100 <= A) {
            a = A * 0.01;
        } else if (100 > A) {
            a = 1.0;
        }
        System.out.println("汇费为:" + a);
    }

}

4. 分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。

public class Answer {
    public static void main(String[] args) {
        //for部分
        {
            int i, a = 0;
            for (i = 1; i <= 100; i++) {
                if (i % 3 == 0)
                    a = a + i;
            }
            System.out.println(a);
        }
        //while 部分
        {
            int i = 1, a = 0;
            while (i < 101) {
                if (i % 3 == 0) {
                    a = a + i;
                }
                i++;
            }
            System.out.println(a);
        }
//do while部分
        {
            int i = 0, a = 0;
            do {
                if (i % 3 == 0) {
                    a = a + i;
                }
                i++;
            } while (i < 101);
            System.out.println(a);
        }
    }
}

5. 输出0-9之间的数,但是不包括5。
public static void main(String[] args)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
continue;
}
System.out.println(i);
}
}
6. 编写一个程序,求整数n的阶乘,例如5的阶乘是12345

import java.util.Scanner;
public class Answer {
    public static void main(String[] args) {
        System.out.println("input:");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int i = 0;
        int m = 1;
        for (i = 1; i <= n; i++) {
            m = m * i;
        }
        System.out.println("n的阶乘为:"+m);
    }
}

7. 编写一个程序,找出大于200的最小的质数

public class Answer {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 200; i < 300; i++) {
            boolean b = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    b = false;
                    break;
                }
            }
            if (!b) {
                continue;
            }
            System.out.println(i);
            break;
        }
    }
}

8. 由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321

int a, b, c, d, s;
        int n = Integer.parseInt(args[0]);
        a = n / 1000;
        b = n / 100 % 10;
        c = n / 10 % 10;
        d = n % 10;
        s = d * 1000 + c * 100 + b * 10 + a;
        System.out.println("反转后数为:" + s);

第四章练习题

1. 编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。
/*例5-1
*数组使用范例
*/

public class ArrayDemo
{
public static void main(String[] args)
{
int[] buffer=new int[5];

buffer[0]=10;
buffer[1]=20;
buffer[2]=30;
buffer[3]=40;
buffer[4]=50;

for(int i=0;i<5;i++)
{
System.out.println(buffer[i]);
}
}
}

2.将一个字符数组的值(neusofteducation)考贝到另一个字符数组中。

public class ArrayCopyDemo {
public static void main(String[ ] args) {
//定义源字符数组 
char[ ]  copyFrom = {'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n'};
char[ ] copyTo = new char[7];

System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));

}
}

3. 给定一个有9个整数(1,6,2,3,9,4,5,7,8})的数组,先排序,然后输出排序后的数组的值。

public class ArraySortDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] point = {1,6,2,3,9,4,5,7,8};
        
        java.util.Arrays.sort( point );
        
        for(int i=0;i<point.length;i++)
        {
        System.out.println(point[i]);
        }

    }

}

4. 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
/*例5-3
*多维数组范例
*/

public class ArrayTwoDimension
{
public static void main(String[] args)
{
double[][] buffer=new double[5][4];

for(int i=0;i<buffer.length;i++)
{
for(int j=0;j<buffer[0].length;j++)
{
System.out.print(buffer[i][j]);
}
System.out.println();
}
}
}

5. 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

public class Arraymax {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] a = {18,25,7,36,13,2,89,63};
        int max = a[0];
        int maxIndex = 0;
        for(int i=1;i<a.length;i++)
        {
            if(max<=a[i]){
                max = a[i];
                maxIndex = i;
            }
        }
        System.out.println("最大值为:"+max+" 最大值下标为:"+maxIndex);
    }

}

1、有2个多维数组分别是 2 3 4 和 1 5 2 8
4 6 8 5 9 10 -3
2 7 -5 -18
按照如下方式进行运算。生成一个2行4列的数组。此数组的第1行1列是21+35+42
第1行2列是2
5+39+47 第2行1列是41+65+8*2 依次类推。

package com.neusoft.javaTest;

public class Array2 {

    /**
     * @param args
     */
    
    public static void main(String[] args) {
            int a[][] = { { 2, 3, 4 }, { 4, 6, 8 } };
            int b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };    
            for(int k=0;k<a.length;k++){                
                for(int i=0;i<b[0].length;i++){
                    int num = 0;
                    for(int j=0;j<b.length;j++){
                        num += a[k][j]*b[j][i];
                    }
                    System.out.print(num+"  ");
                }   
                System.out.println("");          
            }
            
    }

}

2. 将一个数组中的元素逆序存放

public class Answer {
public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       int a[] = new int[20];
    System.out.println("请输入多个正整数(输入-1表示结束):");
       int i=0,j;
       do{
      a[i]=s.nextInt();
      i++;
       }while (a[i-1]!=-1);
       System.out.println("你输入的数组为:");
       for( j=0; j<i-1; j++) {
    System.out.print(a[j]+"   ");
    }
       System.out.println("\n数组逆序输出为:");
       for( j=i-2; j>=0; j=j-1) {
    System.out.print(a[j]+"   ");
    }
    }

}


}

4、给定一维数组{ -10,2,3,246,-100,0,5} ,计算出数组中的平均值、最大值、最小值。

    int a[] = new int[]{ -10,23,246,-100,0,5};
     int max = a[0];
     int min = a[0];
     int add = a[0];
     for(int i =1;i<a.length;i++){
         if(a[i]< min){
             min = a[i];
         }else if(a[i]>max){
             max = a[i];
         }
         add = add+a[i];
     }
    System.out.println("最小值:"+min);
    System.out.println("最大值:"+max);
System.out.println("平均值:"+add/a.length);
上一篇 下一篇

猜你喜欢

热点阅读