实验二docWord文档格式.docx
《实验二docWord文档格式.docx》由会员分享,可在线阅读,更多相关《实验二docWord文档格式.docx(27页珍藏版)》请在冰点文库上搜索。
个水仙花数:
"
+n);
n++;
}
}
}
实验结果
图1-4IsNarcissus类的运行结果
例1-5源程序
importjava.io.*;
publicclassJavaAppShellInout
System.out.println("
大家好,希望大家早成高手!
);
Strings="
;
System.out.print("
请输入你的名字:
try{
BufferedReaderin=
newBufferedReader(newInputStreamReader(System.in));
s=in.readLine();
catch(IOExceptione){};
你好,"
+s+"
,欢迎你!
charc='
'
System.out.print("
请输入一个字符:
try{
c=(char)System.in.read();
你刚输入的字符是:
+c);
System.out.println(MyClass.myMessage);
classMyClass
staticStringmyMessage="
大家好,这是另一个类中的信息。
图1-5JavaAppShellInout类的运行结果
例1-6源程序
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.ImageIcon;
publicclassJavaAppGraphicsInOutextendsFrame
implementsActionListener
Labelprompt;
TextFieldinput,output;
Buttonbtn;
ImagemyImage;
JavaAppGraphicsInOut()
super("
图形界面的JavaApplication程序"
prompt=newLabel("
输入下图的名字:
input=newTextField(6);
output=newTextField(20);
btn=newButton("
关闭"
myImage=newImageIcon("
lfg.jpg"
).getImage();
setLayout(newFlowLayout());
add(prompt);
add(input);
add(output);
add(btn);
input.addActionListener(this);
btn.addActionListener(this);
setSize(420,350);
setVisible(true);
publicvoidactionPerformed(ActionEvente)
if(e.getSource()==input)
output.setText("
欢迎你访问"
+input.getText()+"
网站!
else
{
System.exit(0);
publicvoidpaint(Graphicsg)
g.drawImage(myImage,60,100,this);
publicstaticvoidmain(Stringar[])
newJavaAppGraphicsInOut();
}
图1-6JavaAppGraphicsInOut类的运行结果
例1-7源程序
importjava.applet.*;
publicclassJavaAppletInOutextendsAppletimplementsActionListener
publicvoidinit()
myImage=getImage(getDocumentBase(),"
最美的祝福.jpg"
prompt=newLabel("
请输入你的名字"
input=newTextField(8);
output=newTextField(25);
setSize(400,400);
publicvoidactionPerformed(ActionEvente)
output.setText(input.getText()+"
publicvoidpaint(Graphicsg)
g.drawString("
好好学习,天天向上!
20,60);
g.drawImage(myImage,30,80,this);
图1-7JavaAppletInOut类的运行结果
例1-8源程序
importjava.util.*;
publicclassTestDtypeAndProcess
Processp=newProcess();
求10000以内完全数——"
p.isPerfectNum();
\n猜数小游戏——"
p.GuessNumber();
\n求16/16*8效率最高的算法——"
p.CaculateNumber();
\n输出字母表——"
p.ShowLetters();
\n测试运算符优先级——"
p.testPrecedence();
\n测试逻辑运算符——"
p.testLogic();
\n测试利用异或运算符进行加密解密——"
p.Coding();
classProcess
voidisPerfectNum()
for(inti=1;
i<
10000;
i++)
inty=0;
for(intj=1;
j<
i;
j++)
if(i%j==0)
y+=j;
if(y==1)
{
System.out.println("
10000以内的完全数有:
+i);
}
voidGuessNumber()
System.out.println("
请输入一个数字:
intlintTargetNum=30;
Scannerscanner=newScanner(System.in);
while(true)
intlintGuessNumber=scanner.nextInt();
if(lintGuessNumber>
lintTargetNum)
System.out.println("
太大了!
continue;
if(lintGuessNumber<
太小了!
祝贺你猜对了!
break;
voidCaculateNumber()
intlintNumber=16;
System.out.println(lintNumber>
>
3);
System.out.println(lintNumber<
<
voidShowLetters()
for(charstart='
a'
start<
='
z'
start++)
System.out.println(start+"
"
voidtestLogic()
intx,y=10;
if(((x=0)==1)&
&
((y=20)==20))
intx,y=10,if(((x=0)==1)"
+"
((y=20)==20)),现在y的值是="
+y);
else
现在y的值是:
inta,b=10;
if(((a=0)==0)|((b=20)==20))
inta,b=10;
if(((a=0)==0)|"
((b=20)==20)),现在b的值是:
+b"
voidtestPrecedence()
inta=1,c=6;
a=1,c=6,(-++a+c)="
+(-++a+c)+"
a="
+a);
inte;
floatf;
doubled,d1;
e=5/2;
f=5/2;
d=5/2;
d1=5/2.0;
inte=5/2="
+e);
floatf=5/2="
+f);
doubled=5/2="
+d+"
doubled1=5/2.0="
+d1);
inti=4;
i=4;
c*-i="
+c*-i);
intm=2,n=3,x=4,y=5;
intm=2,n=3,x=4,y=5;
n<
m?
x:
y"
+(n<
y));
voidCoding()
charj1='
施'
charch1='
珺'
chars='
9'
j1=(char)(j1^s);
ch1=(char)(ch1^s);
密文是:
+j1+ch1);
原文是:
汉字珺在unicode码中的顺序位置是"
+(int)ch1);
运行结果
图1-8TestDtypeAndProcess类的运行结果
例1-9JavaApplication源程序
importjavax.swing.*;
publicclassAppGraphicsAdvence
newFrameInOut();
classFrameInOutextendsFrameimplementsActionListener
JButtonbtn1,btn2,btn3,btn4;
JTextAreata,ta1;
JPanelp1,p2,p3;
Stringcourses[]={"
数据结构"
"
毛概"
物理"
概率论"
英语"
体育"
};
intscores[]={80,85,93,86,81,93};
FrameInOut()
学生基本信息及课程成绩信息!
this.setFont(newFont("
隶体"
Font.BOLD,100));
this.setBackground(Color.pink);
btn1=newJButton("
学生基本信息"
btn2=newJButton("
初始成绩"
btn3=newJButton("
成绩排序"
btn4=newJButton("
退出"
ta=newJTextArea(10,25);
ta1=newJTextArea(10,25);
p1=newJPanel();
p2=newJPanel();
p3=newJPanel();
p1.add(btn1);
p1.add(btn2);
p1.add(btn3);
p1.add(btn4);
p2.add(ta);
p3.add(ta1);
add(p1);
add(p2);
add(p3);
p1.setBackground(Color.red);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
setSize(600,360);
Stringcourses1[]={"
a"
b"
c"
d"
e"
f"
for(inti=0;
courses.length;
courses1[i]=courses[i];
intscores1[]={2,2,2,2,2,2};
for(intj=0;
scores.length;
scores1[j]=scores[j];
if(e.getSource()==btn1)
ta.setText(null);
ta.setForeground(Color.blue);
ta.setFont(newFont("
Font.BOLD,14));
ta.append("
姓名:
赵婷\n"
性别:
女\n"
籍贯:
四川南充\n"
班级:
计091\n"
学号:
110911137"
if(e.getSource()==btn2)
ta1.setText(null);
ta1.setForeground(Color.black);
ta1.setFont(newFont("
楷体"
Font.BOLD,16));
for(inti=0;
ta1.append("
课程名"
+courses[i]+"
成绩为"
+scores[i]+"
\n"
if(e.getSource()==btn3)
scores1.length-1;
for(intj=i+1;
scores1.length;
intt;
Stringcoursename;
if(scores1[i]>
scores1[j])
t=scores1[i];
scores1[i]=scores1[j];
scores1[j]=t;
coursename=courses1[i];
courses1[i]=courses1[j];
courses1[j]=coursename;
ta1.setForeground(Color.red);
courses1.length;
课程名:
+courses1[i]+"
成绩为:
+scores1[i]+"
if(e.getSource()==btn4)
dispose();
System.exit(0);
图1-91AppGraphicsAdvence类的初始运行界面
图1-92AppGraphicsAdvence类显示学生基本信息界面
图1-93AppGraphicsAdvence类显示初始成绩信息界面
图1-94AppGraphicsAdvence类显示排序后的成绩信息界面
例1-10JavaApplet源程序
publicclassAppletGraphicsAdvenceextendsAppletimplementsActionListener
JButtonbtn1,btn2,btn3;
publicvoidinit()
ta=newJTextArea(8,20);
ta1=newJTextArea(8,20);
setSize(300,460);
ta.setFont