编写一个简单java应用程序

作者&投稿:彭具 (若有异议请与网页底部的电邮联系)
急求一个简单的java应用程序helloworld的编写,编译和运行。一个简单的java小应用程序~

一个简单的计算输出:
创建一个.java后缀名的java文件,内容
public class helloworld {
public static void main(String[] args) {
int number_1 = 1;
int number_2 = 2;
int sum=number_1+number_2;
System.out.println("和为:"+sum);
}
}
其内容为number_1变量和number_2变量赋值,再定义一个sum变量存储两个变量的和。

在确保jdk运行环境都配置完成的情况下,微软键+r进入控制台,找到文件所在路径,输入javac helloworld.java回车进行编译。编译无误再输入java helloworld回车。得到值为3。

我刚好谢了一个 绝对原创
用到了图片,你可以将图片的代码删掉


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;

public class Notepad extends JFrame implements ActionListener {

JMenuBar jbar; //菜单条
JMenu wj,bj,bz; //菜单
JMenuItem open,save,osave,exit,help,me; //菜单项
JTextArea jta; //文本区


public Notepad(){
jbar = new JMenuBar();

wj = new JMenu("文件");
bj = new JMenu("编辑");
bz = new JMenu("帮助");
jbar.add(wj);
jbar.add(bj);
jbar.add(bz);
setJMenuBar(jbar);

open = new JMenuItem("打开",new ImageIcon("img/dk.png"));
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
wj.add(open);
open.addActionListener(this);
open.setActionCommand("open");
wj.addSeparator();
save = new JMenuItem("保存");
wj.add(save);
save.addActionListener(this);
save.setActionCommand("save");
osave = new JMenuItem("另存为",new ImageIcon("img/bc.png"));
wj.add(osave);
osave.addActionListener(this);
osave.setActionCommand("osave");
wj.addSeparator();
exit = new JMenuItem("退出");
wj.add(exit);
exit.addActionListener(this);
exit.setActionCommand("exit");
help = new JMenuItem("查看帮助");
bz.add(help);
bz.addSeparator();
me = new JMenuItem("关于记事本");
bz.add(me);

jta = new JTextArea();
this.add(new JScrollPane(jta),BorderLayout.CENTER);

this.setVisible(true);
this.setSize(500,400);
this.setTitle("桃子记事本");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){


if(e.getActionCommand().equals("open")){
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("请选择文件");
jfc.showOpenDialog(null);
jfc.setVisible(true);

String filepath = jfc.getSelectedFile().getAbsolutePath();

FileReader fr=null;
BufferedReader br=null;
try{
fr = new FileReader(filepath);
br = new BufferedReader(fr);

String s="";
String b="";
while((s=br.readLine())!=null){
b+=s+"
";
//System.out.println(s);
}
jta.setText(b);
}catch(Exception e1){
e1.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}


else if(e.getActionCommand().equals("osave")){
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("请选择路径");
jfc.showSaveDialog(null);
jfc.setVisible(true);

String filepath = jfc.getSelectedFile().getAbsolutePath();

FileWriter fw=null;
BufferedWriter bw=null;
try{
fw = new FileWriter(filepath);
bw = new BufferedWriter(fw);
bw.write(jta.getText());
}catch(Exception e2){
e2.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
else if(e.getActionCommand().equals("exit")){
System.exit(0);
}
}
}

class Trangle{
private double a, b, c;//三个边
private double area;//面积
private double perimeter;//周长

public double getArea() {//返回面积
return area;
}
public double getPerimeter() {//返回周长
return a+b+c;
}
public void setA(double a) {//修改边a
this.a = a;
}
public void setB(double b) {//修改边b
this.b = b;
}
public void setC(double c) {////修改边c
this.c = c;
}

public static boolean isTrangle(double a, double b, double c){//判断三个数能否构成一个三角形
if(a <= 0 || b <=0 || c<=0){
return false;
}

return a + b > c && a + c > b && b + c > a;
}

}

class Lader{
private double a;//上底
private double b;//下底
private double h;//高
private double area;//面积

public double getArea() {//返回面积
return (a+b)*h/2;
}
}

class Circle{
private double r;//半径
private double perimeter;//周长
private double area;//面积
public double getArea() {//返回面积
return 3.14* r * r;
}
public double getPerimeter() {//返回周长
return 3.14*2*r;
}
}

public class main {

public static void main(String[] args) {
//三角形
Trangle trangle = new Trangle();
double tranglea = 1, trangleb = 1, tranglec = 1, trangleArea = 1, tranglePerimeter = 1;
trangle.setA(tranglea);
trangle.setB(trangleb);
trangle.setC(tranglec);
System.out.println("trangleArea:" + trangle.getArea());
System.out.println("tranglePerimeter:" + trangle.getPerimeter());

//圆形
Circle circle= new Circle();
double circleR=0,circleArea=0,circlePerimeter=0;
circle.setR(circleR);
System.out.println("circleArea:" + circle.getArea());
System.out.println("circlePerimeter:" + circle.getPerimeter());

//梯形
Lader lader= new Lader();
double ladera=0,laderb=0,laderh=0,laderArea=0;
lader.setA(ladera);
lader.setB(laderb);
lader.setH(laderh);
System.out.println("laderArea:" + lader.getArea());
System.out.println("laderPerimeter:" + lader.getPerimeter());
}

}

class Trangle {

public Trangle() {
}

private double a, b, c;// 三个边
private double area;// 面积
private double perimeter;// 周长
private double h = (a + b + c) / 2;

public double getArea() {// 返回面积
return Math.sqrt(h * (h - a) * (h - b) * (h - c));
}

public double getPerimeter() {// 返回周长
return a + b + c;
}

public void setA(double a) {// 修改边a
this.a = a;
}

public void setB(double b) {// 修改边b
this.b = b;
}

public void setC(double c) {// //修改边c
this.c = c;
}

public boolean isTrangle(double a, double b, double c) {// 判断三个数能否构成一个三角形
if (a <= 0 || b <= 0 || c <= 0) {
return false;
}

return a + b > c && a + c > b && b + c > a;
}

}

class Lader {
private double a;// 上底
private double b;// 下底
private double h;// 高
private double area;// 面积
private double perimeter;// 周长

public double getPerimeter() {
return perimeter; //周长不会算了。你自己把分式放上去
}

public double getArea() {// 返回面积
return (a + b) * h / 2;
}

public void setA(double a) {
this.a = a;
}

public void setB(double b) {
this.b = b;
}

public void setH(double h) {
this.h = h;
}
}

class Circle {
private double r;// 半径
private double perimeter;// 周长
private double area;// 面积

public void setR(double r){
this.r=r;
}

public double getArea() {// 返回面积
return 3.14 * r * r;
}

public double getPerimeter() {// 返回周长
return 3.14 * 2 * r;
}
}

在main里输出。借用了楼上的类,自己懒的再写了

楼主 你不给flyingFish211 分 真是天理难容啊

没整明白你需要啥,三个最基本的类 flyingFish211 这个兄弟已经给及写了


谁能帮我写一个java程序
要求用数组(array)给a,b,c,d,f五人投票,我会用图灵写但是java刚学不是很会,谢谢各位大神在此附上我写的图灵程序希望大神作为参考... 要求用数组(array)给a,b,c,d,f五人投票,我会用图灵写但是java刚学不是很会,谢谢各位大神在此附上我写的图灵程序希望大神作为参考 展开 ...

用Java 写一个两个整数相加的程序
代码如下:public class Test { public static int add(int a,int b){ return a+b;} public static void main(String args[]){ Scanner scanner = new Scanner(System.in);System.out.println("请输入第一个数");int a = scanner.nextInt();System.out.println("请输入第二个数");int b...

用JAVA定义一个学生类Student来表示学生信息,学生类中包含成员有学号...
Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。Java 编程语言的风格十分接近C、C++语言。Java是一个纯的面向对象的程序...

JAVA编写一个简单的计算器,要求用户在界面上输入两个数,能够自动计算出...
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class SimpleCalculator { private JFrame f = new JFrame("Simple Calculator");private JLabel num1 ...

java编程 写一个程序 要求输入几就可以得到几行星星且每一行都比上一 ...
public static void main(String[] args) { System.out.print("请输入行数:"); Scanner scan = new Scanner(System.in); int length = scan.nextInt(); System.out.println("输入行数为:" + length); int i = 1, m = 1, n = length - 1; for (; i <= length; i+...

一个窗体,一个按钮,最简单的java代码怎写?
public class Demo extends JFrame{ JButton jb; \/\/一个按钮 public static void main(String []args){ new Demo(); } public Demo() { this.setLayout(new FlowLayout()); jb=new JButton("按扭"); this.add(jb); this.setSize(400,300); this.setVi...

用java编写 产生20个1-1000之间的随机正整数,并判断其中素数的个数...
import java.util.Random;public class RandomPrime { \/ param args \/ public static void main(String[] args) { int[] ranNums = new int[20];Random ran = new Random();for (int i = 0; i < ranNums.length; i++) { int num = ran.nextInt(1000) + 1;\/\/ ran.nextInt(1000...

在java里用最简单的方法编写一个程序,判断输入的是大写字母还是小写字 ...
全部利用String的自身方法来判定 import java.util.Scanner;public class Validator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);String str = scanner.nextLine().trim();if(str.length() > 1){ System.out.println("输入的是字符串");}else if(str...

谁会用java写这个程序,简单点,在线等初学者,简单点谢谢
package game.wall;import java.util.Scanner;public class GameDemo { public static int count1=0;\/\/ren public static int count2=0;\/\/机器 public static void main(String[] args) { boolean is = false;int math11=0;int math12=0;int math13=0;int math21=0;int math22=0;int ...

求解:写一段Java程序,要求简单实现计算器的功能,是GUI编程,代码简洁最...
import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;\/\/暂时不用考虑连加问题 \/\/点第一个运算符 点运算符...

广阳区19842876340: 一个简单的JAVA程序编写 -
卢胡典泰: public class test2 { public static void main(String[] args){ String sName[]= new String[]{"c","b","a","d","e"}; //输出上面的三行 for(int i = 2; i >= 0; i--) { for(int j = 0; j < i ; j++) { System.out.print(" "); } for(int n = 0; n < 5 - 2 * i; n++) { ...

广阳区19842876340: 编写一个最简单的java程序? -
卢胡典泰: import java.util.*; public class Tester{public static void main(String[] args){Scanner sc = new Scanner(System.in);while(true){System.out.print("输入一组数据:");String line = sc.nextLine().trim();if(!line.matches("^\\d+(\\s+\\d+)*$")){...

广阳区19842876340: 写一个简单的java程序 -
卢胡典泰: import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class CircleFrame { private Circle[] circles = null; private JFrame frame = null; final int circleNumber = 20; final ...

广阳区19842876340: 编写一个JAVA简单的程序! -
卢胡典泰: public static void main(String[] args){String firstString="This is";String secondString="This is"; if(firstString==null && secondString==null){System.out.println("①firstString 与 secondString 相等");}else if(firstString!=null && firstString...

广阳区19842876340: 编写一个简单的java程序 -
卢胡典泰: public class Hello { /*** @param args* @throws IOException*/public static void main(String[] args) throws IOException {Hello hello = new Hello();String path = "D:/abc.txt";// 写入文件hello.writeFile(path, "Hello world !"); // 删除文...

广阳区19842876340: 编写一个简单java应用程序 -
卢胡典泰: class Trangle{ private double a, b, c;//三个边 private double area;//面积 private double perimeter;//周长 public double getArea() {//返回面积 return area; } public double getPerimeter() {//返回周长 return a+b+c; } public void setA(double a) ...

广阳区19842876340: 怎样编写一个最简单的java程序 -
卢胡典泰: 给个最最简单的程序给你: public class FirstClass { //类名FirstClasspublic static void main(String[]args) { //主方法main,也就是程序的入口System.out.println("This is my first java program"); //系统输出} }

广阳区19842876340: 编写一个简单的Java程序?
卢胡典泰: import java.util.*; public class T { public static void main(String[] args) { F a=new F(); for(int i=1;i<100;i++){ System.out.print(" "+a.f(i)); } } } class F { public int f(int i){ if(i==1){ return 1; } else if(i==2){ return 1; } else{ return f(i-1)+f(i-2); } } }

广阳区19842876340: 帮忙写个简单的JAVA程序(本人刚入门) -
卢胡典泰: class oushu { public static void main(String args[]) { int i,num; System.out.println("50以内的偶数是:"); for(i=1;i<50;i++) { num=i%2; if(num==0) System.out.println(i+""); } System.out.println(""); } }

广阳区19842876340: 用JAVA编写一个应用程序 -
卢胡典泰: //Lader类 class Lader { double shangDi; //上底 double xiaDi; //下底 double heigth; //高 double area; //面积 //下面是构造方法 public Lader(double shangDi,double xiaDi,double heigth){ this.shangDi=shangDi; this.xiaDi=xiaDi; this.heigth=heigth...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网