我提示“编写执行以下操作的程序:+, - ,*,/,%(由Java定义)。您的程序应首先读取第一个数字,然后是操作,然后是两个数字都是整数。如果操作中用户输入了上面显示的符号之一,程序应该执行相应的操作并计算结果。之后应该打印操作和结果。如果操作如果无法识别,程序应显示有关它的消息。您不需要对整数进行任何输入验证。“
我给出的示例输出是:
Enter the first number: 6
Enter the operation: +
Enter the second number: 10
6 + 10 = 16
我该如何开始这个?我非常困惑,对java很新!非常感谢任何帮助。
答案 0 :(得分:1)
您通常首先想要从STDIN开始读取输入:
Scanner in = new Scanner(System.in);
然后,我会读取所有参数,然后执行计算:
System.out.print("Enter the first number: ");
int left = Integer.parseInt(in.nextLine());
System.out.print("Enter the operation: ");
String operation = in.nextLine();
System.out.print("Enter the second number: ");
int right = Integer.parseInt(in.nextLine());
现在收集了所有输入,您就可以开始行动了。
int result;
switch(operation)
{
case "+": result = left + right; break;
case "-": result = left - right; break;
case "*": result = left * right; break;
case "/": result = left / right; break;
case "%": result = left % right; break;
default: throw new IllegalArgumentException("unsupported operation " + operation);
}
System.out.println(left + " " + operation + " " + right + " = " + result);
答案 1 :(得分:0)
要读取整数,请使用扫描仪
public static void main(String [] args)
{
Scanner stdin = new Scanner(System.in);
System.out.println("Enter the first number: ");
int firstNum = stdin.nextInt(); //first number
System.out.println("Enter the operation: ");
String operation = stdin.next(); //operation
System.out.println("Enter the second number: ");
int secondNum = stdin.nextInt(); //second number
doOperation(firstNum, secondNum, operation);
}
public static void doOperation(int firstNum, int secondNum, String operation)
{
if(operation.equals("+")
{
int result = firstNum + secondNum;
}
else if(...)
{
//etc
}
System.out.println(firstNum + " " + operation + " " + secondNum + " = " + result);
}
答案 2 :(得分:0)
听起来我们正在做功课! :)确保你学习这些东西,否则它最终会咬你。你只能拖延不可避免的事情。有了这个“父亲的建议”,就在这里。
首先,您需要能够从控制台读取输入,以便您可以获取输入数字和操作。当然,已经有了完整的答案。一个link: Java: How to get input from System.console()
获得输入后,即可使用它。
您需要查看输入的项目。他们说您不需要验证数字,但需要验证操作。因此,从控制台获取操作后,查看操作String变量,看看它是否为“equalsIgnoreCase”(或者只是等于,因为这些符号没有大写),每个接受的操作。如果它不等于它们中的任何一个,那么你应该打印出它所说的信息。 (再次使用System.out.println)。
然后你可以进入一些if条件,并且如果操作等于其中一个项目,则实际进行数学计算。例如:
if(inputOperation.equalsIgnoreCase("+")){
double solution = inputInt1 + inputInt2;
//Need to do for all other operations. I didn't do the WHOLE thing for you.
}else if(NEED_TO_FILL_IN_THIS){
//Need to fill in the operation.
//You will need to have more else if conditions below for every operation
}else{
System.out.println("Your operation of '"+inputOperation+"' did not match any accepted inputs. Accepted input operations are '+','-','%','/' and '*'. Please try again.");
}
System.out.println("Your answer to the equation '"+inputInt1+" "+inputOperation+" "+inputInt2+"' is the following:"+solution);
这应该让你开始。如果您还需要进一步的指导,请告诉我。
我希望有所帮助!
并以一些父亲的建议结束:再说一遍,听起来你正在做作业。如果您只是知道如何谷歌,这一切都有很好的记录。 “Java从控制台获取输入”。或者“Java检查String是否等于另一个字符串”。学习如何钓鱼比获取鱼更重要。我建议你做一些追赶,因为如果这是你的功课,你不确定那么你似乎有点落后。我不是故意粗鲁。我只是想长期帮助你。
答案 3 :(得分:0)
输入第一个数字:6 输入操作:+ 输入第二个数字:10 6 + 10 = 16
Scanner f=new Scanner(System.in)
System.out.print("Enter the first number: ")
int firstNum=f.nextInt();
System.out.println();
System.out.print("Enter the operation: ")
String Op=f.nextLine();
System.out.println();
System.out.print("Enter the Second number: ")
int secNum=f.nextInt();
System.out.println();
int answ=0;
if(Op.equals("+"){
answ=firstNum+secNum;
}else if(.......){
}
希望它有所帮助:)
答案 4 :(得分:-2)
这是我的解决方案
package com.company;
import com.sun.org.apache.regexp.internal.RE;
import java.util.Scanner;
public class Main {
private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args) {
// write your code here
int First,Second,Resualt;
char operation;
System.out.println("Enter the first number: ");
First=scanner.nextInt();
System.out.println("Enter the operation:");
operation=scanner.next().charAt(0);
System.out.println("Enter the second number :");
Second=scanner.nextInt();
if (operation=='+'){
Resualt=First+Second;
System.out.println(First+" "+"+ "+Second+" = "+Resualt);
}
else if (operation=='-'){
Resualt=First-Second;
System.out.println(First+" "+"- "+Second+" = "+Resualt);
}
else if (operation=='*'){
Resualt=First*Second;
System.out.println(First+" "+"* "+Second+" = "+Resualt);
}
else if (operation=='%'){
Resualt=First%Second;
System.out.println(First+" "+"% "+Second+" = "+Resualt);
}
else {
System.out.println("Error");
}
}
}
祝你好运!