/*求用java比较四个数大小源代码 (初学者 希望朋友给的代码是基础的那种) 老师说只要两个类 其中一个类从另一个类里调用*/
public class TestOrder {
public static void main(String[] args) {
// 也可以从控制台输出
int[] newArray = Order.orderBy(array);
for (int i = 0; i newArray.length; i++) {
System.out.println(newArray[i] + " ");
}
class Order {
public static int[] orderBy(int[] intArray) {
int tempArray[] = intArray;
int length = tempArray.length;
int tempValue = 0;
for (int i = 0; i length; i++) {
for (int j = i + 1; j length; j++) {
if (tempArray[i] tempArray[j]) {
tempValue = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = tempValue;
return tempArray;
class test
{
public static void main(String[] args)
System.out.println("Hello World!");
在C盘保存文件名为test
进入cmd运行 javac test.java 进行编译
然后运行test.java看到输出hello world
我的第一个程序就完成了
如果运行javac 报错的话那可能是环境变量没有配置好,在仔细看看重新配置一下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ConstructFrame extends JFrame
private static final long serialVersionUID = 1L;
int flag=0,fix=0,sum=1;
Boolean happy;
int flagsum=0;
Container c=getContentPane();
JButton buttonx;
ConstructFrame()
{ super("计算器");
c.setLayout(null);
c.setBackground(Color.blue);
c.add(text);
text.setHorizontalAlignment(JTextField.RIGHT);
final JButton buttonx=new JButton("BackSpace");
c.add(buttonx);
buttonx.addMouseListener(new MouseAdapter()
public void mousePressed(MouseEvent e)
int count=0;
String temp;
if(flag==0)
count=value1.length();
if(count!=1)
temp=value1.substring(0, count-1);
else
temp="0";
value1=temp;
text.setText(temp);
});
final JButton buttony=new JButton("CE");
c.add(buttony);
buttony.addMouseListener(new MouseAdapter()
value1="";
flag=0;
text.setText("0");
final JButton button1=new JButton("1");
c.add(button1);
button1.addMouseListener(new MouseAdapter()
value1=value1+1;
temp=value1;
final JButton button0=new JButton("0");
c.add(button0);
button0.addMouseListener(new MouseAdapter()
value1=value1+0;
final JButton buttonadd=new JButton(" + ");
c.add(buttonadd);
buttonadd.addMouseListener(new MouseAdapter()
flag=1;
fix=1;
flagsum=0;
final JButton buttonsubtract=new JButton(" - ");
c.add(buttonsubtract);
buttonsubtract.addMouseListener(new MouseAdapter()
final JButton buttoncheng=new JButton(" * ");
c.add(buttoncheng);
buttoncheng.addMouseListener(new MouseAdapter()
final JButton buttonchu=new JButton(" / ");
c.add(buttonchu);
buttonchu.addMouseListener(new MouseAdapter()
final JButton buttonequal=new JButton(" = ");
c.add(buttonequal);
buttonequal.addMouseListener(new MouseAdapter()
double temp=0;
temp1=Double.parseDouble(value1);
switch(fix)
result=Double.valueOf(temp).toString();
value1=result;
text.setText(result);
final JButton buttonpoint=new JButton(".");
c.add(buttonpoint);
buttonpoint.addMouseListener(new MouseAdapter()
{ if(flagsum==0)
if(flag==0 )
value1=value1+".";
flagsum=1;
JButton buttonz=new JButton("Start");
c.add(buttonz);
buttonz.addMouseListener(new MouseAdapter()
happy=true;
text.setText("0.");
happy=false;
text.setText("");
text.setEnabled(happy);
button1.setEnabled(happy);
button0.setEnabled(happy);
buttonx.setEnabled(happy);
buttony.setEnabled(happy);
buttonadd.setEnabled(happy);
buttonsubtract.setEnabled(happy);
buttonpoint.setEnabled(happy);
buttonequal.setEnabled(happy);
buttoncheng.setEnabled(happy);
buttonchu.setEnabled(happy);
sum++;
button1.setEnabled(false);
button0.setEnabled(false);
buttonx.setEnabled(false);
buttony.setEnabled(false);
buttonadd.setEnabled(false);
buttonsubtract.setEnabled(false);
buttonpoint.setEnabled(false);
buttonequal.setEnabled(false);
buttoncheng.setEnabled(false);
buttonchu.setEnabled(false);
text.setEnabled(false);
setVisible(true);
class MYMouseEvent extends MouseAdapter
value1=e.toString();
text.setText(value1);
class Calutator
new ConstructFrame();
你自己慢慢的看吧!
class Car
// 车辆属性
private String brand; // 品牌
private double engineDisplacement;// 排气量
private double speed;// 当前速度
private boolean status;// 启动状态
private double maxSpeed;// 最大速度
public double getSpeed () {
return this.speed;
public Car(String brand, double engineDisplacement, double maxSpeed) {
this.brand = brand;
this.engineDisplacement = engineDisplacement;
this.maxSpeed = maxSpeed;
// 其他属性也要有初始值,不然执行出错.
this.speed = 0;
this.status = false;
/** 启动 */
public void start() {
this.status = true;
printNowStatus ();
/** 关闭(熄火) */
public void stop() {
// 只在速度为0时关闭(貌似楼上两位没仔细看题...)
if (this.speed == 0)
/** 加速 */
public void speedUp() {
// 只在启动时可以加速
if (this.status)
if (this.speed this.maxSpeed)
this.speed = this.maxSpeed;
/** 减速 */
public void slowDown() {
// 只在启动时可以减速
this.speed = this.speed - 10;
if (this.speed 0)
/** 状态打印,在每次启动,加减速,关闭时调用 */
private void printNowStatus () {
System.out.println("轿车【" + this.brand + "】现在的启动状态是:" + this.status + "速度是:" + this.speed +".");
public class TestCar
//启动
myCar.start();
myCar.speedUp();
//循环减速
while (myCar.getSpeed() 0)
myCar.slowDown();
//关闭
myCar.stop();
/* 直接拿文本写的,我用的电脑没装jdk,楼主自己到Java开发环境下调试,应该没什么问题... */
//都是从新手过来的,以下代码供参考
//1.
public?class?BankAccount?{
private?static?String?acctnum;
private?static?double?money;
private?static?void?showAcct()?{
System.out.println("账号为:?"?+?acctnum);
private?static?void?showMoney()?{
System.out.println("余额为:?"?+?money);
public?BankAccount(String?acc,?double?m)?{
this.acctnum?=?acc;
this.money?=?m;
public?static?void?main(String[]?args)?{
ba.showAcct();
ba.showMoney();
public?class?Triangle?{
private?static?float?a;
private?static?float?b;
private?static?float?c;
public?Triangle(float?a,?float?b,?float?c)?{
this.a?=?a;
this.b?=?b;
this.c?=?c;
public?static?boolean?judgeTriangle(float?a,?float?b,?float?c)?{
if?((a?Math.abs(b?-?c)?a?b?+?c)
(b?Math.abs(a?-?c)?b?a?+?c)
(c?Math.abs(a?-?b)?c?a?+?b))
return?true;
return?false;
public?float?getCircumference()?{
return?this.a?+?this.b?+?this.c;
public?class?TestTriangle?{
System.out.print("能够成三角形,周长为:?");
System.out.println("不能构成三角形");
package com.regex;
import java.io.*;
import java.net.URLDecoder;
import java.util.regex.*;
public class Regex {
private int REMARK=0;
private int LOGIC=0;
private int PHYSIC=0;
boolean start=false;
/**
* @param args
*/
public static void main(String[] args) { //测试方法
// TODO Auto-generated method stub
Regex re=new Regex();
re.regCount("Regex.java");
System.out.println("remark Line: "+re.REMARK);
System.out.println("logic Line: "+re.LOGIC);
System.out.println("physic Line: "+re.PHYSIC);
}/**
* @author BlueDance
* @param s
* @deprecated count
public void regCount(String s){
String url=null;
try {
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
BufferedReader br=new BufferedReader(new FileReader(new File(url)));
String s1=null;
while((s1=br.readLine())!=null){
PHYSIC++;
if(CheckChar(s1)==1){
REMARK++;
System.out.println("纯注释行:"+s1);
LOGIC++;
System.out.println("非纯注释行:"+s1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}catch(IOException e){
*
* @return int
* @version check s
public int CheckChar(String s){
if(s!=null)
s1=s.trim();
//System.out.println(regCheck(s1,re));
if(regCheck(s1,"(//.*)")) //判断//开头的为纯注释行
return 1;
if(regCheck(s1,"(.*[;{})] *//.*)")) //判断不是//开头的非纯注释行
if(regCheck(s1,"(//*.*)")){ //判断/*开头的纯注释行
start=true;
if(regCheck(s1,"(.*[;{})]//*.*)")){ //判断不是/*开头的非纯注释行
if(regCheck(s1,"(.* */*/)")){ //判断*/结尾的纯注释行
start=false;
if(regCheck(s1,"(.* */*/.*)")!strCheck(s1)){ //判断不是*/结尾的非纯注释行
if(strCheck(s1)){
if(start==true) //状态代码,start即/*开始时start=true*/结束时为false
}//aeee
* @param re
* @return boolean
public boolean regCheck(String s,String re){ //正则表达试判断方法
return Pattern.matches(re,s);
public boolean strCheck(String s){ //中间有*/的字符判断 此方法最关键
if(s.indexOf("*/")0){
String y[]=s.split("/*/");
boolean boo[]=new boolean[y.length];
for (int i = 0; i y.length-1; i++) {
char c[]=y[i].toCharArray();
for (int j = 0; j c.length; j++) {
if(c[j]=='\\'c[j+1]=='"'){
count++;
boo[i]=true;
}else{
boo[i]=false;
for(int i=0;iboo.length;i++){
if(!boo[i])
return false;
return true;
public int countNumber(String s,String y){ //此方法为我前面写的字符串出现次数统计方法,不懂的可以看我前面的文章
String [] k=y.split(s);
if(y.lastIndexOf(s)==(y.length()-s.length()))
count=k.length;
count=k.length-1;
if(count==0)
System.out.println ("字符串\""+s+"\"在字符串\""+y+"\"没有出现过");
return count;
return -1;
public class GoodLucky extends JFrame implements ActionListener{
JTextField tf = new JTextField(); //实例化一个文本域
//设置两个按钮
JButton b1 = new JButton("开始");
boolean isGo = false;
//构造函数
public GoodLucky(){
b1.setActionCommand("start");//在开始按钮上设置一个动作监听 start
JPanel p = new JPanel(); //实例化一个可视化容器
//将两个按钮添加到可视化容器上面,用add方法
p.add(b1);
//在两个按钮上增加监听的属性,自动调用下面的监听处理方法actionPerformed(ActionEvent e),如果要代码有更好的可读性,可用内部类实现动作
//监听处理.
b1.addActionListener(this);
this.getContentPane().add(tf,"North"); //将上面的文本域放在面板的北方,也就是上面(上北下南左西右东)
this.getContentPane().add(p,"South"); //将可视化容器pannel放在南边,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作,参数EXIT_ON_CLOSE是使用 System exit 方法退出应用程序.仅在应用程序中使用
Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名称创建一个新的定制光标对象,参数表示手状光标类型
this.setCursor(cu); //为指定的光标设置光标图像,即设置光标图像为上面所创建的手状光标类型
this.setVisible(true); //将面板可视化设置为true,即可视,如果为false,即程序运行时面板会隐藏
tf.setText("welcome you! "); //设置面板的标题为欢迎
this.go(); //调用go方法
public void go(){
if(isGo == true){ //上面所定义的isGo的初始值为false,所以程序第一次到此会跳过
String s = ""; //设置空字符串
if(i 10){
s = s + " 0" + i; //如果产生的随机数小于10的话做处理:这里就牵扯到一个重要的概念,简单叙述一下:
/*
当一个字符串与一个整型数项相加的意思是连接,上面的s = s + " 0" + i的意思是字符串s链接0再连接整型i值,而不会导致0和整型的i相加,
s = s + " " + i; //如果产生的随机数比10打的话,那么加上空格显示,即数字和数字之间有个空格
tf.setText(s); //将产生的随机数全部显示在文本域上,用文本域对象tf调用它的设置文本的方法setText(String)实现.
//以下为线程延迟
try{
Thread.sleep(10); //线程类同步方法sleep,睡眠方法,括号里的单位为ms.
}catch(java.lang.InterruptedException e){
e.printStackTrace(); //异常捕获,不用多说.
//以下是上面设置的事件监听的具体处理办法,即监听时间处理方法,自动调用
public void actionPerformed(ActionEvent e){ //传入一个动作事件的参数e
String s = e.getActionCommand(); //设置字符串s来存储获得动作监听,上面的start
执行else语句块中的语句,isGo设置为false,将不执行上面go中的循环语句块,从而停止产生随机数,并显示,并且把开始按钮设置为可用,而把
停止按钮设置为不可用,等待用户按下开始再去开始新一轮循环产生随机数.
if(s.equals("start")){ //如果捕获到start,也就是用户触发了动作监听器,那么下面处理
isGo = true; //设置isGo为true
b1.setEnabled(false); //将开始按钮设置为不可用
isGo = false; //将isGo设置为false,isGo为循环标志位
b1.setEnabled(true); //设置开始按钮为可用
public static void main(String[] args){
new GoodLucky(); //产生类的实例,执行方法
以上就是土嘎嘎小编为大家整理的java基础入门源代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!