计算机等级 > 二级考试 > Java语言程序设计 > 模拟试题 > 文章内容

全国计算机等级考试二级Java模拟试题及答案(29)

2016-11-22编辑:ljnbset
1、编写程序,计算下列分段函数的值。
x? (x>=0)
y=
-x? (x<0)
import java.io.*;
public class testa
{ public static void main(String[] args) throws IOException
{
float? x,y;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x:");
String temp=input.readLine();
x = Float.parseFloat(temp);
if(x>=0)
{
y=x;
}else
{
y=-x;
}
System.out.println("y="+y);
}
}
2、根据年龄,判断某人是否为成年。
import java.io.*;
public class testa
{
public static void main(String[] args) throws IOException
{
int x;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x:");
String temp=input.readLine();
x = Float.parseFloat(temp);
if(x>=18)
{
System.out.println("成年人");
}
if(x<18)
{
System.out.println("未成年");
}
}
}
3、判断2020的奇偶性,并进行输出。
public class test
{
public static void main(String[] args)
{
int x;
x=2020;
if(x%2==0)
System.out.println(+x+"是偶数");
else
System.out.println(+x+"不是偶数");
}
}
4、比较两个数的大小,找出其中的最大数并输出。
import java.io.*;
public class ka
{ public static void main(String[] args) throws IOException
{
float? x,y,m;
m=0;
InputStreamReader readerx=new InputStreamReader(System.in);
BufferedReader inputx=new BufferedReader(readerx);
System.out.println("请输入x:");
String tempx=inputx.readLine();
x = Float.parseFloat(tempx);
InputStreamReader readery=new InputStreamReader(System.in);
BufferedReader inputy=new BufferedReader(readery);
System.out.println("请输入y:");
String tempy=inputy.readLine();
y= Float.parseFloat(tempy);
if(x>y)
{
m=x;
}else
{
m=y;
}
System.out.println("最大数为"+m);
}
}
5、比较两个数的大小,找出其中的最小数并输出。
import java.io.*;
public class ka
{ public static void main(String[] args) throws IOException
{
float? x,y,m;
m=0;
InputStreamReader readerx=new InputStreamReader(System.in);
BufferedReader inputx=new BufferedReader(readerx);
System.out.println("请输入x:");
String tempx=inputx.readLine();
x = Float.parseFloat(tempx);
InputStreamReader readery=new InputStreamReader(System.in);
BufferedReader inputy=new BufferedReader(readery);
System.out.println("请输入y:");
String tempy=inputy.readLine();
y= Float.parseFloat(tempy);
if(x{
m=x;
}else
{
m=y;
}
System.out.println("最小数为"+m);
}
}
6、编写一个Java程序,判断某年份是否为闰年。
import java.io.*;
public class testa
{
public static void main(String[] args) throws IOException
{
float? x;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x:");
String temp=input.readLine();
x =Float.parseFloat(temp);
if(x@0==0)
{
System.out.println(+x+"是闰年");
}
else
if(x%4==0)
{
System.out.println(+x+"是闰年");
}
else{ System.out.println(+x+"不是闰年");}
}
}
7、比较两个数的大小,找出其中的最大数和最小数并输出。
import java.io.*;
public class ka
{ public static void main(String[] args) throws IOException
{
float? x,y;
InputStreamReader readerx=new InputStreamReader(System.in);
BufferedReader inputx=new BufferedReader(readerx);
System.out.println("请输入x:");
String tempx=inputx.readLine();
x = Float.parseFloat(tempx);
InputStreamReader readery=new InputStreamReader(System.in);
BufferedReader inputy=new BufferedReader(readery);
System.out.println("请输入y:");
String tempy=inputy.readLine();
y= Float.parseFloat(tempy);
if(x{
System.out.println("最小数为"+x);
System.out.println("最大数为"+y);
}else
{
System.out.println("最小数为"+y);
System.out.println("最大数为"+x);
}
}
}
8、比较两个数的大小,找出其中的最大数和最小数,并输出最大数和最小数之差。
import java.io.*;
public class ka
{ public static void main(String[] args) throws IOException
{
float? x,y,m;
InputStreamReader readerx=new InputStreamReader(System.in);
BufferedReader inputx=new BufferedReader(readerx);
System.out.println("请输入x:");
String tempx=inputx.readLine();
x = Float.parseFloat(tempx);
InputStreamReader readery=new InputStreamReader(System.in);
BufferedReader inputy=new BufferedReader(readery);
System.out.println("请输入y:");
String tempy=inputy.readLine();
y= Float.parseFloat(tempy);
if(x{
System.out.println("最小数为"+x);
System.out.println("最大数为"+y);
m=y-x;
System.out.println("最大数与最小数之差为"+m);
}else
{
System.out.println("最小数为"+y);
System.out.println("最大数为"+x);
m=x-y;
System.out.println("最大数与最小数之差为"+m);
}
}
}
9、编写程序,计算下列分段函数的值。
x? (x>0)
y=???? 0? (x=0)
-x? (x<0)
import java.io.*;
public class testa
{
public static void main(String[] args) throws IOException
{
float? x,y;
y=0;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x:");
String temp=input.readLine();
x = Float.parseFloat(temp);
if(x>0)
{
y=x;
}
if(x==0)
{
y=0;
}
if(x<0)
{
y=-x;
}
System.out.println("y="+y);
}
}
10、编写程序,计算下列分段函数的值。
x-1? (x>1)
y=???? 0? (-1≤x≤1)
x+10? (x<-1)
import java.io.*;
public class testa
{
public static void main(String[] args) throws IOException
{
float? x,y;
y=0;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x:");
String temp=input.readLine();
x = Float.parseFloat(temp);
if(x>1)
{
y=x-1;
}
if(1.0>=x&&x>=-1.0)
{
y=0;
}
if(x<-1)
{
y=x+10;
}
System.out.println("y="+y);
}
}
全国计算机等级考试二级Java模拟试题及答案(28)

热点推荐

登录注册
触屏版电脑版网站地图