
整个项目打包到现场部署需要大量成本的时候,编写一个单功能的测试小程序部署就会便捷很多。 直接上教程
注意:如果需要引入的第三方包是多个,在Windows中用分号;隔开,在Linux中用冒号:隔开
(1) import javaxswingJFrame;
import javaxswingJOptionPane;
public class HelloWorld {
public static void main(String args[]){
JFrame ff=new JFrame();
String R=JOptionPaneshowInputDialog("请输入圆的半径:");
double r=DoubleparseDouble(R);
JOptionPaneshowMessageDialog(ff, "圆的面积为:"+314rr+"\n"+"圆的周长为:"+2314r);
}
}
(2) import javautilScanner;
public class HelloWorld {
public static void main(String args[]){
Scanner scan=new Scanner(Systemin);
int a=scannextInt();
int b=scannextInt();
int c=scannextInt();
if(a+b>c&&b+c>a&&a+c>b){
if((aa==bb+cc)||(cc==aa+bb)||(bb==aa+cc))
Systemoutprint("是直角三角形");
}else
Systemoutprint("不能构成三角形");
}
}
(3) public static void main(String args[]){
int a=1000,i=0,sum=0;
while(i<=a)
{
if(i%2==0)
sum=sum+i;
i++;
}
Systemoutprint(sum);
}
(4) public static void main(String args[]){
int a=12454,i=0;
while(a>0){
a=a/10;
i++;
}
Systemoutprintln(i);
}
(5) import javautilScanner;
public class HelloWorld {
public static void main(String args[]){
Scanner scan=new Scanner(Systemin);
int road=scannextInt();
int weight=scannextInt();
double sum=0;
if (road<=100){
sum=weightroad1;
}else if(road<=300){
sum=weightroad09;
}else if(road<=500){
sum=weightroad08;
}else if(road<=1000){
sum=weightroad07;
}else
sum=weightroad06;
Systemoutprint(sum);
}
}
(6) public static void main(String args[]){
int sum=0;
int total=1;
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++)
total=totalj;
sum=sum+total;
total=1;
}
Systemoutprintln(sum);
}
参考下,建议自己动手,进步更快哦
package com;
public interface PlaneGraphics {
public abstract double area();
public abstract double perimeter();
public abstract void print();
}
package com;
public class Rectangle implements PlaneGraphics{
private double x = 75;
private double y = 157;
private double s = 0;
private double l= 0;
public double area() {
s = x y;
return s;
}
public double perimeter() {
l = 2 (x + y);
return l;
}
public void print() {
Systemoutprintln("这是长方形");
Systemoutprintln("长方形的宽度:"+x+"长方形的长度:"+y);
Systemoutprintln("长方形的面积:"+s);
Systemoutprintln("长方形的周长:"+l);
}
package com;
public class Ellipse implements PlaneGraphics {
private double a = 57;
private double b = 25;
private double s = 0;
private double l = 0;
private double pie = 314;
public double area() {
s = pie a b;
return s;
}
public double perimeter() {
double m = 0;
m = javalangMathsqrt(a b);
l = pie (15 (a+b) - m);
return l;
}
public void print() {
Systemoutprintln("这是椭圆形");
Systemoutprintln("椭圆形的a轴长:"+a+"椭圆形的b轴长:"+b);
Systemoutprintln("椭圆形的面积:"+s);
Systemoutprintln("椭圆形的周长:"+l);
}
}
package com;
public class Test {
public static void main(String[] args) {
Rectangle rec = new Rectangle();
recarea();
recperimeter();
recprint();
Ellipse ell = new Ellipse();
ellarea();
ellperimeter();
ellprint();
}
}
import javaxswing;
import javaawt;
import javaawtevent;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton(""),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton(""),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 00; // 计算的中间结果
private boolean operateValidFlag = true; //判断 *** 作是否合法
public Calculator()
{
super("Calculator");
thissetBounds(300, 300, 300, 300);
thissetResizable(false);
thissetBackground(Colororange);
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
thisgetContentPane()setLayout(new BorderLayout());//设置布局
text_double=new JTextField("0",20);//设置文本区
text_doublesetHorizontalAlignment(JTextFieldRIGHT);//设置水平对齐方式未右对齐
thisgetContentPane()add(text_double,BorderLayoutNORTH);//将文本区添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
thisgetContentPane()add(panel);//添加panel面板
for(int i=0;i<buttonlength;i++)//在面板上添加按钮
paneladd(button[i]);
for(int i=0;i<buttonlength;i++)
button[i]addActionListener(this);//为按钮注册
text_doublesetEditable(false);//文本框不可编辑
text_doubleaddActionListener(this);//
thissetVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= egetActionCommand();//返回与此动作相关的命令字符串。
Systemoutprintln("##########command is "+c);
if(cequals("C")){
handleC(); //用户按了“C”键
}
else if (cequals("CE")) // 用户按了"CE"键
{
text_doublesetText("0");
}
else if ("0123456789"indexOf(c) >= 0) // 用户按了数字键或者小数点键
{
handleNumber(c); // handlezero(zero);
} else //用户按了运算符键
{
handleOperator(c);
}
}
private void handleC() // 初始化计算器的各种值
{
text_doublesetText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_doublesetText(button);
} else if ((buttonequals("")) && (text_doublegetText()indexOf("") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_doublesetText(text_doublegetText() + "");
} else if (!buttonequals(""))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_doublesetText(text_doublegetText() + button);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
private void handleOperator(String button) {
if (operatorequals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 00){
// *** 作不合法
operateValidFlag = false;
text_doublesetText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operatorequals("+")){
// 加法运算
resultNum += getNumberFromText();
} else if (operatorequals("-")){
// 减法运算
resultNum -= getNumberFromText();
} else if (operatorequals("")){
// 乘法运算
resultNum = getNumberFromText();
} else if (operatorequals("sqrt")) {
// 平方根运算
if(getNumberFromText()<0){
operateValidFlag = false;
text_doublesetText("被开方数不能为负数");}
else
resultNum = Mathsqrt(resultNum);
}
else if (operatorequals("+/-")){
// 正数负数运算
resultNum = resultNum (-1);
} else if (operatorequals("=")){
// 赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_doublesetText(StringvalueOf(t1));
} else {
text_doublesetText(StringvalueOf(resultNum));
}
}
operator = button; //运算符等于用户按的按钮
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //从结果的文本框获取数字
{
double result = 0;
try {
result = DoublevalueOf(text_doublegetText())doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 实例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}
给你一个简单的实现吧,注意一定要先运行MyServerjava
//MyCilentjava
import javaio;
import javanet;
import javaxswing;
import javaawt;
import javaawtevent;
public class MyClient extends JFrame implements ActionListener{
JTextField tf;
JTextArea tx;
JButton bt;
PrintWriter out;
public MyClient(){
tf=new JTextField(20);
tx=new JTextArea();
txsetLineWrap(true);
txsetWrapStyleWord(true);
JPanel pan=new JPanel();
JScrollPane jsp=new JScrollPane(tx);
add(jsp,"Center");
bt=new JButton("SEND");
btaddActionListener(this);
panadd(tf);
panadd(bt);
add(pan,"South");
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Systemexit(0);
}
});
setTitle("THE CLIENT");
setSize(400,300);
setVisible(true);
try{
Socket socket=new Socket("127001",1680);
out=new PrintWriter(socketgetOutputStream(),true);
InputStreamReader in = new InputStreamReader(socketgetInputStream());
BufferedReader sin=new BufferedReader(in);
String s;
while(true){
s=sinreadLine();
txappend("#Server Said#: "+s+"\n");
}
}catch(Exception e){
eprintStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(egetSource()==bt){
txappend("@Client Said@: "+tfgetText()+"\n");
outprintln(tfgetText());
tfsetText("");
}
}
public static void main(String[] args){
MyClient mct = new MyClient();
}
}
//MyServerjava
import javaio;
import javanet;
import javaxswing;
import javaawt;
import javaawtevent;
public class MyServer extends JFrame implements ActionListener{
JTextField tf;
JTextArea tx;
JButton bt;
JScrollPane jsp;
JPanel pan;
PrintWriter out;
public MyServer(){
tx=new JTextArea();
txsetLineWrap(true);
txsetWrapStyleWord(true);
jsp=new JScrollPane(tx);
tf=new JTextField(20);
bt=new JButton("SEND");
btaddActionListener(this);
pan=new JPanel();
panadd(tf);
panadd(bt);
add(pan,"South");
add(jsp,"Center");
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Systemexit(0);
}
});
setTitle("THE SERVER");
setSize(400,300);
setVisible(true);
try{
ServerSocket server = new ServerSocket(1680);
Socket socket = serveraccept();
InputStreamReader in = new InputStreamReader(socketgetInputStream());
BufferedReader sin=new BufferedReader(in);
out=new PrintWriter(socketgetOutputStream(),true);
while(true){
String s=sinreadLine();
txappend("@Client Said@: "+s+"\n");
}
}catch(Exception e){
eprintStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(egetSource()==bt){
String st = tfgetText();
txappend("#Server Said#: "+st+"\n");
outprintln(st);
tfsetText("");
}
}
public static void main(String[] args){
MyServer msr = new MyServer();
}
}
以上就是关于如何编写一个Java小程序通过java -jar的方式去测试一个功能全部的内容,包括:如何编写一个Java小程序通过java -jar的方式去测试一个功能、求写几个JAVA小程序!急!、求助,用java编写一个小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)