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();
}
}
public class test{
public static void main(String[] args){
int count = 0;
int num = 0;
for(int i=1; i<=1000; i++){
count += i;
String str = StringvalueOf(count);
if(strsubstring(strlength()-1)equals("8")){
num++;
}
}
Systemoutprintln("以8字结尾的得数总共有: "+num +" 个");
}
}
我也不是高手,也是初学。正巧碰到这个问题感觉我还是能解决的。就随手写了下,回答下。大家共同进步哈。有不对的地方还要请各位真正的高手指正。
代码在下面,复制到你的编辑器里面看比较好,加了注解的。还有问题就再问哈。
---------------------------------------------------------------
public class d_array
{
public static void main(String[] args)
{
int[][] d_array = new int[8][8]; //定义一个8行8列的二维数组
int num = 1; //定义一个填充用的数备用
for (int i = 0 ; i < d_arraylength ; i++)
for (int j = 0 ; j < d_array[i]length ; j++)
d_array[i][j] = num++;
//让上面那个填充数自加并填充进数组
for (int p = 0 ; p < d_arraylength ; p++)
{
Systemoutprintln("数组第 "+(p+1)+" 行中的值为:"); //加一句说明是第几行的,不要也可以。
for (int q = 0 ; q < d_array[p]length ; q++)
Systemoutprint(d_array[p][q]+"\t"); //加这个\t可以有效的分离开两个数,排版好看
Systemoutprint("\n\n"); //换行,第一个用于一行之后换行,第二个用于一个空行,与下面那行隔开,排版好看的
}
//分行打印数组
int sum = 0; //一样,定义一个合备用
for (int m = 0 ; m < d_arraylength ; m++)
for (int n = 0 ; n < d_array[m]length ; n++)
sum = sum + d_array[m][n];
//分别将数组中的各个数加到这个合中去,最后得到的这个合就是数组中所有数的合了。
Systemoutprintln("数组中的所有值得合为:"+sum); //打印结果
}
}
---------------------------------------------------------------
其实二维数组的 *** 作秘诀就是两个for循环套起来用,这样就很简单了。
import javautilScanner;
public class Grade {
public static void main(String[] args) {
Scanner scan = new Scanner(Systemin);
double arr[] = new double[5];
for (int i = 0; i < arrlength; i++) {
arr[i] = scannextDouble();
}
sort(arr);
average(arr);
}
public static void sort(double arr[]) {
for (int i = 0; i < arrlength; i++) {
double max = arr[0];
for (int j = i; j < arrlength; j++) {
if (arr[i] < arr[j]) {
double temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public static void average(double arr[]) {
double temp = 0, sum = 0;
for (int i = 1; i < arrlength - 1; i++) {
sum += arr[i];
temp = sum / (arrlength - 2);
}
Systemoutprintln(temp);
}
}
数学上,斐波那契数列是以递归的方法来定义:
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2)
就是说从第三个数字开始,每一个数等于前两项的和,所以
A1输入0 A2输入1
A3输入=A1+A2
意大利数学家列昂纳多·费波纳茨(Leonardo Fibonacci,生于公元1170年,卒于1240年,籍贯大概是比萨),“费波纳茨数列”的发明者。斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……
这个数列从第三项开始,每一项都等于前两项之和。它的通项公式为:(1/√5){[(1+√5)/2]^n - [(1-√5)/2]^n}(又叫“比内公式”,是用无理数表示有理数的一个范例。)(√5表示根号5)
有趣的是:这样一个完全是自然数的数列,通项公式居然是用无理数来表达的。
随着数列项数的增加,前一项与后一项之比越来越逼近黄金分割的数值06180339887……
从第二项开始,每个奇数项的平方都比前后两项之积多1,每个偶数项的平方都比前后两项之积少1。
给你一个参考,希望不要被百度吞了当晚餐
import javaawtBorderLayout;
import javaawtGridLayout;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javatextDecimalFormat;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJTextField;
public class Calculator {
//计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);
//退格键
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//删除
private JButton C = new JButton("C ");
//四则运算符号键
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("");
private JButton div = new JButton("/");
//小数点
private JButton decimal = new JButton("");
//等号
private JButton equal = new JButton("=");
//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号
public static void main(String[] args) {
new Calculator();//new计算器实例
}
public Calculator(){
//添加组件,布局
inputPaneladd(input);
fadd(inputPanel, BorderLayoutNORTH);
operationPaneladd(backspace);
operationPaneladd(CE);
operationPaneladd(C);
fadd(operationPanel, BorderLayoutCENTER);
buttonsPaneladd(add);
buttonsPaneladd(sub);
buttonsPaneladd(mul);
buttonsPaneladd(div);
buttonsPaneladd(one);
buttonsPaneladd(two);
buttonsPaneladd(three);
buttonsPaneladd(zero);
buttonsPaneladd(four);
buttonsPaneladd(five);
buttonsPaneladd(six);
buttonsPaneladd(decimal);
buttonsPaneladd(seven);
buttonsPaneladd(eight);
buttonsPaneladd(nine);
buttonsPaneladd(equal);
buttonsPanelsetLayout(new GridLayout(4, 4));
fadd(buttonsPanel, BorderLayoutSOUTH);
//注册各个组件监听事件
backspaceaddMouseListener(new OperationMouseListener());
CEaddMouseListener(new OperationMouseListener());
CaddMouseListener(new OperationMouseListener());
decimaladdMouseListener(new OperationMouseListener());
equaladdMouseListener(new OperationMouseListener());
//注册四则运算监听
addaddMouseListener(new CalcMouseListener());
subaddMouseListener(new CalcMouseListener());
muladdMouseListener(new CalcMouseListener());
divaddMouseListener(new CalcMouseListener());
//注册数字监听事件
zeroaddMouseListener(new NumberMouseListener());
oneaddMouseListener(new NumberMouseListener());
twoaddMouseListener(new NumberMouseListener());
threeaddMouseListener(new NumberMouseListener());
fouraddMouseListener(new NumberMouseListener());
fiveaddMouseListener(new NumberMouseListener());
sixaddMouseListener(new NumberMouseListener());
sevenaddMouseListener(new NumberMouseListener());
eightaddMouseListener(new NumberMouseListener());
nineaddMouseListener(new NumberMouseListener());
fsetVisible(true);
fpack();
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(inputgetText()trim()equals("0")){//如果文本框已经是0,结果还是0
inputsetText(((JButton)egetSource())getText()trim());
}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10
inputsetText(inputgetText()concat(((JButton)egetSource())getText()trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
//如果输入的是运算符号,保存第一个结果和运算符号
public void mouseClicked(MouseEvent e) {
num1 = inputgetText()trim();inputsetText("");
operator = ((JButton)egetSource())getText()trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(egetSource() == backspace){//退格键,删除一个直到没有字符删除
String inputText = inputgetText();
if(inputTextlength() > 0){
inputsetText(inputTextsubstring(0, inputTextlength() - 1));
}
}else if(egetSource() == C){
inputsetText("0");//C,清空所有运算数字和符号
num1 = "";
}else if(egetSource() == CE){
inputsetText("0");//CE-->将文本框置为0
}else if(egetSource() == decimal){
String text = inputgetText()trim();
//如果按了小数点,如果文本框已经有小数点,不做任何 *** 作,否则在结果后面加上小数点
if(textindexOf("") == -1){
inputsetText(textconcat(""));
}
}else if(egetSource() == equal){
//如果是等号
if(!operatortrim()equals("")){
if(!inputgetText()trim()equals("")){
double result = 0D;
if(operatorequals("+")){//执行加法运算
result = DoubleparseDouble(num1) + DoubleparseDouble(inputgetText()trim());
}else if(operatorequals("-")){//减法运算
result = DoubleparseDouble(num1) - DoubleparseDouble(inputgetText()trim());
}else if(operatorequals("")){//乘法运算
result = DoubleparseDouble(num1) DoubleparseDouble(inputgetText()trim());
}else if(operatorequals("/")){//除法运算
result = DoubleparseDouble(num1) / DoubleparseDouble(inputgetText()trim());
}
//格式化最终结果,保留两位小数点
inputsetText(new DecimalFormat("000")format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
Java 8 以上(包含java8) Lambda 方式,这种方式比较简洁,代码如下:
import javautilArrayList;import javautilList;
import javautilScanner;
public class App20 {
public static void main(String[] args) {
Scanner scanner = new Scanner(Systemin);
List<Float> scores = new ArrayList<>();
Systemoutprintln("请输入10个人的成绩:");
for (int i = 0; i < 10; i++) {
scoresadd(scannernextFloat());
}
float max = scoresstream()max((x, y) -> Floatcompare(x, y))get();
Systemoutprintln("最高分:" + max);
float min = scoresstream()min((x, y) -> Floatcompare(x, y))get();
Systemoutprintln("最低分:" + min);
float average = scoresstream()reduce(00f, Float::sum) / scoressize();
Systemoutprintln("平均分:" + average);
scoressort((x, y) -> Floatcompare(x, y));
Systemoutprintln("从小到大分数排序:");
scoresforEach(s -> Systemoutprint(s + " "));
long n = scoresstream()filter(r -> r > average)count();
Systemoutprintln("\n高于平均分的有" + n + "个");
}
}
传统方式,如下:
import javautilArrays;public class App21 {
public static void main(String[] args) {
float[] scores = new float[] { 10f, 9f, 8f, 7f, 6f, 5f, 4f, 3f, 2f, 1f };
float max = FloatMIN_VALUE;
for(float score : scores) {
if (score > max) {
max = score;
}
}
Systemoutprintln("最高分:" + max);
float min = FloatMAX_VALUE;
for(float score : scores) {
if (score < min) {
min = score;
}
}
Systemoutprintln("最低分:" + min);
float sum = 0;
for(float score : scores) {
sum += score;
}
float average = sum / scoreslength;
Systemoutprintln("平均分:" + average);
// 从小到大排序
Arrayssort(scores);
Systemoutprintln("从小到大分数排序:");
for(float score : scores) {
Systemoutprint(score + " ");
}
int n = 0;
for(float score : scores) {
if (score > average)
n++;
}
Systemoutprintln("\n高于平均分的有" + n + "个");
}
}
以上就是关于java:编写一个计算器小程序,要求可以做加减乘除运算全部的内容,包括:java:编写一个计算器小程序,要求可以做加减乘除运算、java语言实现求1+2+3……+1000的和,把和输出,计算每步结果中有多少个最后以8结尾的。、要求在java编程中,编写应用程序,定义一个8行8咧的二维数组,等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)