您的位置:

自己写的简单java,自己写的简单遗嘱

本文目录一览:

JAVA 编写计算器 要代码最简单的

学java的时候自己编的,很简单,能够连续输入计算式后进行计算

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**简易计算器,能够进行简单的计算

*

* @see 2008.12.9

*/

public class CalculatorA

implements ActionListener{

private JFrame frame;

private JTextField field;

private JButton[] allButtons;

private JButton clearButton;

// private JButton backButton;

String result="";//保存结果

StringBuilder sb = new StringBuilder();//保存要进行的计算式

int x = 0; //用来判断上一次的事件类型

String str = "123+456-789*0.=/";

ArrayListString arrayList = new ArrayListString();//保存计算式,通过方法进行运算

public CalculatorA(){

frame = new JFrame("我的计算器v1.1");

frame.setLocation(300,300);

field = new JTextField(25);

allButtons = new JButton[16];

for(int i=0;iallButtons.length;i++){

allButtons[i]= new JButton(str.substring(i,i+1));

}

clearButton = new JButton("CLEAR");

// backButton = new JButton("——");

init();

setFondAndColor();

addEventHander();

}

public void init(){

frame.setLayout(new BorderLayout());

JPanel northPanel = new JPanel();

JPanel centerPanel = new JPanel();

JPanel southPanel = new JPanel();

northPanel.setLayout(new FlowLayout());

centerPanel.setLayout(new GridLayout(4,4));

southPanel.setLayout(new FlowLayout());

northPanel.add(field);

for(int i=0;iallButtons.length;i++){

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

//southPanel.add(backButton);

frame.add(northPanel,BorderLayout.NORTH);

frame.add(centerPanel,BorderLayout.CENTER);

frame.add(southPanel,BorderLayout.SOUTH);

}

//设置输入字体

public void setFondAndColor(){

field.setFont(new Font("宋体",Font.BOLD,24));

field.setBackground(new Color(100,200,200));

field.setForeground(Color.RED);

//设置字体从右起始

field.setHorizontalAlignment(JTextField.RIGHT);

}

public void showMi(){

frame.pack();

frame.setResizable(false);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void addEventHander(){

for(int i=0;iallButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(this);

// backButton.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();//取得当前事件返回的值

if("0123456789.".indexOf(str)!=-1){

if(x == 0){ //当x为0时表示还没有进行输入

result=str;

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 1){

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 2){

sb.delete(0,sb.length());

result = result+str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 3){

result = str;

sb.delete(0,sb.length());

arrayList.clear();

field.setText(str);

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

result = str;

sb.append(str);

field.setText(str);

x = 1;

}

else{

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

}

else if("+*-/".indexOf(str)!=-1){

if(x == 0){

field.setText("");

x = 2;

}

else if(x == 1){

result = result + str;

arrayList.add(sb.toString());

arrayList.add(str);

sb.append(str);

field.setText(result);

x = 2;

}

else if(x == 2){

x = 2;

}

else if(x == 3){

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

x = 2;

}

else{

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

}

else if("=".equals(str)){

if(x == 0){

field.setText("0");

arrayList.clear();

result = "0";

x = 3;

}

else if(x == 1){

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("数据格式异常");

x = 0;

}

}

else if(x == 2){

field.setText("数据格式错误.....");

arrayList.clear();

x = 0;

}

else if(x == 3){

field.setText(result);

x = 3;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

x = 3;

}

else {

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("数据格式异常");

x = 0;

}

}

}

else if("CLEAR".equals(str)){

arrayList.clear();

field.setText("0");

arrayList.add("0");

x = 4;

}

else{

if(result.length()1){

result = result.substring(0,result.length()-1);

if(sb.length()0){

sb.delete(sb.length()-1,sb.length());

}

else {

sb.delete(0,1);

}

field.setText(result);

x = 5;

}

else{

result = "";

sb.delete(0,sb.length());

arrayList.clear();

field.setText("0");

x = 0;

}

}

}

public static ArrayListString getResult(ArrayListString list){

String res = null;

String[] s = {"/","*","-","+"};

int i=0;

if(list.size()1){

for(;is.length;){

if(s[i].equals("/")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));

//本地的数据格式

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("*")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("-")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getNumberInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else {

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

}

}

return list;

}

//对数字字符串进行排除不必要符号

public static String getChange(String res){

String s_temp = "";

char[] c = new char[res.length()];

for(int k=0;kc.length;k++){

c[k] = res.charAt(k);

}

for(int k=0;kc.length;k++){

if((c[k]= '0' c[k]= '9')|| c[k] == '.'){

s_temp += c[k];

}

}

return s_temp;

}

public static void main(String[] args){

new CalculatorA().showMi();

}

}

JAVA编程 新手,写简单点

public static void main(String[] args) {

    //控制台输入

    Scanner scan = new Scanner(System.in);

    //三次机会

    for (int i = 2; i = 0; i--) {

        //提示用户输入

        System.out.println("请输入用户名");

        //用户名

        String userName = scan.next();

        //提示用户输入

        System.out.println("请输入密码");

        //密码

        String password = scan.next();

        //如果输入正确,  break,结束循环

        if("jim".equals(userName)  "123456".equals(password)) {

            System.out.println("欢迎登录MyShopping系统");

            break;

        }

        else {

            if(i  0) {

                System.out.println("密码错误,你还有"+i+"次机会!");

                continue;

            }

            if(i == 0) {

                System.out.println("密码错误,三次机会已用完!");

            }

        }

    }

}

求高手帮忙写个简单的java?

import java.math.BigDecimal;

import java.util.HashMap;

import java.util.Scanner;

public class MallPrice {

/**

* 打折力度

*/

private static final BigDecimal discount = new BigDecimal(0.8);

/**

* 存储商品和对应单价的map

*/

private static HashMapString, BigDecimal fruitsMap = new HashMap();

static {

fruitsMap.put("香蕉", new BigDecimal(7.99));

fruitsMap.put("苹果", new BigDecimal(6.89));

fruitsMap.put("橘子", new BigDecimal(3.99));

}

public static void main(String[] args) {

//测试数据:香蕉|2,苹果|1,橘子|3

Scanner scanner = new Scanner(System.in);

System.out.println("请输入需要购买的商品和数量,按: 商品|数量,商品|数量  区分");

String input = scanner.next();

String[] array = input.split(",");

System.out.println("\n商品名称|购买数量(斤)|商品价格(元/斤)|打折前价格|打折后价格");

BigDecimal sum = BigDecimal.ZERO;

for (String s : array) {

String[] tmp = s.split("\\|");

if (fruitsMap.keySet().contains(tmp[0])) {

String name = tmp[0];

String count = tmp[1];

BigDecimal sumPrice = fruitsMap.get(name).multiply(new BigDecimal(Double.parseDouble(count))).setScale(3, BigDecimal.ROUND_HALF_UP);

BigDecimal discountPrice = sumPrice.multiply(discount).setScale(3, BigDecimal.ROUND_HALF_UP);

sum = sum.add(discountPrice);

System.out.println(name + "|" + count + "|" + fruitsMap.get(name).doubleValue() + "|" + sumPrice.doubleValue() + "|" + discountPrice.doubleValue());

} else {

System.out.println("没有" + tmp[0] + "这件商品!");

}

}

System.out.println("商品折扣:" + discount.doubleValue());

System.out.println("打折后购买商品总价格:" + sum.doubleValue()+"元");

}

}

怎样编写一个最简单的java程序

下下来,是不是exe文件 ,还是一个文件夹呢,你得配置一下你的系统变量在我的电脑右击属性,打开高级,在里面有一个变量 ,打开,在上面添加一个 名为PATH 值为,你安装的那个目录,下的bin 如: C:\java\bin 这样就可以用了 打开记事本,在里面输入:public class HellowWord{public static void main(String[]args){System.out.println("HellowWord!");} 保存为 HellowWord.java文件 文件名一定要和类名一样 进入Dos 中,并进入到这个文件 的目录在里面输入: javac HellowWord.java没有问题的情况下再次输入: java HellowWord就OK了}

写个简单的java语句!!

import java.util.Random;

public class GDUT {

private int num;

private String[] major;

public GDUT() {

major = new String[5];

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public String[] getMajor() {

return major;

}

public void setMajor(String[] major) {

this.major = major;

}

public void output() {

System.out.println("广东工业大学");

}

public static void main(String[] args) {

GDUT g = new GDUT();

Random r = new Random();

g.setNum(r.nextInt(100));

String[] strs = { "a", "b", "c", "d", "e" };

g.setMajor(strs);

g.output();

System.out.println(g.getNum());

for (String s : g.getMajor()) {

System.out.println(s);

}

}

}