码迷,mamicode.com
首页 > 编程语言 > 详细

java程序设计作业 c.njupt.edu.cn java实现第三次作业

时间:2018-04-06 14:02:48      阅读:1033      评论:0      收藏:0      [点我收藏+]

标签:func   cas   scan   顺序   static   详细   选择题   char   错误   

王利国的"Java语言程序设计第3次作业(2018)"详细

总分:100

选择题得分:60

 1. 设有如下定义语句: String s1=”My cat”; int m=s1.compareTo(“My a cat”); 语句被执行后m的值为( )
A.2
B.1
C.0
D.-2
 2. 语句 String s1=new String(“Hello”); String s2=new String(“Hello”); System.out.println(s1==s2); System.out.printlv(s1.equals(s2)); 执行后的输出结果是( )
A.Hello false
B.Hello true
C.Hello Hello
D.false true
 3. 执行下列语句 int[] lx={2,3,4,5}; lx[3]=lx[3]==--lx[0]?++lx[1]:lx[2]--; 后,数组lx的元素值分别为( )
A.1,2,3,4
B.1,3,3,3
C.1,2,3,3
D.1,3,3,4
 4. 在一个应用程序中定义了数组a:int[] a={1,2,3,4,5,6,7,8,9,10},为了打印输出数组a的最后一个数组元素,下面正确的代码是( )
A.System.out.println(a[10]);
B.System.out.println(a[9]);
C.System.out.println(a[8]);
D.System.out.println(a[a.length]);
 5. 设有定义语句int a[]={36,72,99};则以下对此语句叙述错误的是( )
A.该语句定义了一个名为a的一维数组;
B.a数组有3个元素;
C.数组中的每个元素是整型;
D.a数组的元素的下标为1~3;
 6. 下面关于数组定义语句不正确的是( )
A.float f[]=new{2.4f,3.5f,5.7f,7.9f};
B.int a[]={1,2,3,4,5}
C.double[]d=new double[10];
D.int[]a2;
 7. 下列语句序列执行后,c的值是( ) int a=3,b=4,c=0; while ((a++)<(--b))++c;
A.0
B.1
C.2
D.3
 8. 下列语句序列执行后,a的值是( ) int a=1; for(int i=5;i>0;i-=2) a*=i;
A.0
B.1
C.15
D.60
 9. 下列语句序列执行后,c的值是( ) int a=10,b=18,c=30; switch(b-a){ case 8 : c++; case 9 : c+=2; case 10 : c+=3; default :c/=b; }
A.31
B.32
C.2
D.33
 10. 设a、b为int型变量,c、d为fload类型变量,ch为char类型变量,且所有变量均已赋值,则下列正确的switch语句是( )
A.switch(a+b);{...}
B.switch(ch+1){...}
C.switch ch {...}
D.switch(c+d){...}
 11. 下列语句序列执行后,c的值是( ) int a=4,b=5,c=9,d=6; if (a>b||c<d) c--; else c++;
A.6
B.10
C.8
D.9
 12. 下列语句序列执行后,c的值是( ) int a=6,b=3,c=5; if (a==b) c+=a;else c=++a*c;
A.15
B.25
C.35
D.45
 13. 下列语句序列执行后,c变量的值为( ) int a=2,b=4,c=5; if (a<--b) c*=a;
A.5
B.20
C.15
D.10
 14. 下列语句序列执行后,ch1变量中的值为( ) char ch1=’A’,ch2=’B’; if (ch1+2<ch2)++ch1;
A.‘A‘
B.‘B‘
C.A
D.B
 15. 以下选项中,合法的赋值语句是( )
A.++m!=n--;
B.++m;
C.m=m+1=5;
D.m==1;
 16. 设有定义“int a=22;long b=56;”,下面赋值不正确的语句是( )
A.a=b;
B.b=(long)a;
C.a=(int)b;
D.b=a;
 17. 下面选项中,( )是正确的输出结果 int m=2,n=1; m+=m-=n; System.out.println(“m=”+m);
A.m=1
B.m=2
C.m=3
D.m=4
 18. 下列语句序列执行后的结果为( ) int a=10,b=4,c=20,d=6; System.out.println(a++*b+c*--d);
A.144
B.140
C.28
D.不能执行
 19. 假设以下选项中的变量都已经正确定义,则不合法的表达式是( )
A.a>4==6<1;
B.’n’-3;
C.’a’=8;
D.’A’%6
 20. 假设a为已经声明并以赋初值的int类型变量,则对于a的赋值语句正确的是( )
A.int a=6;
B.a==3;
C.a=3.2f;
D.a+=a*3;

编程题得分:40

 打印每月有几天  得分:10 / 10
import java.util.Scanner;

/**
* @Author liguo
* @Description
* @Data 2018-04-03
*/
public class Main {

    public static void main(String[] args) {
        int year;
        int month;
        int[] a = {1, 3, 5, 7, 8, 10, 12};
        int[] b = {4, 6, 9, 11};
        Scanner in = new Scanner( System.in );
        year = in.nextInt();
        month = in.nextInt();
        //判断二月,考虑闰年情况
        if (month == 2) {
            if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0)
                System.out.println( year + "-" + month + "-29" );
            else
                System.out.println( year + "-" + month + "-28" );
        }
        //判断三十一天的情况
        for (int element : a) {
            if (month == element) {
                System.out.println( year + "-" + month + "-31" );
            }
        }
        //判断三十天的情况
        for (int element : b) {
            if (month == element) {
                System.out.println( year + "-" + month + "-30" );
            }
        }
    }
}
 4-3-2 百分制成绩转换等级制成绩  得分:10 / 10
import java.util.Scanner;

/**
* @Author liguo
* @Description 从键盘上读入一个百分制成绩x(0 < = x < = 100),
* 将其转换为等级制成绩输出。本题在C语言和Java语言中要求使用switch分支实现。
* 等级制成绩(百分制成绩)
* A(90<=x<=100)
* B(80<=x<90)
* C(70<=x<80)
* D(60<=x<70)
* E(0<=x<60)
* @Data 2018-04-03
*/
public class Main {

    static void judge(int mark) {
        char degree = ‘A‘;
        int temp = mark / 10;
        if (temp >= 0 && temp < 6)
            degree = ‘E‘;
        if (temp == 6)
            degree = ‘D‘;
        if (temp == 7)
            degree = ‘C‘;
        if (temp == 8)
            degree = ‘B‘;
        if (temp == 9 || temp == 10)
            degree = ‘A‘;
        System.out.println( mark + "--" + degree );
    }

    public static void main(String[] args) {
        int temp;
        Scanner in = new Scanner( System.in );
        int x = in.nextInt();
        temp = x / 10;
        if (temp >= 0 && temp < 6)
            temp = 5;
        switch (temp) {
            case 5:
                judge( x );
                break;
            case 6:
                judge( x );
                break;
            case 7:
                judge( x );
                break;
            case 9:
                judge( x );
                break;
            case 10:
                judge( x );
                break;
        }
    }
}
 用if语句求解分段函数  得分:10 / 10
import java.util.Scanner;

/**
* @Author liguo
* @Description分段函数求解:输入 x ,计算并输出 y 的值:
* y=x+100 ( 当 x < 20)
* y= x ( 当 2 0 ≤ x ≤ 100)
* y=x-100 ( 当 x > 100)
* @Data 2018-04-03
*/
public class Main {

    public static void main(String[] args) {
        double x, y;
        Scanner in = new Scanner( System.in );
        x = in.nextDouble();
        if (x < 20)
            y = x + 100;
        else if (x >= 20 && x <= 100)
            y = x;
        else
            y = x - 100;
        System.out.printf( "x=%.2f,y=%.2f", x, y );

    }
}
 2-2 混合类型数据格式化输入  得分:10 / 10
import java.util.Scanner;

/**
* @Author liguo
* @Description 输入在一行中顺序给出浮点数1、整数、字符、浮点数2,其间以1个空格分隔。
* 输入描述
* 输入在一行中顺序给出浮点数1、整数、字符、浮点数2,其间以1个空格分隔。
* 输出描述
* 在一行中按照字符、整数、浮点数1、浮点数2的顺序输出,其中浮点数保留小数点后2位。
* @Data 2018-04-03
*/
public class Main {

public static void main(String[] args) {
Scanner in = new Scanner( System.in );
double d1 = in.nextDouble();
int i = in.nextInt();
String s = in.next();
char c = s.charAt( 0 );
double d2 = in.nextDouble();
System.out.printf( "%c %d %.2f %.2f", c, i, d1, d2 );
	}
}
 3-4-1a 计算三位数的位数和  得分:10 / 10

import java.util.Scanner;
/**
* @Author liguo
* @Description 从键盘输入任意一个三位数的整数,请编写程序计算这个整数的数位和。
输入描述
输入一个三位数的整数
输出描述
* @Data 2018-04-03
*/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner( System.in );
        int temp =  in.nextInt();
        temp = Math.abs(temp);
        int a = temp/100;
        int b = temp /10%10;
        int c = temp %10;
        int sum = a+b+c;
        System.out.println(sum);
    }
}

java程序设计作业 c.njupt.edu.cn java实现第三次作业

标签:func   cas   scan   顺序   static   详细   选择题   char   错误   

原文地址:https://www.cnblogs.com/liguo-wang/p/8727309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!