二十个JAVA程序代码.docx
《二十个JAVA程序代码.docx》由会员分享,可在线阅读,更多相关《二十个JAVA程序代码.docx(38页珍藏版)》请在冰豆网上搜索。
二十个JAVA程序代码
1百分制分数到等级分数
packagepm;
publicclassSwitchTest{
//编写程序,实现从百分制分数到等级分数的转换
//
//>=90A
//80~89B
//70~79C
//60~69D
//<60E
publicstaticvoidmain(String[]args){
ints=87;
switch(s/10){
case10:
System.out.println("A");break;
case9:
System.out.println("A");break;
case8:
System.out.println("B");break;
case7:
System.out.println("c");break;
case6:
System.out.println("D");break;
default:
System.out.println("E");break;
}
}
}
2成法口诀阵形
packagepm;
publicclassSwitchTest{
publicstaticvoidmain(String[]args){
for(inti=1;i<=9;i++){
for(intj=1;j<=i;j++){
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
3华氏和摄氏的转换法
packagepm;
importjava.util.Scanner;
publicclassSwitchTest{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
while(true){
System.out.println("请输入要转换的温度类型:
C或F");
Strings=sc.next().trim();
if("c".equalsIgnoreCase(s)){
//做摄氏向华摄的转换
System.out.println("请输入要转换摄氏的温度:
..");
doubledb=sc.nextDouble();
doubledb2=(db*9/5)+32;
System.out.println("对应的华氏温度:
"+db2+"F");
}elseif("f".equalsIgnoreCase(s)){
//做华摄向摄氏的转换
System.out.println("请输入要转换华氏的温度:
..");
doubledb=sc.nextDouble();
doubledb2=(db-32)*5/9;
System.out.println("对应的摄氏温度:
"+Math.round(db2)+"C");
}elseif("exit".equalsIgnoreCase(s)){
break;
}
}
}
}
packagepm;
importjava.util.Scanner;
publicclassSwitchTest{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
booleanflag=true;
while(flag){
System.out.println("请输入要转换的温度,如:
50c或100f");
Stringstr=sc.nextLine().trim();
if(str.endsWith("c")||str.endsWith("C")){
//做摄氏向华摄的转换30c
Stringst=str.substring(0,str.length()-1);
doubledb=Double.parseDouble(st);//[0,2)
//2doubledb=Double.valueOf(st).doubleValue();
doubledb2=(db*9/5)+32;
System.out.println("对应的华氏温度:
"+db2+"F");
}elseif(str.endsWith("f")||str.endsWith("F")){
//做华摄向摄氏的转换
Stringst=str.substring(0,str.length()-1);
doubledb=Double.parseDouble(st);//[0,2)
//2doubledb=Double.valueOf(st).doubleValue();
doubledb2=(db-32)*5/9;
System.out.println("对应的摄氏温度:
"+Math.round(db2)+"C");
}elseif("exit".equalsIgnoreCase(str)){
flag=false;
}
}
}
}
4三个数的最大数
packagepm;
publicclassSwitchTest{
publicstaticvoidmain(String[]args){
inta=1,b=2,c=3,d=0;
d=a>b?
a:
b;
d=a>b?
(a>c?
a:
c):
(b>c?
b:
c);
System.out.println("最多数为:
"+d);
}
}
5简单计算器的小程序
packageone;
importjava.awt.BorderLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
publicclassJsqimplementsActionListener{
privateJFrameframe;
privateJButton[]bus;
privateJTextFieldjtx;
privateJButtonbu;
privatechar[]strs;
privateStringd_one="";
privateStringoperator;
publicstaticvoidmain(String[]args){
newJsq();
}
/*利用构造进行实例化*/
publicJsq(){
frame=newJFrame("计算器");
jtx=newJTextField(14);
bus=newJButton[16];
strs="789/456*123-0.+=".toCharArray();
for(inti=0;i<16;i++){
bus[i]=newJButton(strs[i]+"");
bus[i].addActionListener(this);
}
bu=newJButton("C");
bu.addActionListener(this);
init();
}
/*GUI初始化*/
publicvoidinit(){
JPaneljp1=newJPanel();
jp1.add(jtx);
jp1.add(bu);
frame.add(jp1,BorderLayout.NORTH);
JPaneljp2=newJPanel();
jp2.setLayout(newGridLayout(4,4));
for(inti=0;i<16;i++){
jp2.add(bus[i]);
}
frame.add(jp2,BorderLayout.CENTER);
frame.pack();
frame.setLocation(300,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*事件的处理*/
publicvoidactionPerformed(ActionEventarg0){
/*获取输入字符*/
Stringconn=arg0.getActionCommand();
/*清除计算器内容*/
if("C".equals(conn)){
d_one="";
operator="";
jtx.setText("");
return;
}
/*暂未实现该功能*/
if(".".equals(conn)){
return;
}
/*记录运算符,保存运算数字*/
if(("+-*/".indexOf(conn))!
=-1){
if("".equals(d_one)&&"".equals(jtx.getText()))return;
d_one=jtx.getText();
operator=conn;
jtx.setText("");
return;
}
/*计算结果*/
if("=".equals(conn)){
if("".equals(d_one)&&"".equals(jtx.getText()))return;
doubledb=0;
if("+".equals(operator)){
db=Double.parseDouble(d_one)
+Double.parseDouble(jtx.getText());
jtx.setText(db+"");
}
if("-".equals(operator)){
db=Double.parseDouble(d_one)
-Double.parseDouble(jtx.getText());
jtx.setText(db+"");
}
if("*".equals(operator)){
db=Double.parseDouble(d_one)
*Double.parseDouble(jtx.getText());
jtx.setText(db+"");
}
if("/".equals(operator)){
db=Double.parseDouble(d_one)
/Double.parseDouble(jtx.getText());
jtx.setText(db+"");
}
d_one=db+"";
return;
}
//界面显示
jtx.setText(jtx.getText()+conn);
}
}
6三角形图案
packagepm;
publicclassSwitchTest{
publicstaticvoidmain(String[]args){
intn=5;
for(inti=0;i<=n;i++){
for(intj=0;jSystem.out.print("");
}
for(intk=0;k<2*i-1;k++){
System.out.print("*");
}
System.out.println();
}
}
}
7输出输入的姓名
packagepm;
importjava.util.Scanner;
publicclassSwitchTest{
publicstaticvoidmain(String[]args){
Stringname=null;
Scannersca=newScanner(System.in);
charfirstChar;
do{
System.out.println("pleaseinputyourname");
name=sca.nextLine();
firstChar=name.charAt(0);
}while(!
(firstChar>='a'&&firstChar<='z'||
firstChar>='A'&&firstChar<='Z'));
System.out.println("OK!
yournameis:
"+name);
}
}
8一小时倒计时小程序
packagepm;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
publicclassSwitchTest{
privateJFrameframe;
privateJLabeljl1;
privateJLabeljl2;
privateJLabeljl3;
/*主方法*/
publicstaticvoidmain(String[]args){
newSwitchTest().getTime();
}
/*倒计时的主要代码块*/
privatevoidgetTime(){
longtime=1*3600;
longhour=0;
longminute=0;
longseconds=0;
while(time>=0){
hour=time/3600;
minute=(time-hour*3600)/60;
seconds=time-hour*3600-minute*60;
jl1.setText(hour+"时");
jl2.setText(minute+"分");
jl3.setText(seconds+"秒");
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
time--;
}
}
/*构造实现界面的开发GUI*/
publicSwitchTest(){
frame=newJFrame("倒计时");
jl1=newJLabel();
jl2=newJLabel();
jl3=newJLabel();
init();
}
/*组件的装配*/
privatevoidinit(){
JPaneljp=newJPanel();
jp.add(jl1);
jp.add(jl2);
jp.add(jl3);
frame.add(jp);
frame.setVisible(true);
frame.setLocation(300,400);
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
9棋盘图案
publicclassSjx{
publicstaticvoidmain(String[]args){
intSIZE=19;
for(inti=0;iif(i<10){
System.out.print(""+i);//两个空格
}else{
System.out.print(""+(char)(i+'a'-10));//两个空格
}
}
System.out.println();
//System.out.print(i<10?
(char)(i+'0'):
(char)(i+'a'-10));
for(inti=0;iif(i<10){
System.out.print(i+"");//一个空格
}else{
System.out.print((char)(i+'a'-10)+"");//一个空格
}
for(intj=0;jSystem.out.print("*"+"");//两个空格
}
System.out.println();
}
}
}
10数组输出唐诗
packageday04;
publicclassArrayTest{
publicstaticvoidmain(String[]args){
char[][]arr=newchar[4][7];
Strings="朝辞白帝彩云间千里江陵一日还两岸猿声啼不住轻舟已过万重山";
for(inti=0;ifor(intj=0;jarr[i][j]=s.charAt(i*7+j);
}
}
for(inti=0;ifor(intj=0;jSystem.out.print(arr[arr.length-j-1][i]+"");
}
System.out.println();
}
}
}
11找出满足条件的最小数
packageday02;
publicclassFangk{
publicstaticvoidmain(String[]args){
//for(inti=1000;i<=9999;i++){
//intq=i/1000;
//intb=i/100%10;
//ints=i/10%10;
//intg=i%10;
//if(q>b&&s>g&&q+g==b+s&&b%2!
=0){
//System.out.println("TheMinNumberis:
"+i);
//break;
//}
//}
loop1:
for(intq=1;q<=9;q++){
loop2:
for(intb=0;b<=9;b++){
if(b%2==0){
continueloop2;
}
for(ints=0;s<=9;s++){
for(intg=0;g<=9;g++){
if(q>b&&s>g&&q+g==b+s){
System.out.println("TheMinNumberis:
"+(q*1000+b*100+s*10+g));
breakloop1;
}
}
}
}
}
}
}
12判断一个数是否是素数
packageday02;
publicclassFangk{
publicstaticvoidmain(String[]args){
intnum=14;
booleanflag=true;
for(inti=2;i<=num/2;i++){
if(num%i==0){
flag=false;
break;
}
}
if(flag){
System.out.println(num+"isaprimenumber!
");
}else{
System.out.println(num+"isnotaprimenumber!
");
}
}
}
//////////////////////////////////////////////////////////////////////
packageday04;
importjava.util.Scanner;
publicclassA1{
publicstaticvoidmain(String[]args){
intn;
Scannersca=newScanner(System.in);
System.out.println("pleaseinputanumber:
");
n=sca.nextInt();
if(isPrimeNumber(n)){
System.out.println(n+"isaprimenumber!
");
}else{
System.out.println(n+"isnotaprimenumber!
");
}
}
publicstaticbooleanisPrimeNumber(intn){
for(inti=2;i<=n/2;i++){
if(n%i==0){
returnfalse;
}
}