本文目錄一覽:
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);
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/251816.html