计算机技术论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

  • 欢迎访问 计算机技术论坛-电脑迷与初学者的家园!由于论坛管理严格,新注册会员可能遇到各种问题,无法解决的请发邮件 admin@jsjbbs.cn
查看: 6360|回复: 7

Java代码1

  [复制链接]
发表于 2009-10-11 02:59:16 | 显示全部楼层 |阅读模式
import javax.swing.*;

public class Cashe {

        
        public static void main(String[] args) {
        /*StringBuffer类的构造方法。*/
                StringBuffer buf=new StringBuffer("Hello,how are you?");//该构造方法构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
                                                                        //该字符串的初始容量为 16 加上字符串参数的长度。
                String output="buf="+buf.toString()+           //toString()方法返回此对象本身(它已经是一个字符串!)。 (String类地方法)
                             
                              "\nlength="+ buf.length()+       //length()方法返回此字符串的长度。长度等于字符串中 Unicode 代码单元的数量。
                                                                 // (String类地方法)
                                                                //length的值是18
                              
                               "\ncapacity="+buf.capacity();  //capacity()方法返回当前容量。容量指可用于最新插入的字符的存储量,
                                                                //超过这一容量就需要再次进行分配。(StirngBuffer类的一个方法)
                                                             //capacity的值是16+18
               
               
                /*以下方法是StringBuffer类的一个方法*/
                buf.ensureCapacity(75);//确保容量至少等于指定的最小值。
                                      //如果当前容量小于 minimumCapacity 参数,则可分配一个具有更大容量的新的内部数组。
               
                output +="\n\nNew capacity=" + buf.capacity();
               
                buf.setLength(10);   //设置字符序列的长度。序列将被更改为一个新的字符序列,新序列的长度由参数指定。
               
                output +="\n\nNew Length="+buf.length()+"\nbuf="+buf.toString();
               
                JOptionPane.showMessageDialog(null,output,"字符串缓存长度和容量的实例",JOptionPane.INFORMATION_MESSAGE);
                /*上句是swing包中的类:标准对话框JOptionPane,调用方法showMessageDialog(),JOptionPane.INFORMATION_MESSAGE是要显示消息类型。*/
               
                System.out.println();
               
        }


}
import java.util.Scanner;
public class PrimeNumber {
        
        public static void main(String[] args) {
                System.out.println("please input a number:\n");
                Scanner sc=new Scanner(System.in);
                int n=sc.nextInt();
                int i;
            for(i=2;i<=n;i++){
                    while(n!=i){
                            if(n%i==0){
                                    System.out.println(i+"*");
                                    n=n/i;
                            }
                            else
                                    break;
                    }
            }
                System.out.println(n);
               
        }

}
import java.util.Scanner;

public class Conversion {
        
        public static void main(String[] args) {
        
                final int CONSTANT=32;
                float F,T;
                System.out.println("请输入摄氏温度:");
                Scanner input=new Scanner(System.in);
                T=input.nextFloat();
                F=T*9/5+CONSTANT;
                System.out.println("转换后得到的华氏温度为:"+F);
               
        }

}
import java.util.Scanner;

public class Conversion {
        
        public static void main(String[] args) {
               
                final float CONSTANT=1.60934f;
                float yingli,gongli;
                System.out.println("请输入公里数:");
                Scanner input=new Scanner(System.in);
                gongli=input.nextFloat();
                yingli=gongli*CONSTANT;
                System.out.println("转换后的英里数为:"+yingli);

        }

}

import java.util.Scanner;

public class CircumferenceAndArea {
        
        public static void main(String[] args) {
                float width,length,circumference,area;
                System.out.println("请输入矩形的宽:");
                Scanner input=new Scanner(System.in);
                width=input.nextFloat();
                System.out.println("请输入矩形的长:");
                length=input.nextFloat();
                circumference=2*(length+width);
                area=length*width;
                System.out.println("矩形的周长为:"+circumference);
                System.out.println("矩形的面积:"+area);

        }

}

public class Sum {
        
        public static void main(String[] args) {
        
                float x,y,s;
              System.out.println("please input x");
              Scanner input=new Scanner(System.in);
              x=input.nextFloat();
              System.out.println("please input y");
              y=input.nextFloat();
              s=x+y;
              System.out.println(s);


        }

}
 楼主| 发表于 2009-10-11 03:04:59 | 显示全部楼层
我建议一下,能否把网页要输入的文字 或代码调成是自动换行的,这样会方便很多...............
发表于 2009-10-11 10:35:31 | 显示全部楼层
晕了  有点看不懂了
发表于 2010-1-30 17:45:43 | 显示全部楼层
啥玩意啊!
发表于 2010-2-1 21:02:09 | 显示全部楼层
这难道就是传说中的编程?
发表于 2010-2-2 16:19:28 | 显示全部楼层
这代码运行的目的是达到什么效果啊
发表于 2010-6-14 21:46:17 | 显示全部楼层
代码结构有错了吧??
发表于 2010-11-24 10:21:19 | 显示全部楼层
lz发完了也该在后面加段说明吧
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

无图版|手机版|计算机技术论坛 JSJBBS.CN @ 2008-2024 ( 鲁ICP备17021708号 )

技术支持 : 北京康盛新创科技有限责任公司

快速回复 返回顶部 返回列表