Java上机答案

这是一组关于Java编程的选择题,涵盖了条件判断、switch语句、变量赋值等多个知识点。同时,提供了几个编程题目,包括计算阶乘之和、递归求解阶乘、判断闰年、计算一年中的天数以及打印乘法口诀等,旨在检验和提升编程能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

选择

题目1:If number is an Integer, analyze the following code:
Code 1:
boolean even;
if (number % 2 == 0)
even = true;else even = false;

Code 2:
boolean even = (number % 2 == 0);
选择一项:
Code 2 has syntax errors.
Both Code 1 and Code 2 have syntax errors.
Code 1 has syntax errors.
Both Code 1 and Code 2 are correct, but Code 2 is better.
反馈
正确答案是:Both Code 1 and Code 2 are correct, but Code 2 is better.
题目2
What is the output of the following switch statement char ch = ‘a’; switch (ch) { case ‘a’: case ‘A’: System.out.print(ch); break; case ‘b’: case ‘B’: System.out.print(ch); break; case ‘c’: case ‘C’: System.out.print(ch); break; case ‘d’: case ‘D’: System.out.print(ch); }
选择一项:
ab
aa
abcd
a
abc
反馈
正确答案是:a
题目3
What is x after evaluating x = (2 > 3) ? 2 : 3;
选择一项:
2
3
4
5
反馈
正确答案是:3
题目4
正确
获得1.00分中的1.00分
未标记标记题目
题干
Which of the following is a constant, according to Java naming conventions?
选择一项:
MAX_VALUE
Test
ReadInt
read
反馈
正确答案是:MAX_VALUE
题目5
正确
获得1.00分中的1.00分
未标记标记题目
题干
What is y after the following switch statement? int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -1 }
选择一项:
-1
2
0
1
反馈
正确答案是:-1
题目6
正确
获得1.00分中的1.00分
未标记标记题目
题干
To declare a constant MAX_LENGTH inside a method with value 99.98, you write
选择一项:
final float MAX_LENGTH = 99.98;
final double MAX_LENGTH = 99.98;
final MAX_LENGTH = 99.98;
double MAX_LENGTH = 99.98;
反馈
正确答案是:final double MAX_LENGTH = 99.98;
题目7
正确
获得1.00分中的1.00分
未标记标记题目
题干
Which of the following code displays the area of a circle if the radius is positive.
选择一项:
if (radius <= 0) System.out.println(radius * radius * 3.14159);
if (radius > 0) System.out.println(radius * radius * 3.14159);
if (radius != 0) System.out.println(radius * radius * 3.14159);
if (radius >= 0) System.out.println(radius * radius * 3.14159);
反馈
正确答案是:if (radius > 0) System.out.println(radius * radius * 3.14159);
题目8
正确
获得1.00分中的1.00分
未标记标记题目
题干
The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
选择一项:
76.02
76.03
76.0252175
76
反馈
正确答案是:76
题目9
正确
获得1.00分中的1.00分
未标记标记题目
题干
Analyze the following code. int x = 0; if (x > 0); { System.out.println(“x”); }
选择一项:
The symbol x is always printed twice.
The symbol x is always printed.
The value of variable x is always printed.
Nothing is printed because x > 0 is false.
反馈
正确答案是:The symbol x is always printed.
题目10
正确
获得1.00分中的1.00分
未标记标记题目
题干
What is y after the following switch statement is executed?x = 3;switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1;}
选择一项:
1
2
3
4
0
反馈
正确答案是:2
题目11
正确
获得1.00分中的1.00分
未标记标记题目
题干
Assume x is 0. What is the output of the following statement?if (x > 0) System.out.print(“x is greater than 0”);else if (x < 0) System.out.print(“x is less than 0”);else System.out.print(“x equals 0”);
选择一项:
x is greater than 0
x equals 0
x is less than 0
None
反馈
正确答案是:x equals 0

编程

题目1
正确
获得10.00分中的7.00分
未标记标记题目
题干
编写一个Java程序在屏幕上输出1!+2!+3!+……+n!的和。

For example:

Input Result
10`
1!+2!+3!+……+10!= 4037913
Answer:(penalty regime: 10, 20, … %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.

public class jiecheng {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sum1=0;
		for(int i=1;i<=10;i++) {
			int sum2=1;
			for(int j=1;j<=i;j++ ){
				sum2*=j;
			}
			sum1+=sum2;
		}
		System.out.println("1!+2!+3!+……+10!= "+sum1);

	}

}

题目2
正确
获得10.00分中的10.00分
未标记标记题目
题干
利用递归方法求5!。

程序分析:递归公式:f(n)=f(n-1)*4!

import java.util.*;
public class digui {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner gx=new Scanner(System.in);
		int n=gx.nextInt();
		F fum=new F();
		System.out.println(fum.f(n));
	}

}
class F{
	
	public int f(int n) {
		if(n==1) {
			return 1;
		}else {
			return f(n-1)*n;
		}
	}
}

题目3

题干
编写一个Java程序,用if-else语句判断某年份是否为闰年。

For example:

Input Result
2008
2008年是闰年
2010
2010年不是闰年
Answer:(penalty regime: 10, 20, … %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.

import java.util.*;
public class work3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner gx=new Scanner(System.in);
		int n=gx.nextInt();
		if(n%400==0||(n%4==0&&n%100!=0)) {
			System.out.print(n+"年是闰年");
		}else {
			System.out.print(n+"年不是闰年");
		}
	}

}

题目四:
题干
输入某年某月某日,判断这一天是这一年的第几天?

For example:

Input Result
2018-8-5
It is the 217th day of 2018
Answer:(penalty regime: 10, 20, … %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.

import java.util.*;
public class work4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//先将天数按不是闰年算出来,然后判断是否为闰年,在判断月份是否大于2
		Scanner gx=new Scanner(System.in);
		String input=gx.next();
		String[] strs=input.split("-");//使用正则表达式读取年月日
		int year=Integer.parseInt(strs[0]);
		int month=Integer.parseInt(strs[1]);
		int day=Integer.parseInt(strs[2]);
		int whichDay=0;
		int [] monthDay= {31,28,31,30,31,30,31,31,30,31,30,31};
		for(int i=0;i<month-1;i++) {
			whichDay+=monthDay[i];
		}
		whichDay+=day;
		if(month>2) {
			if(year%400==0||(year%4==0&&year%100!=0)) {
				whichDay+=1;
				System.out.print("It is the "+whichDay+"th day of "+year);
			}else {
				System.out.print("It is the "+whichDay+"th day of "+year);
			}
		}else{
			System.out.print("It is the "+whichDay+"th day of "+year);
			
		}
		
		//System.out.println(strs[0]);
		
	}

}

题目5
正确
获得10.00分中的9.00分
未标记标记题目
题干
输出拟n*n乘法口诀(1<=n<=9)。

For example:

Input Result
9
11=1
1
2=2 22=4
1
3=3 23=6 33=9
14=4 24=8 34=12 44=16
15=5 25=10 35=15 45=20 55=25
1
6=6 26=12 36=18 46=24 56=30 66=36
1
7=7 27=14 37=21 47=28 57=35 67=42 77=49
18=8 28=16 38=24 48=32 58=40 68=48 78=56 88=64
19=9 29=18 39=27 49=36 59=45 69=54 79=63 89=72 9*9=81
Answer:(penalty regime: 10, 20, … %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.

import java.util.*;
public class work5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner gx=new Scanner(System.in);
		int n=gx.nextInt();
		//System.out.println(l2);
		for(int i=1;i<=n;i++){
			for(int j=1 ;j<=i;j++) {
				String s1=String.valueOf(j*i);
				String s3=String.format("%-6s", s1);
				System.out.print(j+"*"+i+"="+s3);
			}
			System.out.println();
		}
		//将结果转换为string性
		//得到规定长度的字符串
		//定长输出。
//		String str="测试";
//		String name = String.format("%-12s",str);
//		System.out.println(name+"结果");
	}

}

Question author’s solution:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i=1;i<=n;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j+""+i+"="+ji+" “);
if(j*i<10){System.out.print(” ");}
}
System.out.println();
}
}
}
正确
Marks for this submission: 10.00/10.00. Accounting for previous tries, this gives 9.00/10.00.
题目6
正确
获得10.00分中的9.00分
未标记标记题目
题干

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
For example:

Input Result
ssdfd
数字个数: 0
英文字母个数: 5
空格个数: 0
其他字符个数:0
Answer:(penalty regime: 10, 20, … %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.

import java.util.*;
public class work6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner gx=new Scanner(System.in);
		String s1=gx.nextLine();
		int num=0,letter=0,space=0,others=0;
		for(int i=0;i<s1.length();i++) {
			if('0'<=s1.charAt(i)&&s1.charAt(i)<='9') {
				num++;
			}else if(('a'<=s1.charAt(i)&&s1.charAt(i)<='z')||('A'<=s1.charAt(i)&&s1.charAt(i)<='Z')) {
				letter++;
			}else if(s1.charAt(i)==' ') {
				space++;
			}else{
				others++;
			}
		}
		System.out.print("数字个数: "+num+'\n'+"英文字母个数: "+letter+'\n'+"空格个数: "+space+'\n'+"其他字符个数:"+others);
	}

}

正确
Marks for this submission: 10.00/10.00. Accounting for previous tries, this gives 9.00/10.00.
题目7
正确
获得10.00分中的8.00分
未标记标记题目
题干
输入3个整数a,b,c,按从大到小顺序输出

For example:

Input Result
6 3 9
从大到小的顺序输出:9 6 3
Answer:(penalty regime: 10, 20, … %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.

import java.util.*;
public class work7 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner gx=new Scanner(System.in);
		int []a=new int[3];
		for(int i=0;i<3;i++) {
			a[i]=gx.nextInt();
		}
		Arrays.sort(a);
		int max=a[2],mid=a[1],min=a[0];
		System.out.print("从大到小的顺序输出:"+max+" "+mid+' '+min);	
	}

}
Question author's solution:
import java.util.Scanner;
public class Test { 
	public static void main(String[] args) {
	     Scanner s = new Scanner(System.in);
	     //System.out.println("请输入3个整数:");
	     int a = s.nextInt();
	     int b = s.nextInt(); 
		 int c = s.nextInt();
		if(a < b) {      int t = a;      a = b;      b = t;     } 
		if(a < c) {      int t = a;      a = c;      c = t;     } 
		if(b < c) {      int t = b;      b = c;      c = t;     } 
	System.out.print("从大到小的顺序输出:");
	System.out.println(a + " " + b + " " + c);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值