幾個小時以來,我一直試圖找出代碼中的問題,有什么辦法可以解決這個問題嗎?如果是這樣,請幫我解決這個問題。它可以運行,但是 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;
}
}
}
uj5u.com熱心網友回復:
嗨,您的問題在很多地方都不清楚,但我相信您只需要驗證輸入值即可只有數字/字母/字母數字。
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 這樣您就不需要驗證輸入。
uj5u.com熱心網友回復:
如果您希望該用戶可以輸入期望的字母或數字,則用戶只能輸入特殊字符作為輸入
因為這很簡單
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".kotlin.DemoActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="50sp"
android:hint="Type something..."
android:inputType="text"
android:digits="!@#$%^*()-_/*- =[]{};:'?.,></" //here you can edit
/>
</RelativeLayout>
class DemoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/389858.html
上一篇:在Rails5.1之后,attribute_changed是如何變化的?更改“驗證”回呼?
下一篇:驗證ECDSAP-256簽名
