[英]JAVA How do I read an input from the user containing math operators and numbers in one expression?
I have an assignment that asks me to create a Basic calculator, by taking an input from the user in one expressions as in "4 - 1" or "5 + 3", and I need to check each number(1-9) and the operator (+,-,*,/) to see whether it is applicable.我有一个任务要求我创建一个基本计算器,通过从用户那里获取一个表达式中的输入,如“4 - 1”或“5 + 3”,我需要检查每个数字 (1-9) 和运算符 (+,-,*,/) 看是否适用。 My question is how do I check whether the expression has a valid operator and spaces in between numbers and operator?
我的问题是如何检查表达式是否具有有效的运算符以及数字和运算符之间的空格? (I'm a beginner in Java, and thank you in advance to whoever answers)
(我是Java的初学者,先谢谢回答的人)
Though I am not entirely sure where you are stuck, this example expression may be your best friend:虽然我不完全确定你被困在哪里,但这个示例表达式可能是你最好的朋友:
String exampleString = "Example String";
char exampleChar = example.charAt(3);
This statement is true:这个说法是正确的:
exampleChar == 'm'
EDIT FOR ADDITIONS编辑添加
To read the user input, you would need to read different types of characters.要读取用户输入,您需要读取不同类型的字符。 To read a number using the Scanner class, you would use .nextInt() or .nextDouble() to check for spaces and symbols, using .next() only reads till the next break.
要使用扫描仪 class 读取数字,您可以使用.nextInt()或.nextDouble()来检查空格和符号,使用.next()仅读取到下一个中断。 Please elaborate where you are getting stuck/where we can be more specific:)
请详细说明您遇到困难的地方/我们可以更具体的地方:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.