我已经尝试在代码中找出这个问题好几个小时了,有什么方法可以解决这个问题吗?如果是,请帮我解决这个问题。它运行,但while内部的代码没有按预期方式工作,即使条件有意义,或者我只是不知道while是如何工作的???不管怎样,谢谢你的帮助。这就是的所有细节
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
//imported JoptionPane for a basic UI
//Don't mind the comments
public class Calculator {
public static void main(String[] args) {
DecimalFormat format = new DecimalFormat("0.#####");
//Used this class to trim the zeroes in whole numbers and in the numbers with a trailing zero
String[] response = {"CANCEL", "Addition", "Subtraction", "Multiplication", "Division", "Modulo"};
//Used array to assign reponses in the response variable(will be used in the showOptionDialog())
//CANCEL is 0, Addition is 1, Subtraction is 2 and so on. Will be used later in switch statement
int selectCalculation = JOptionPane.showOptionDialog(null,
"SELECT CALCULATION THAT YOU WANT TO PERFORM:",
"SELECT CALCULATION",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
response,
null);
String numberInput1 = JOptionPane.showInputDialog(null, "ENTER THE FIRST NUMBER:");
//Used the class String instead of the data type double because I will be using a String method in while condition
while (numberInput1.matches("[a-zA-Z]"))
//Used regex here basically saying if numberInput1 does contain letter then start the loop until user inputs number
{
JOptionPane.showMessageDialog(null, "Make sure to enter numbers ONLY", "INVALID", JOptionPane.WARNING_MESSAGE);
//Shows warning message so user will know what the accepted input is
numberInput1 = JOptionPane.showInputDialog(null, "ENTER THE FIRST NUMBER:");
}
String numberInput2 = JOptionPane.showInputDialog(null, "ENTER THE SECOND NUMBER:");
//Same reason as stated previously
while (numberInput2.matches("[a-zA-Z]")) {
JOptionPane.showMessageDialog(null, "Make sure to enter numbers ONLY", "INVALID", JOptionPane.WARNING_MESSAGE);
numberInput2 = JOptionPane.showInputDialog(null, "ENTER THE SECOND NUMBER:");
}
double firstNumber = Double.parseDouble(numberInput1);
double secondNumber = Double.parseDouble(numberInput2);
//Converts the numberInputs to double so Java will add numbers instead of String later
switch (selectCalculation)
//Used switches instead of if else because it will be a longer process if I used if else
{
case 1:
double sum = firstNumber + secondNumber;
JOptionPane.showMessageDialog(null, format.format(firstNumber) + " and " + format.format(secondNumber) + " is equals to " + format.format(sum), "The sum of:", JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
double difference = firstNumber - secondNumber;
JOptionPane.showMessageDialog(null, format.format(firstNumber) + " and " + format.format(secondNumber) + " is equals to " + format.format(difference), "The difference of:", JOptionPane.INFORMATION_MESSAGE);
break;
case 3:
double product = firstNumber * secondNumber;
JOptionPane.showMessageDialog(null, format.format(firstNumber) + " and " + format.format(secondNumber) + " is equals to " + format.format(product), "The product of:", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
double quotient = firstNumber / secondNumber;
JOptionPane.showMessageDialog(null, format.format(firstNumber) + " and " + format.format(secondNumber) + " is equals to " + format.format(quotient), "The quotient of:", JOptionPane.INFORMATION_MESSAGE);
break;
case 5:
double remainder = firstNumber % secondNumber;
JOptionPane.showMessageDialog(null, format.format(firstNumber) + " and " + format.format(secondNumber) + " is equals to " + format.format(remainder), "The remainder of:", JOptionPane.INFORMATION_MESSAGE);
break;
default:
JOptionPane.showMessageDialog(null, "MADE BY: GABRIEL POTAZO", "CALCULATION CANCELLED",JOptionPane.ERROR_MESSAGE);
break;
}
}
}
嗨,您的问题在很多地方都不清楚,但我认为您只需要验证输入值,使其只有数字/字母/字母数字。
String numberInput1 = JOptionPane.showInputDialog(null, "ENTER THE FIRST NUMBER:");
//If its not Numbers matching
if (!Pattern.matches("[0-9]*", numberInput1)){
JOptionPane.showMessageDialog(null, "Make sure to enter numbers ONLY", "INVALID", JOptionPane.WARNING_MESSAGE);
//Clear your Input Box
}
只匹配字母
Pattern.matches("[a-zA-Z]*", numberInput1)
用于匹配字母数字
Pattern.matches("[a-zA-Z0-9]*", numberInput1)
我不相信使用Loops进行验证,但我建议您查看关于Dialogs的Swing教程,以及如何轻松使用JOptionPane,这样您就不需要验证输入了。