2024年1月25日发(作者:)
JAVA编程习题及答案_完全版
10001显示短句
//程序填空.
//在屏幕上显示一个短句“Programming in Java is fun!”
import .*;
public class Test10001{
public static void main(String args[]){
/*------------------------*/
n("Programming in Java is fun!"); }
}
10002显示网格
/*程序填空。
在屏幕上显示如下网格。
+---+---+
| | |
| | |
+---+---+
*/
import .*;
public class Test10002{
public static void main(String args[]){
/*------------------------*/
n("+---+---+");
n("| | |");
n("| | |");
n("+---+---+");
}
}
10095显示hello world!
import .*;
public class Test10095{
public static void main(String args[]){
/*---------------------------*/
n("Hello world!"); }
}
10097求两个数的和
/*输入n表示做n次下面的操作:
输入两个正整数,输出它们的和.
例:
输入:
2 //表示n=2,做两次操作
10 11
20 30
输出:
sum=21
sum=50
*/
import r;
public class Test10097{
public static void main(String args[]){ int n,a,b,i,sum;
Scanner in=new Scanner();
n=t(); //输入n
for(i=1; i<=n; i++){
/*------------*/
a=t();
b=t();
sum=a+b;
n("sum="+sum);
}
}
}
20001求两个数的和与差
/*程序填空,不要改变与输入输出有关的语句。
输入整数a和b,计算并输出a、b的和与差。
例:
输入
2 -8
输出
The sum is -6
The difference is 10*/
import .*;
import r;
public class Test20001{
public static void main(String args[]){
int a, b, sum, diff;
Scanner in=new Scanner();
a=t();
b=t();
/*------------------*/
sum=a+b;
diff=a-b;
n("The sum is "+sum);
n("The difference is "+diff); }
}
20002求平方根
//程序填空,不要改变与输入输出有关的语句。
输入1个实数x,计算并输出其平方根。
例:
输入
1.21
输出
The square root of 1.21 is 1.1
import .*;
import r;
public class Test20002{
public static void main(String args[]){
double x, root;
Scanner in=new Scanner();
x=uble();
/*------------------*/
root=(x);
n("The square root of "+x+" is "+root);
}
}
20003华氏温度转换成摄氏温度
/*程序填空,不要改变与输入输出有关的语句。
输入华氏温度f,计算并输出相应的摄氏温度c。c = 5/9(f-32).
例:括号是说明
输入
17.2 (华氏温度)
输出
The temprature is -8.222222222222223*/
import r;
public class Test20003 {
public static void main(String[] args) {
Scanner in=new Scanner();
double f, c;
f=uble();
/*------------------*/
c=5*(f-50)/9+10;
n("The temprature is "+c);
}
}
20004计算旅途时间
程序填空,不要改变与输入输出有关的语句。
输入2个整数time1和time2,表示火车的出发时间和到达时间,计算并输出旅途时间。有效的时间围是0000到2359,不需要考虑出发时间晚于到达时间的情况。
例:括号是说明
输入
712 1411(出发时间是7:10,到达时间是14:11)
输出
The train journey time is 6 hrs 59 mins.
import r;
public class Test20004 {
public static void main(String[] args) {
Scanner in=new Scanner();
int time1, time2, hours, mins;
time1=t();
time2=t();
/*------------------*/
time1=(time1/100)*60+time1%100;
time2=(time2/100)*60+time2%100;
hours=(time2-time1)/60;
mins=(time2-time1)-hours*60;
n("The train journey time is "+hours+" hrs
"+ mins+" mins.");
}
}
20005数字加密
程序填空,不要改变与输入输出有关的语句。
输入1个四位数,将其加密后输出。
方法是将该数每一位上的数字加9,
然后除以10取余,做为该位上的新数字,
最后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数。例:括号是说明
输入
1257
输出
The encrypted number is 4601(每一位上的数字加9除以10取余后,得0146,交换后得到4601)
import r;
public class Test20005 {
public static void main(String[] args) {
Scanner in=new Scanner();
int number, digit1, digit2, digit3, digit4, newnum;
int temp; //我自己定义了一个变量
number=t();
/*------------------*/
digit1=number/1000;
digit2=(number%1000)/100;
digit3=((number%1000)%100)/10;
digit4=number%10;
//以下是测试语句,看看我们的想法是不是正确.但是作业在提交时一定要把它注释掉
//("
"+digit4);
//-----------------------------------
digit1=(digit1+9)%10;
"+digit1+" "+digit2+" "+digit3+"
digit2=(digit2+9)%10;
digit3=(digit3+9)%10;
digit4=(digit4+9)%10;
//---------------------------
temp=digit1;
digit1=digit3;
digit3=temp;
//----------------------------
temp=digit2;
digit2=digit4;
digit4=temp;
//----------------------------------
newnum=digit1*1000+digit2*100+digit3*10+digit4;
n("The encrypted number is "+newnum);
}
}
20006大小写字母转换
程序填空,不要改变与输入输出有关的语句。
输入一个大写英文字母,输出相应的小写字母。
例:
输入
G
输出
g
import .*;
public class T20006 {
public static void main(String[] args) throws IOException
{
char ch;
ch=(char)();
/*------------------*/
ch=(char)(ch+32); // 以ASCII码计算
n(ch);
}
}
20007计算三门课程的平均成绩
程序填空,不要改变与输入输出有关的语句。
已知某位学生的数学、英语和计算机课程的成绩分别是87分、72分和93分,求该生3门课程的平均分。
本题无输入。
输出示例:
math = 87, eng = 72, comp = 93
average = 84
public class test20007 {
public static void main(String[] args) {
int math, eng, comp, average;
/**********/
math=87;
eng=72;
comp=93;
average=(math+eng+comp)/3;
n("math = "+math+", eng = "+eng+",
comp = "+comp);
n("average = "+ average);
}
}
20008计算存款利息
程序填空,不要改变与输入输出有关的语句。
输入存款金额 money、存期 year 和年利率 rate,
根据下列公式计算存款到期时的利息 interest(税前),输出时保留
2位小数。
interest = money(1+rate)^year - money
输入输出示例:括号为说明
输入
1000 3 0.025 (money = 1000, year = 3, rate = 0.025)
输出
interest = 76.89
import r;
public class test20008 {
public static void main(String[] args) {
int money, year;
double interest, rate;
Scanner in=new Scanner();
/*使用uble()和t()输入double和int型数据*/
/**********/
money=t();
year=t();
rate=uble();
interest=money*((1+rate),year)-money;
n("interest
(int)(interest*100+0.5)/100.0); }
}
30001显示两级成绩
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
输入一个学生的数学成绩,如果它低于60,输出“Fail”,否则,输出“Pass”。输出使用n("Fail");
n("Pass");及= "+
例:括号是说明
输入
2 (repeat=2)
60 59
输出
Pass
Fail
import r;
public class Test30001{
public static void main(String[] args){
int ri, repeat;
int mark;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
mark=t();
if (mark>=60)
n("Pass");
else
n("Fail");
/*------------------*/
}
}
}
30002找最小值
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0 p="">
例:括号是说明
输入
3 (repeat=3)
12 6 1 90
10 40 30 20
-1 -3 -4 -5
输出
min is 1 (12 6 1 90 中的最小值是1)
min is 10 (10 40 30 20 中的最小值是10)
min is -5 (-1 -3 -4 -5中的最小值是-5)
import r;
public class Test30002 {
public static void main(String[] args){
int ri, repeat;
int a, b, c, d, min;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
a=t();
b=t();
c=t();
d=t();
/*------------------*/
min=a;
if(min>b)min=b;
if(min>c)min=c;
if(min>d)min=d;
n("min is "+min);
}
}
}
30003求三角形的面积和周长
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
输入三角形的三条边a, b, c,如果能构成一个三角形,输出面积area和周长perimeter;否则,输出“These sides do not
correspond to a valid triangle”。
在一个三角形中,任意两边之和大于第三边。
三角形的面积计算公式:
aere*area = s(s-a)(s-b)(s-c)
其中:s = (a+b+c)/2
输出代码:
n("area="+area+";perimeter="+perimeter);
n("These sides do not correspond to a valid
triangle");
例:括号是说明
输入
2 (repeat=2)
5 5 3
1 1 4
输出
area=7.154544;perimeter=13.0
These sides do not correspond to a valid triangle
import r;
public class Test30003 {
public static void main(String[] args){
int ri, repeat;
float a, b, c, area, perimeter, s;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
a=oat();
b=oat();
c=oat();
/*------------------*/
if (a+b>c&&a+c>b&&b+c>a)
{
perimeter=a+b+c;
s = (a+b+c)/2.0f;
area =(float)(s*(s-a)*(s-b)*(s-c)) ;
n("area="+area+";perimeter="+perimeter);
}
else
n("These sides do not correspond to a valid
triangle");
}
}
}
30004判断数的符号
/*输入一个正整数repeat (0
输入整数x,若x大于0,y=1;若x等于0,y=0;否则,y=-1,最后输出y。
例:括号是说明
输入
3 (repeat=3)
2 -8 0
输出
1 (x=2时y=1)
-1 (x=-8时y=-1)
0 (x=0时y=0)
*/
import r;
public class Test30004{
public static void main(String[] args){
int ri, repeat;
int x, y=0;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
x=t();
/*------------------*/
if (x>0)
y=1;
else if (x==0)
y=0;
else if (x<0)
y=-1;
n(y);
}
}
}
30005计算个人所得税
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0 bdsfid="487" p="">
tax = rate * (salary-850)
当 salary <= 850 时,rate = 0%;
当 850 < salary <= 1350 时,rate = 5%;
当 1350 < salary <= 2850 时,rate = 10%;
当 2850 < salary <= 5850 时,rate = 15%;
当 5850 < salary 时,rate = 20%;
例:括号是说明
输入
5 (repeat=5)
1010.87
32098.76
800
4010
2850
输出
tax=8.04
tax=6249.75
tax=0.0
tax=474.0
tax=200.0
import r;
public class Test30005 {
public static void main(String[] args){
int ri, repeat;
float rate, salary, tax;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
salary=oat();
/*------------------*/
if(salary<=850)
tax =0;
else if(salary<=1350)
{ rate=0.05f;
tax=rate*(salary-850);
}
else if(salary<=2850)
{
rate=0.10f;
tax=rate*(salary-850);
}
else if(salary<=5850)
{
rate=0.15f;
tax=rate*(salary-850);
}
else
{
rate=0.20f;
tax=rate*(salary-850);
}
n("tax="+(int)(tax*100+0.5)/100.0);
}
}
}
30006显示水果的价格
/*以下4种水果的单价分别是3.00元/公斤,2.50元/公斤,4.10元/公斤,10.20元/公斤。
[1] apples
[2] pears
[3] oranges
[4] grapes
输入水果的编号,输出该水果的单价。如果输入不正确的编号,显示单价为0。
例:括号是说明
输入
1 (repeat=1)
3 (oranges的编号)
输出
[1] apples
[2] pears
[3] oranges
[4] grapes
price=4.1
*/
import r;
public class Test30006{
public static void main(String[] args){
int ri, repeat;
int choice;
float price;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
n("[1] apples");
n("[2] pears");
n("[3] oranges");
n("[4] grapes");
choice=t();
/*------------------*/
if (choice==1)
price=3f;
else if(choice==2)
price=2.5f;
else if (choice==3)
price=4.1f;
else if (choice==4)
price=10.2f;
else
price=0;
n("price="+price);
}
}
}
30007字母转换
程序填空,不要改变与输入输出有关的语句。
输入一批以问号“?”结束的字符,对“?”以前的每一个字符,如果它是大写字母,输出相应的小写字母;如果它是小写字母,输出相应的大写字母;否则,原样输出。
例:
输入
F=y?
输出
f=Y
import .*;
public class Test30007 {
public static void main(String[] args)throws IOException{
char ch;
ch=(char)();
while(ch!='?')
{
/*---------------------*/
if(ch>='A'&&ch<='Z')
ch=(char)(ch+32);
else if((ch>='a'&&ch<='z'))
ch=(char)(ch-32);
(ch);
ch=(char)();
}
}
}
40001求1+1/2+1/3+……1/n
/*程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
读入1 个正整数 n(n<=100),计算并输出1+1/2+1/3+……+1/n 。例:括号是说明
输入
2 (repeat=1)
2
10
输出
1.5
2.9289684*/
import r;
public class Test40001 {
public static void main(String[] args) {
int ri, repeat;
int i, n;
float sum;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
n=t();
/*--------------------*/
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+(float)1/i;
}
n(sum);
}
}
}
40002求n!
/*程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
例:括号是说明
输入
3 (repeat=2)
1
10
5
输出
1.0
3628800.0
120.0*/
import r;
public class Test40002 {
public static void main(String[] args) {
int ri, repeat;
int i, n;
double fact;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
n=t();
/*--------------------*/
fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
n(fact);
}
}
}
40003求x的n次幂
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
读入1 个正实数x和1个正整数 n(n<=50),计算并输出x的n次幂。
例:括号是说明
输入
2 (repeat=2)
1.5 2
2.0 10
输出
2.25
1024.0
import r;
public class Test40003 {
public static void main(String[] args) {
int ri, repeat;
int i, n;
double x, mypow;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
x=uble();
n=t();
/*--------------------*/
mypow=1.0;
for(i=1;i<=n;i++)
{
mypow=mypow*x;
}
n(mypow);
}
}
}
40004求1-1/2+1/3-1/4+……
/*读入1 个正整数 n(n<=100),
计算并输出1-1/2+1/3-1/4+……的前n项和(四舍五入保留小数4位)。
例:括号是说明
输入
2 (repeat=2)
10
3
输出
0.6456
0.8333*/
import r;
public class Test40004 {
public static void main(String[] args) {
int ri, repeat;
int i, n, flag;
float sum;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
n=t();
/*--------------------*/
sum=0;
flag=1;
for(i=1;i<=n;i++)
{sum=sum+(float)flag/i;
flag=-flag;}
n((long)(sum*10000+0.5)/10000.);
}
}
}
24.求1+1/3+1/5+1/7+……
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
读入1 个正整数 n(n<=100),计算并输出1+1/3+1/5+1/7+……的前n项和。例:括号是说明
输入
2 (repeat=2)
10
3
输出
2.133256
1.5333334
import r;
public class Test40005 {
public static void main(String[] args) {
int ri, repeat;
int i, n, temp;
float sum;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
n=t();
/*--------------------*/
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+1.0f/(2*i-1);
}
n(sum);
}
}
}
25.求1-1/3+1/5-1/7+……
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0
读入1 个正实数eps,计算并输出1-1/3+1/5-1/7+……,
直到最后一项的绝对值小于eps为止(要求每一项的绝对值均大于等于eps,并以float类型输出数据)。
例:括号是说明
输入
2 (repeat=2)
1E-4
0.1
输出
0.7853482
0.83492064
import r;
public class Test40006 {
public static void main(String[] args) {
int ri, repeat;
int temp, flag; //temp变量干什么用?
int i; //自己加上的变量
double eps, item, sum;
Scanner in=new Scanner();
repeat=t();
for(ri=1; ri<=repeat; ri++){
eps=uble();
本文发布于:2024-01-25 20:36:10,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1706186170248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |