听起来我们正在做功课! :)确保你学习这些东西,否则它最终会咬你。你只能拖延不可避免的事情。有了这个“父亲的建议”,就在这里。
首先,您需要能够从控制台读取输入,以便您可以获取输入数字和操作。当然,已经有了完整的答案。一 链接 : Java:如何从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是否等于另一个字符串”。学习如何钓鱼比获取鱼更重要。我建议你做一些追赶,因为如果这是你的功课,你不确定那么你似乎有点落后。我不是故意粗鲁。我只是想长期帮助你。
输入第一个数字: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(.......){ }
希望能帮助到你 :)
这是我的解决方案
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"); } } }
的 祝好运!! 强>