如何在Java

时间:2015-11-11 04:48:44

标签: java

import java.util.*;

public class TestProject 
{
    public static void theMath()
    {
        double add = 1;
        double subtract = 2;
        double multiply = 3;
        double divide = 4;

        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);

        //Pick first number
        System.out.println("Please enter a number: ");
        int intOne = input.nextInt();

        // Pick second number
        System.out.println("Please enter another number: ");
        int intTwo = input.nextInt();

        //User chooses operator
        System.out.println("Now please choose an operator (1 for add, 2 for subtract, 3 for mulitply, 4 for divide): ");
        int userChoice = input.nextInt();
        // Add
        if (userChoice == add)
            System.out.println("Your answer is: " + (intOne + intTwo));
        // Subtract
        else if (userChoice == subtract)
            System.out.println("Your answer is: " + (intOne - intTwo));
        // Multiply
        else if (userChoice == multiply)
            System.out.println("Your answer is: " + (intOne * intTwo));
        // Divide
        else if (userChoice == divide)
            System.out.println("Your answer is: " + (intOne / intTwo));
        // If wrong input
        else
        {
            System.out.println("Nothing happens!");
            System.out.println("Please make sure you entered a number and an operator.");
        }
    }

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        theMath();

        System.out.println("Would you like to do another calculation?");
        String redo = input.nextLine();

        if(redo.equals("yes"))
            theMath();
        else if(redo.equals("no"))
            System.out.println("Thanks for calculating with me! It certainly was fun!");
        else
            System.out.println("Please enter 'yes' or 'no' only.");
            String yesNo = input.nextLine();
        if(yesNo.equals("yes"))
                theMath();
        else
                System.out.println("Thanks for calculating with me! It certainly was fun!");
    }
}

我想知道如果我愿意,我可以无限次地回忆主要方法。我正在做的只是一遍又一遍地复制和粘贴它,但必须有一个更好的方法。而且,我想知道如何返回一个带小数的值(所以我可以做25/6并得到正确的答案)。

1 个答案:

答案 0 :(得分:1)

为什么不在循环中只放置应该重复的语句?

String redo;
do{
    System.out.println("Would you like to do another calculation?");
    redo = input.nextLine();

    if(redo.equals("yes"))
        theMath();
    else if(redo.equals("no"))
        System.out.println("Thanks for calculating with me! It certainly was fun!");
    else
        System.out.println("Please enter 'yes' or 'no' only.");
        String yesNo = input.nextLine();
    if(yesNo.equals("yes"))
            theMath();
    else
        System.out.println("Thanks for calculating with me! It certainly was fun!");
}while(redo.equals("yes"))

至于你问题的其他部分。如果你有两个int值并希望从一个除法得到一个小数,你可以这样做:

int x = 2;
int y = 3;
double result = (double)x/y;
System.out.println(result);

这称为铸造。