专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java编程题目及答案

Java编程题目及答案

更新时间:2022-06-16 12:02:59 来源:赢咖4 浏览1150次

1.编写一个Java程序,用if-else语句判断某年份是否为闰年。(分支)

//ProgrammeNameLeapYear.java
publicclassLeapYear{
publicstaticvoidmain(Stringargs[]){
intyear=2010;
if(args.length!=0)
year=Integer.parseInt(args[0]);
if((year%4==0&&year%100!=0)||(year%400==0))
年是闰年。");
else
年不是闰年。");
}
}//if-else语句

2.编写一个Java程序在屏幕上输出1!+2!+3!+ (10)的和。(循环)

//programmenameForTest.java
publicclassForTest{
publicstaticvoidmain(Stringargs[]){
inti,j,mul,sum=0;
for(i=1;i<=10;i++){
mul=1;
for(j=1,j<=i;j++){
mul=mul*j;
}
sum=sum+mul;
}
“1!+2!+3!+……+10!=”+sum);
}
}

3.使用冒泡排序(数组)

publicclass BubbleSort{
publicstaticvoid main(String[]args){ int[]array={63,4,24,1,3,5};
BubbleSortsorter=new BubbleSort();
sorter.sort(array);
}
//冒泡排序
publicvoid sort(int[]array){
for(int i=1;i
for(int j=0;j
if(array[j]>array[j+1]){
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
showArray(array);
}
//遍历数组,并输出数组的元素。
publicvoid showArray(int[]array){
for(int i=0;i
System.out.print(array[i]+"\t");
}
System.out.println();
}
}

4.实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同(字符串)

import
publicclass Register{
String name;
String password;
String newPassword;
///////////
publicvoid nameExe(){
Scannerinput=new Scanner(System.in);
System.out.println("请输入用户名,密码和验证密码");
System.out.print("用户名:");
name=input.next();
System.out.print("密码:");
password=input.next();
System.out.print("验证密码:");
newPassword=input.next();
while(name.length()<3||(password.equals(newPassword)==fa lse)
||(password.length()<6)){
if(name.length()<3){
System.out.println("用户名不能小于3");
}
if((password.equals(newPassword)==false)||password.lengt h()<6){
System.out.println("两次输入密码不一样或密码不能小于6位");
}
System.out.println("\n"+"请重新输入");
System.out.print("用户名:");
name=input.next();
System.out.print("密码:");
password=input.next();
System.out.print("验证密码:");
newPassword=input.next();
}
System.out.println("注册成功!");
}
}
publicclass Verify{
publicstaticvoid main(String[]args){
Registerm1=new Register();
m1.nameExe();
}
}

5.一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购买的门票价格并输出,然后写出测试类测试该类(类的基本实现)

publicclass Tourist{
int age;
int ticketPrice;
publicvoid setAge(int age){
this.age=age;
}
publicvoid ticket(){
if(age>0&&age<12)
ticketPrice=20;
elseif(age<20)
ticketPrice=40;
elseif(age<50)
ticketPrice=80;
else
ticketPrice=35;
System.out.println("门票价格:"+ticketPrice);
}
}/////
import
publicclass Test{
publicstaticvoid main(String[]args){
Scannerinput=new Scanner(System.in);
Touristt1=new Tourist();
System.out.print("请输入年龄:");
t1.setAge(input.nextInt());
t1.ticket();
}
}

6.编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。(异常处理)

importjava.io.*;
publicclassStrinput
{
publicstaticvoidmain(Stringargs[]){
Strings1=null,s2=null,ss,si,sf;
inti1,i2;
floatf1,f2;
BufferedReaderstrin=newBufferedReader(newInputStreamReader(Syste m.in));
try{("输入第一个字符串:");
s1=strin.readLine();
("输入第二个字符串:");
s2=strin.readLine();}
catch(Exceptione){
i1=Integer.parseInt(s1);
i2=Integer.parseInt(s2);
f1=Float.parseFloat(s1);
f2=Float.parseFloat(s2);
ss=strAdd(s1,s2);
si=strAdd(i1,i2);
sf=strAdd(f1,f2);
("输入的二个字符串相加结果为:"+ss);
("输入字符串转换为整数相加结果为:"+si);
("输入字符串转换为浮点数相加结果为:"+sf);
}
staticStringstrAdd(Stringstr1,Stringstr2){
returnstr1+str2;
}
staticStringstrAdd(intint1,intint2){
returnString.valueOf(int1+int2);
}
staticStringstrAdd(floatflt1,floatflt2){
returnString.valueOf(flt1+flt2);
}
}

7.应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/myjava/Hello.java)

//ProgrammeNameFISDemo.java
importjava.io.*;
publicclassFISDemo{
publicstaticvoidmain(Stringargs[]){
byte[]buf=newbyte[2056];
try{
FileInputStreamfileIn=newFileInputStream("e:/myjava/Hello.java") ;
intbytes=fileIn.read(buf,0,2056);
Stringstr=newString(buf,0,bytes);
}catch(Exceptione){
e.printStackTrace();
}
}

8.编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮时,结束程序。(窗体编程)

//ProgrammeNameButtonEventDemo.java
importjavax.swing.*;
import
publicclassButtonEventDemoextendsJPanelimplementsActionListener{ protectedJButtonb1; //声明一个按钮对象
publicButtonEventDemo(){ //构造方法
ImageIconButtonIcon=newImageIcon("images/green.png");//创建按钮的图标对象
b1=newJButton("退出按钮",ButtonIcon); //生成按钮对象
b1.setMnemonic(KeyEvent.VK_E);//设置b1的助记符是Alt+E
b1.setToolTipText("这是退出按钮。"); //设置按钮提示条
this.add(b1);//往面板对象中加载按钮
b1.addActionListener(this);//本类对象注册为按钮的事件监听器
}
publicvoidactionPerformed(ActionEvente){ //按钮事件响应方法
System.exit(0);//按b1则退出主程序
}
privatestaticvoidcreateGUI(){//创建窗体
JFrame.setDefaultLookAndFeelDecorated(true);//设置java隐含观感JFrameframe=newJFrame("按钮测试");//生成应用程序主窗体
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭时隐含操作
ButtonEventDemoCPane=newButtonEventDemo();//生成主类对象--面板
CPane.setOpaque(true);//面板要求不透明
frame.setContentPane(CPane);//设置主类对象为主窗体的内容面板
frame.pack();//主窗体紧缩显示
frame.setVisible(true);//设置主窗体可见
}
publicstaticvoidmain(String[]args){//将createGUI()列入线程
Runnable(){
publicvoidrun(){
createGUI();
}
});
}
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>