简体   繁体   English

JAVA 如何从一个表达式中读取包含数学运算符和数字的用户输入?

[英]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.

相关问题 如何从Java中的Windows Math输入面板读取发布到系统剪贴板的数据。 数据味道是什么? - How do I read data posted to the System Clipboard from Windows Math Input Panel in Java. What is the data flavor? 如何使Java从文本中读取数字列表? - How do I get Java to read a list of numbers from a text? 如何在 Java 中读取两个点运算符? - How do I read two dot operators in Java? 如何打印用户在Java中输入的字符串的一个字母? - How do I print one letter of a string that was input by the user in Java? 如何从Java中获取用户的输入(控制台)? - How do I get input from the user in Java (console)? 如何在JFrame Java中从用户输入中绘制字符串 - How do I draw string from user input in a JFrame Java 使用循环读取数学表达式,使用单独的数字 - use loop to read math expression, separate numbers 使用Java,如何接受用户输入中包含数字和字母的短语并对其进行分类? - Using Java, how do I accept and categorize a phrase that has numbers and letters in user input? 如何让 java 只接受用户输入的数字 1-5? - How do I get java to only accept the user input of numbers 1-5? 如何根据用户输入显示偶数和奇数? -Java - How do I display even and odd numbers based on user input? - java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM