Java基础程序编写(超详细超全)

2025-03-12 10:53:48

1、1百分制分数到等级分数packagepm;publicclassSwitchTest{//编写程序,实现从百分制分数到等级分数的转换////>=90A//80~89B//70~79C//60~69D//<60Epublicstaticvoidmain(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;}}}

Java基础程序编写(超详细超全)

2、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();}}}

Java基础程序编写(超详细超全)

3、3华氏和赢悲苍璩摄氏的转换法packagepm;importjava.util.Scanner;publicclassSwitchTest{publicstaticvoidmai荏鱿胫协n(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")){//做摄氏向华摄的转换30cStringst=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;}}}}

Java基础程序编写(超详细超全)

4、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、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、6三角形图案packagepm;publicclassSwitchTest{publicstaticvoidmain(String[]args){intn=5;for(inti=0;i<=n;i++){for(intj=0;j<n-i;j++){System.out.print("");}for(intk=0;k<2*i-1;k++){System.out.print("*");}System.out.println();}}}

7、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、8一小时倒计时小程碌食撞搁序packagepm;importjavax.swing.JFrame;importjavax.swing.J讣嘬铮篌Label;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、9棋盘图案publicclassSjx{publicstaticvoidmain(String[]args){intSIZE=19;for(inti=0;i<SIZE;i++){ if(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;i<SIZE;i++){if(i<10){System.out.print(i+"");//一个空格 }else{System.out.print((char)(i+'a'-10)+"");//一个空格}for(intj=0;j<SIZE;j++){System.out.print("*"+"");//两个空格}System.out.println();}}}

10、10数组输出唐诗packageday04;publicclassArrayTest{publicstaticvoidmain(String[]args){ char[][]arr=newchar[4][7]; Strings="朝辞白帝彩云间千里江陵一日还两岸猿声啼不住轻舟已过万重山"; for(inti=0;i<arr.length;i++){ for(intj=0;j<arr[i].length;j++){ arr[i][j]=s.charAt(i*7+j); } } for(inti=0;i<arr[0].length;i++){ for(intj=0;j<arr.length;j++){ System.out.print(arr[arr.length-j-1][i]+""); } System.out.println(); }}}

11、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、12幼榍嘈酾判断一个数是否是素数packageday02;publicclassFangk{publicstati艘早祓胂cvoidmain(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;} }returntrue;}}

13、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;} }returntrue;}}

14、14将一个整数以二进制输出packageday04;importjava.util.Scanner;publicclassArrayTest{publicstaticvoidmain(String[]args){ intn; Scanners=newScanner(System.in); System.out.println("pleaseinputanumber:"); n=s.nextInt(); for(inti=31;i>=0;i--){ if((n&(1<<i))!=0){ System.out.print("1"); }else{ System.out.print("0"); } if((32-i)%8==0){ System.out.print(""); } }}}

15、15矩形图案packageday02;publicclassFangk{publicstaticvoidmain(String[]args){intm=5,n=6;for(inti=0;i<n;i++){System.out.print("*");}System.out.println();for(inti=0;i<m-2;i++){ System.out.print("*"); for(intj=0;j<n-2;j++){ System.out.print(""); } System.out.print("*"); System.out.println();}for(inti=0;i<n;i++){System.out.print("*");}}}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:57
  • 阅读量:35
  • 阅读量:48
  • 阅读量:48
  • 阅读量:62
  • 猜你喜欢