0

I have never taken a programming class before so I am very new to all of this and am having a bit of a challenge trying to get my answers to be displayed in writing. For example: if the user enters the numbers 2 and 5 and *, the answer should be displayed as two multiplied by five is 10. Here is my program: import java.util.Scanner;

 public class CalculatorProjectCH 
 {//begin class
 public static void main(String[] args) 
 {//begin main

//This program will ask the user to input two digits from 0-9 and then input a method of operation.

    System.out.println("This program will act as a simple calculator. ");

    System.out.println("It will ask you to enter two numbers from 0-9 and a method of operation " 
                      +"(+, -, *, /, ^.) ");

    //Declare variables input1, input2, result1, result2, result3, result4, and result5, as doubles.
    double input1, input2, result1, result2, result3, result4, result5; 
    String text;

    //Create scanner object to allow for input
    Scanner input = new Scanner (System.in);

    //Ask the user to enter the first number
    System.out.print("\nEnter your first number: ");  
    input1 = input. nextDouble();

    //Ask the user to enter the operation  
    System.out.println("Please enter the operation you would like to   execute: ");
    text = input.next(); 

    //Ask the user to enter the second number
    System.out.println("Enter your second number: ");
    input2 = input.nextDouble(); 

    result1= input1+input2;

    result2= input1-input2;

    result3= input1*input2;

    result4= input1/input2;

    result5= Math.pow(input1,input2);

    switch (text) 
    {

    case "+" :
        System.out.println(result1);
        break; 

    case "-" :  
        System.out.println(result2);
        break; 

    case "*" : 
        System.out.println(result3);
        break;  

    case "/" :  
        System.out.println(result4);
        break;

    case "^" :  
        System.out.println(result5);
        break; 

    //If the user did not enter a valid method of operation
    default : 
        System.out.println("Your operation was not recognized.");

    }
   }//end main
  }//end class
Cole H.
  • 21
  • 1
  • 2

3 Answers3

0

You need to link the int value to the String version of the word somehow. I would suggest using an array:

String[] wordNumbers = new String[]{"zero","one", "two", "three"..."nine"};

Now when you need to print the String version of a number, just do this:

System.out.println(wordNumbers[0]);

Output:

zero
Alex S
  • 572
  • 7
  • 15
0
case "*" : 
        System.out.println(input1 + " multiplied by " + input2 + " is " + result3);
        break;  

    case "/" :  
        System.out.println(input1 + " devided by " + input2 + " is " + result4);
        break;

So I didn't change your code, just added this little part as an example to make sure what your question is about, is this what you are looking for?? And if this is it, you can just add + and - on your own with this principle.