如何制作计算器

时间:2016-03-09 20:16:03

标签: java do-while

我正在尝试为课程创建一个计算器,我们必须显示操作,我们必须让它循环回到它所说的输入第一个数字的顶部,但也可以选择返回到顶部 每次我尝试添加一些东西,都会出错 关于如何根据我的想法制作这个计算器的任何想法?

import java.util.Scanner;

public class Calculator {
  public static void main(String[] argument) {
    {
      char repeat; //initialize repeat
      String input; //initialize input
      Scanner keyboard = new Scanner(System.in);
      System.out.println(" Please enter the first Number");
      int Number1 = keyboard.nextInt();
      System.out.println("Please enter the second number");
      int Number2 = keyboard.nextInt();
      System.out.println("Please enter the operation");
      keyboard.nextLine();
      {
        System.out.println(
            "The result of " + Number1 + " + " + Number2 + " = " + (Number1 + Number2));
        System.out.println(
            "The result of " + Number1 + " - " + Number2 + " = " + (Number1 - Number2));
        System.out.println(
            "The result of " + Number1 + " * " + Number2 + " = " + (Number1 * Number2));
        System.out.println(
            "The result of " + Number1 + " % " + Number2 + " = " + (Number1 % Number2));
        System.out.println(
            "The result of " + Number1 + " / " + Number2 + " = " + (Number1 / Number2));
        if (Number2 >= 0) System.out.println("Division by Zero is not possible.");
        System.out.println("Please run program again and ");
        System.out.println("enter a number other then zero.");
      }
      String userChoice = null;
      do {
        System.out.println("Would you like to " + "select again? ");
        System.out.print("Enter Y for yes or N for no: ");
        input = keyboard.nextLine();
        repeat = input.charAt(0);
      } while (repeat == 'Y' || repeat == 'y');
      System.out.println(userChoice); //give user option to go back to the top

      {
        keyboard.close();
      }
    }
  }
}

2 个答案:

答案 0 :(得分:2)

您的> head file1.txt | ./shell_scripts/print_columns.sh /dev/stdin col_1 col_4 col_6 col_2 | head 0185 10215 10195 10136 10166 10186 10146 10137 10167 10187 10147 10138 10168 10188 10148 10139 10169 10189 10149 应位于该方法的开头。这样我会执行一次然后询问用户他/她是否想重复这个过程。

do

答案 1 :(得分:0)

您的代码存在许多问题:

  1. 如果要提示用户反复输入数字,则需要移动do-while循环do {
  2. do { System.out.println(" Please enter the first Number"); int Number1 = keyboard.nextInt();

    1. 在检查Number2 == 0之前,你正在计算除法和mod。因此,如果number2是0,那么你的程序将抛出异​​常。您需要将其更改为:
    2. if (Number2 == 0) System.out.println("Division by Zero is not possible."); else { System.out.println("The result of " + Number1 + " % " + Number2 + " = " + (Number1 % Number2)); System.out.println("The result of " + Number1 + " / " + Number2 + " = " + (Number1 / Number2)); }

      1. 进行这些更改后,我在System.out.print("Enter Y for yes or N for no: "); keyboard.nextLine();收到错误,因为它不会等待用户输入。我查了一遍,看起来像扫描仪工作方式的问题,解释here
      2. 所以更改代码:

        System.out.print("Enter Y for yes or N for no: ");
        keyboard.nextLine();
        input = keyboard.nextLine();
        repeat = input.charAt(0);
        

        瞧!有用。 :)