真的不知道从哪里开始,多项选择操作?

Really confused on where to begin, multiple choice operations?

我有提示"Write a program that performs the following operations: +, -, *, /, % (as defined by Java). Your program should first read the first number, then the operation, and then the second number. Both numbers are integers. If, for the operation, the user entered one of the symbols shown above, your program should perform the corresponding operation and compute the result. After that it should print the operation and the result. If the operation is not recognized, the program should display a message about it. You do not need to do any input validation for the integers."

我得到的示例输出是:

Enter the first number: 6
Enter the operation: +
Enter the second number: 10
6 + 10  =  16

我该如何开始呢?我非常困惑,对 java 真的很陌生!非常感谢任何帮助。

要读取整数,请使用扫描器

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);
}

您通常首先要开始读取来自 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);

听起来我们正在做你的功课! :) 确保你学会了这些东西,否则它最终会咬你一口。你只能推迟不可避免的事情。有了那个 "fatherly advice",开始吧。

首先,你需要能够从控制台读取输入,这样你才能得到输入的数字和操作。当然,对此已经有了完整的答案。一 link: Java: How to get input from System.console()

获得输入后,即可使用它。

您需要查看输入的项目。他们说你不需要验证数字,但你需要验证操作。因此,在从控制台获取操作字符串变量后,查看它是否是每个接受的操作的 "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);

这应该可以帮助您入门。如果您还需要进一步指导,请告诉我。

希望对您有所帮助!

并以一些父亲般的建议作为结尾:再说一遍,听起来你像是在做作业。如果您只知道如何google,那么这一切都有很好的记录。 "Java get input from console"。或者 "Java check if String is equal to another 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");
        }

    }
}

祝你好运!!