Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * class of the womework for HWv1v12v17 .
- *
- * @author (Noah)
- * @version (1/12/17)
- */
- import java.util.*; //importing various libraries
- import java.lang.*;
- import javax.swing.*;
- import java.text.*;
- //these are left over from the rest of the methods that are included in this for me, class but not included here
- public class HWv1v12v17
- {
- public static void prob55()
- {
- System.out.println("55."); //print the problem number
- Scanner scanin = new Scanner(System.in);
- int num1;
- String Num1="";
- String Num2="";
- int num2;
- String operate;
- String operateW = "";
- double result = 0;
- boolean answ = true;
- String again = "";
- do
- {
- System.out.println();
- do //ask for the user to enter a integer until they enter something thats from 0 to 9
- {
- System.out.print("Enter a number from 0 to 9: ");
- num1 = scanin.nextInt();
- System.out.println();
- }
- while (num1<0 || num1 >= 10);
- do //ask for the user to input one of the specified operator types. if you look at the while statement, you'll see that it will actually accept more than just the specified versions of the stated operator
- {
- System.out.print("Enter your operation type (+,-,x,/, or ^): ");
- operate = scanin.next();
- }
- while (!operate.equalsIgnoreCase("+") && !operate.equalsIgnoreCase("add") && !operate.equalsIgnoreCase("addition") && !operate.equalsIgnoreCase("-") && !operate.equalsIgnoreCase("minus") && !operate.equalsIgnoreCase("subtract") && !operate.equalsIgnoreCase("subtraction") && !operate.equalsIgnoreCase("x") && !operate.equalsIgnoreCase("multiply") && !operate.equalsIgnoreCase("times") && !operate.equalsIgnoreCase("/") && !operate.equalsIgnoreCase("divide") && !operate.equalsIgnoreCase("division") && !operate.equalsIgnoreCase("^") && !operate.equalsIgnoreCase("power"));
- System.out.println();
- if (operate.equalsIgnoreCase("/") || operate.equalsIgnoreCase("divide") || operate.equalsIgnoreCase("division" ))
- //if the user has entered a division operator, than this while loop will repeat until it gets an input from 1 to 9 (not zero)
- {
- do
- {
- System.out.print("Enter your second single didget number (1 to 9). You cannot divide by zero: ");
- num2 = scanin.nextInt();
- System.out.println();
- }
- while (num2 <= 0 || num1 >= 10);
- }
- else
- //else it will accept 0-9
- {
- do
- {
- System.out.print("Enter your second single digit number (0 to 9) : ");
- num2 = scanin.nextInt();
- System.out.println();
- }
- while (num2 < 0 || num1 >= 10);
- }
- System.out.println();
- switch (num1)//this gets the spelt out version of the first number
- {
- case 0:
- Num1 = "Zero";
- break;
- case 1:
- Num1 = "One";
- break;
- case 2:
- Num1 = "Two";
- break;
- case 3:
- Num1 = "Three";
- break;
- case 4:
- Num1 = "Four";
- break;
- case 5:
- Num1 = "Five";
- break;
- case 6:
- Num1 = "Six";
- break;
- case 7:
- Num1 = "Seven";
- break;
- case 8:
- Num1 = "Eight";
- break;
- case 9:
- Num1 = "Nine";
- break;
- default:
- System.out.println("invalid first integer");
- }
- switch (num2)//this makes a spelt out version of the second number
- {
- case 0:
- Num2 = "Zero";
- break;
- case 1:
- Num2 = "One";
- break;
- case 2:
- Num2 = "Two";
- break;
- case 3:
- Num2 = "Three";
- break;
- case 4:
- Num2 = "Four";
- break;
- case 5:
- Num2 = "Five";
- break;
- case 6:
- Num2 = "Six";
- break;
- case 7:
- Num2 = "Seven";
- break;
- case 8:
- Num2 = "Eight";
- break;
- case 9:
- Num2 = "Nine";
- break;
- default:
- System.out.println("invalid second integer");
- }
- //the following if and elseif statements find the spelt out version of the operation (or operation word, operateW) and sets a double value for the result
- //i did not use switch-case because i wasnt sure how to do that with a string
- if (operate.equalsIgnoreCase("+") || operate.equalsIgnoreCase("add") || operate.equalsIgnoreCase("addition"))
- {
- operateW = "added to";
- result = num1+num2;
- }
- else if (operate.equalsIgnoreCase("-") || operate.equalsIgnoreCase("minus") || operate.equalsIgnoreCase("subtract") || operate.equalsIgnoreCase("subtraction"))
- {
- operateW = "minus";
- result = num1-num2;
- }
- else if (operate.equalsIgnoreCase("x") || operate.equalsIgnoreCase("multiply") || operate.equalsIgnoreCase("times"))
- {
- operateW = "times";
- result = num1*num2;
- }
- else if (operate.equalsIgnoreCase("/") || operate.equalsIgnoreCase("divide") || operate.equalsIgnoreCase("division" ))
- {
- operateW = "divided by";
- result = (double)num1/ num2;
- }
- else if (operate.equalsIgnoreCase("^") || operate.equalsIgnoreCase("power"))
- {
- operateW = "to the power of";
- result = Math.pow(num1,num2);
- }
- System.out.println(Num1 + " "+ operateW+ " " +Num2+ " equals " + result); //prints the results
- System.out.println();
- System.out.println();
- do
- {
- System.out.println("Would you like to calculate another number? Answer \"Y\" for yes or \"N\" for no:");
- again = scanin.next();
- }
- while (!again.equalsIgnoreCase("y") && !again.equalsIgnoreCase("n"));
- System.out.println();
- if (again.equalsIgnoreCase("n"))
- {
- answ = false;
- }
- }
- while (answ);
- System.out.println("Goodbye! :)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement