我可以寻求帮助吗?

时间:2019-10-14 21:03:58

标签: java input output newline removing-whitespace

当我运行代码时,我在mimir中遇到一个错误(如果大家知道,idk,但它是一个平台,教师可用来检查您提交的代码的测试用例),说我有一个空格不应该在那里(查看图片)。

error in my code 1.)我该如何解决我的代码。

另一个让我发疯的问题是, 我该怎么做,以便当我输入字母时,在这种情况下,它被输入为'f'(检查图像)以显示“输入两个整数:”,就像想象的那样。我尝试了不同类型的循环并更改了布局,但我从来没有正确过。 output of how its supposed to be

p.d。是的,这是作业,应于昨天完成。即使我在这方面取得了不错的成绩,但仍然令我感到困惑,我无法弄清楚这两件事。

import java.util.InputMismatchException;
import java.util.Scanner;

public class MathTeacher {


  public static int addNumbers(int n1, int n2){
    int add = n1+n2;
    return add;
  }

  public static int subtractNumbers(int n1, int n2){
    int subs = n1-n2;
    return subs;
  }

  public static int multiplyNumbers(int n1, int n2){
    int mulp = n1*n2;
    return mulp;
  }

  public static int divideNumbers(int n1, int n2){
    int div = n1/n2;
    return div;
  }

  private static int getIntFromUser(Scanner scan) {
    int x;
    while (true) {
      try {
        x = scan.nextInt();
        break;
      } catch (InputMismatchException e) {
        scan.next();
      }
    }
    return x;
  }
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    System.out.println("Welcome to *Mental Math Practice* where you can test your addition, subtraction, multiplication, and division.");
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter two integers: ");
    String typeQuit= "";
    do {
      int choices;

      int n1 = getIntFromUser(scanner);
      int n2 = getIntFromUser(scanner);
      System.out.println("Enter 1 to add the two numbers.");
      System.out.println("Enter 2 to subtract the second number from the first number.");
      System.out.println("Enter 3 to multiply the two numbers.");
      System.out.println("Enter 4 to divide the first number by the second number.");

      choices = scanner.nextInt();
      switch (choices) {
        case 1: {
          int add = addNumbers(n1, n2);
          System.out.println(add);
          break;
        }
        case 2: {

          int sub = subtractNumbers(n1, n2);
          System.out.println(sub);
          break;
        }
        case 3: {

          int mulp = multiplyNumbers(n1, n2);
          System.out.println(mulp);
          break;
        }
        case 4: {

          int div = divideNumbers(n1, n2);
          System.out.println(div);
          break;
        }

      }
      System.out.println("Enter 'Quit' to end the program.");
      typeQuit = scanner.next();
    } while(!typeQuit.equals("Quit"));
  }
}

0 个答案:

没有答案