我正在使用p5.js构建计算器,所有功能都已正常工作,除了一个按钮——小数点。我在代码中对小数点进行了一些处理,但效果并不像我想象的那样。有谁知道如何使这个功能正常工作?非常感谢您的帮助。谢谢。
let number = 0;
let number1 = 0;
let operation = "";
function setup() {
createCanvas(400, 400);
let one = createButton('1');
one.mousePressed(clickedOne);
let two = createButton('2');
two.mousePressed(clickedTwo);
let three = createButton('3');
three.mousePressed(clickedThree);
let four = createButton('4');
four.mousePressed(clickedFour);
let five = createButton('5');
five.mousePressed(clickedFive);
let six = createButton('6');
six.mousePressed(clickedSix);
let seven = createButton('7');
seven.mousePressed(clickedSeven);
let eight = createButton('8');
eight.mousePressed(clickedEight);
let nine = createButton('9');
nine.mousePressed(clickedNine);
let zero = createButton('0');
zero.mousePressed(clickedZero);
let equals = createButton('=');
equals.mousePressed(pressedEquals);
let clear = createButton('AC');
clear.mousePressed(clickedClear);
let divide = createButton('÷');
divide.mousePressed(clickedDivide);
let multiply = createButton('x');
multiply.mousePressed(clickedMultiply);
let add = createButton('+');
add.mousePressed(clickedAdd);
let minus = createButton('-');
minus.mousePressed(clickedMinus);
let decimal = createButton('.');
decimal.mousePressed(clickedDecimal);
one.position(100, 150);
one.size(45, 45);
two.position(144, 150);
two.size(45, 45);
four.position(100, 194);
four.size(45, 45);
six.position(188, 194);
six.size(45, 45);
three.position(188, 150);
three.size(45, 45);
five.position(144, 194);
five.size(45, 45);
seven.position(100, 238);
seven.size(45, 45);
eight.position(144, 238);
eight.size(45, 45);
nine.position(188, 238);
nine.size(45, 45);
zero.position(100, 282);
zero.size(45, 45);
equals.position(188, 282);
equals.size(45, 45);
divide.position(232, 150);
divide.size(45, 45);
multiply.position(232, 194);
multiply.size(45, 45);
add.position(232, 238);
add.size(45, 45);
minus.position(232, 282);
minus.size(45, 45);
clear.position(100, 106);
clear.size(177, 45);
decimal.position(144, 282);
decimal.size(45, 45);
}
function clickedOne() {
number = number * 10 + 1;
}
function clickedTwo() {
number = number * 10 + 2;
}
function clickedThree() {
number = number * 10 + 3;
}
function clickedFour() {
number = number * 10 + 4;
}
function clickedFive() {
number = number * 10 + 5;
}
function clickedSix() {
number = number * 10 + 6;
}
function clickedSeven() {
number = number * 10 + 7;
}
function clickedEight() {
number = number * 10 + 8;
}
function clickedNine() {
number = number * 10 + 9;
}
function clickedZero() {
number = number * 10 + 0;
}
function clickedDivide() {
number1 = number;
number = 0;
operation = "÷";
}
function clickedMultiply() {
number1 = number;
number = 0;
operation = "x";
}
function clickedAdd() {
number1 = number;
number = 0;
operation = "+";
}
function clickedMinus() {
number1 = number;
number = 0;
operation = "-";
}
function clickedClear() {
number = 0;
number1 = 0;
operation = ""
}
function clickedDecimal() {
number = number + 0.0 ;
}
function pressedEquals() {
if (operation === "+") {
number = number1 + number;
} else if (operation === "-") {
number = number1 - number;
} else if (operation === "x") {
number = number1 * number
} if (operation === "÷") {
number = number1 / number
}
operation = "";
}
function draw() {
background(150);
noStroke();
fill(20, 255, 255, 30);
rect(100, 53, 177, 54, 5);
strokeWeight(6);
stroke(0);
noFill();
rect(97, 50, 183, 280, 5);
strokeWeight(1);
stroke(0);
fill(0);
textSize(25);
if (operation != "") {
let display = number1 + " " + operation + " " + number;
text(display, 110, 100);
} else {
text(number, 110, 100);
}
}