Java簡略單純盤算器法式設計。本站提示廣大學習愛好者:(Java簡略單純盤算器法式設計)文章只能為提供參考,不一定能成為您想要的結果。以下是Java簡略單純盤算器法式設計正文
本文引見了C#中csv文件與DataTable相互導入處置實例解析,重要功效代碼封裝處置下,絕對比擬簡略。今後項目用到的話可以直接應用。詳細辦法以下:
1.封裝好的類以下:
using System; using System.Data; using System.IO; using System.Text; using CSharpUtilHelpV2; using StringUtilHelp; namespace DBUtilHelpV2Plus { public static class DBToolV2Plus { /// <summary> /// 將DataTable導出到CSV. /// </summary> /// <param name="table">DataTable</param> /// <param name="fullSavePath">保留途徑</param> /// <param name="tableheader">題目信息</param> /// <param name="columname">列稱號『eg:姓名,年紀』</param> /// <returns>導出勝利true;導出掉敗false</returns> public static bool ToCSV(this DataTable table, string fullSavePath, string tableheader, string columname) { ArgumentChecked(table, fullSavePath); //------------------------------------------------------------------------------------ try { string _bufferLine = ""; using (StreamWriter _writerObj = new StreamWriter(fullSavePath, false, Encoding.UTF8)) { if (!string.IsNullOrEmpty(tableheader)) _writerObj.WriteLine(tableheader); if (!string.IsNullOrEmpty(columname)) _writerObj.WriteLine(columname); for (int i = 0; i < table.Rows.Count; i++) { _bufferLine = ""; for (int j = 0; j < table.Columns.Count; j++) { if (j > 0) _bufferLine += ","; _bufferLine += table.Rows[i][j].ToString(); } _writerObj.WriteLine(_bufferLine); } return true; } } catch (Exception) { return false; } } /// <summary> /// 參數檢討 /// </summary> /// <param name="table"></param> /// <param name="fullSavePath"></param> private static void ArgumentChecked(DataTable table, string fullSavePath) { if (table == null) throw new ArgumentNullException("table"); if (string.IsNullOrEmpty(fullSavePath)) throw new ArgumentNullException("fullSavePath"); string _fileName = CSharpToolV2.GetFileNameOnly(fullSavePath); if (string.IsNullOrEmpty(_fileName)) throw new ArgumentException(string.Format("參數fullSavePath的值{0},不是准確的文件途徑!", fullSavePath)); if (!_fileName.InvalidFileNameChars()) throw new ArgumentException(string.Format("參數fullSavePath的值{0},包括不法字符!", fullSavePath)); } /// <summary> /// 將CSV文件數據導入到Datable中 /// </summary> /// <param name="table"></param> /// <param name="filePath">DataTable</param> /// <param name="rowIndex">保留途徑</param> /// <returns>Datable</returns> public static DataTable AppendCSVRecord(this DataTable table, string filePath, int rowIndex) { ArgumentChecked(table, filePath); if (rowIndex < 0) throw new ArgumentException("rowIndex"); using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8, false)) { int i = 0, j = 0; reader.Peek(); while (reader.Peek() > 0) { j = j + 1; string _line = reader.ReadLine(); if (j >= rowIndex + 1) { string[] _split = _line.Split(','); DataRow _row = table.NewRow(); for (i = 0; i < _split.Length; i++) { _row[i] = _split[i]; } table.Rows.Add(_row); } } return table; } } } }
2.代碼應用測試以下:
using System; using System.Data; using DBUtilHelpV2; using DBUtilHelpV2Plus; namespace DBUtilHelpV2PlusTest { class Program { static DataTable testDb = null; static string fullSavePath = string.Format(@"C:\{0}.csv", DateTime.Now.ToString("yyyyMMddHH")); static void Main(string[] args) { try { CreateTestDb(); Console.WriteLine(string.Format("DataTable導出到CSV文件{0}.", testDb.ToCSV(fullSavePath, "姓名,年紀", "人員信息表") == true ? "勝利" : "掉敗")); testDb.Rows.Clear(); Console.WriteLine(string.Format("清空數據,以後{0}條數據.", testDb.Rows.Count)); testDb = testDb.AppendCSVRecord(fullSavePath, 2); Console.WriteLine(string.Format("CSV文件導入到Datable,導入{0}條數據.", testDb.Rows.Count)); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadLine(); } } static void CreateTestDb() { if (testDb == null) { testDb = DBToolV2.CreateTable("Name,Age|int"); for (int i = 1; i <= 10; i++) { DataRow _row = testDb.NewRow(); _row["Name"] = string.Format("YanZhiwei_{0}", i); _row["Age"] = i; testDb.Rows.Add(_row); } } } } }
運轉後果以下圖所示:
h(z) { case 0: t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; case 1: t1.setText(""+(x-y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; case 2: t1.setText(""+(x*y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; case 3: t1.setText(""+(x/y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; } } else if(e.getSource()==b[11])//單擊"."按鈕輸出小數 { if(t1.getText().trim().indexOf('.')!=-1)//斷定字符串中能否曾經包括了小數點 { } else //假如沒有小數點 { if(t1.getText().trim().equals("0"))//假如初時顯示為0 { t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT); } else if(t1.getText().trim().equals(""))//假如初時顯示為空則不做任何操作 {} else { t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT); } } y=0d; } else if(e.getSource()==b6) //求平方根 { x=Double.parseDouble(t1.getText().trim()); if(x<0) { t1.setText("數字格局異常"); t1.setHorizontalAlignment(JTextField.RIGHT); } else { t1.setText(""+Math.sqrt(x)); t1.setHorizontalAlignment(JTextField.RIGHT); } str.setLength(0); y=0d; } else { if(e.getSource()==b[0])//假如選擇的是"0"這個數字鍵 { if(t1.getText().trim().equals("0"))//假如顯示屏顯示的為零不做操作 {} else t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT); y=Double.parseDouble(t1.getText().trim()); } else if (e.getSource()==b2) //選擇的是back鍵 { if(!t1.getText().trim().equals("0"))//假如顯示屏顯示的不是零 { if(str.length()!=1) { t1.setText(str.delete(str.length()-1,str.length()).toString());//能夠拋出字符串越界異常 t1.setHorizontalAlignment(JTextField.RIGHT); } else { t1.setText("0"); t1.setHorizontalAlignment(JTextField.RIGHT); str.setLength(0); } } y=Double.parseDouble(t1.getText().trim()); } else { t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT); y=Double.parseDouble(t1.getText().trim()); } } } catch(NumberFormatException e1){ t1.setText("數字格局異常"); t1.setHorizontalAlignment(JTextField.RIGHT); } catch(StringIndexOutOfBoundsException e1){t1.setText("字符串索引越界"); t1.setHorizontalAlignment(JTextField.RIGHT);} } }運轉後果圖:
一個Java簡略單純盤算器法式設計就這麼完成了,願望本文可以對年夜家編寫盤算器有所啟示,這只是一個簡略單純的盤算器,年夜家還可以持續施展,完美盤算器的功效。