Java简单英文计算器,输出问题

huangapple go评论107阅读模式
英文:

Java Simple English Calculator, output issue

问题

import java.util.Scanner;

public class Project {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char operation;
        int num1, num2;

        // Asks for user to input first number
        System.out.println("Please enter the first number (0-9)");
        num1 = input.nextInt();

        // Asks user for an operation
        System.out.println("Please enter the type of operation that you would like to perform");
        operation = input.next().charAt(0);

        // Asks user to input the second number
        System.out.println("Please enter the second number (0-9)");
        num2 = input.nextInt();

        // Limits the numbers to the range of 0-9
        if (num1 > 9 || num2 > 9) {
            System.out.println("Invalid Digit!!");
            System.exit(0);
        }

        // An array that converts number into a string
        String num[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        switch (operation) {

            case '-': // Subtraction conversion
                System.out.println(num[num1] + " minus " + num[num2] + " is " + (num1 - num2));
                break;

            case '+': // Addition conversion
                System.out.println(num[num1] + " plus " + num[num2] + " is " + (num1 + num2));
                break;

            case '*': // Multipication conversion
                System.out.println(num[num1] + " multiplied by " + num[num2] + " is " + (num1 * num2));
                break;

            case '/': // Division conversion
                System.out.println(num[num1] + " divided by " + num[num2] + " is " + (num1 / num2));
                break;

            case '^': // Exponentiation conversion

                if (num2 == 0) { // checks to see if the second number entered is 0
                    System.out.println("Error: Cannot divide by Zero"); // Divide by Zero error message
                    break;
                }

                System.out.println(num[num1] + " to the power of " + num[num2] + " is " + Math.pow(num1, num2));
                break;

            default:
                System.out.println("Error: Invalid Operation Entered");
        }
    }
}
英文:

I am writing a calculator program that takes inputs from 0-9 and outputs the results in both numerical from and in plain english (Ex: Two plus three equals 5). I have gotten it to be able to print out the result in words but I am now stuck on how to get it to print out the original problem in numerical form. The output must include both the result in numbers and in words.

The numbers have already been converted into strings through a switch statement but is there anyway I can print out the original problem? If not would I have to completely restructure this instead? Any help would be appreciated I have been stuck on this for a while now.

Here is my code

import java.util.Scanner;
public class Project {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char operation;
int num1, num2;
//Asks for user to input first number
System.out.println("Please enter the first number (0-9)");
num1 = input.nextInt();
//Asks user for an operation
System.out.println("Please enter the type of operation that you would like to perform");
operation = input.next().charAt(0);
//Asks user to input the second number
System.out.println("Please enter the second number (0-9)");
num2 = input.nextInt();
//Limits the numbers to the range of 0-9 
if(num1 > 9 || num2 > 9){
System.out.println("Invalid Digit!!");
System.exit(0);}
//An array that converts number into a string
String num[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
switch(operation){
case '-': //Subtraction conversion
System.out.println(num[num1]+" minus "+num[num2] + " is " + (num1-num2));
break;
case '+':   //Addition conversion
System.out.println(num[num1]+" plus "+num[num2] + " is " + (num1+num2));
break;   
case '*':   //Multipication conversion
System.out.println(num[num1]+" multiplied by "+num[num2] + " is " + (num1*num2));
break;
case '/': //Division conversion
System.out.println(num[num1]+" divided by "+num[num2] + " is " + (num1/num2));
break;
case '^':   //Exponentiation conversion
if(num2 == 0){ //checks to see if the second number entered is 0
System.out.println("Error: Cannot divide by Zero"); //Divide by Zero error message
break;}
System.out.println(num[num1]+" to the power of "+num[num2] + " is " + Math.pow(num1,num2));
break;
default:
System.out.println("Error: Invalid Operation Entered"); }
}
}

答案1

得分: 1

非常简单,我认为你有些混淆。考虑以下几点:

  • 你的原始数字保存在 num1num2 中,所以如果你想以整数形式显示原始数字,那么只需使用 num1,而不是 num[num1]
  • 在计算指数时,你正在检查“零除法”。你应该将这个检查移到除法的情况下。

将你的 switch 替换为以下内容:

switch (operation) {

    case '-': //减法转换
        System.out.println(num[num1] + " 减去 " + num[num2] + " 等于 " + (num1 - num2));
        System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
        break;

    case '+':   //加法转换
        System.out.println(num[num1] + " 加上 " + num[num2] + " 等于 " + (num1 + num2));
        System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
        break;

    case '*':   //乘法转换
        System.out.println(num[num1] + " 乘以 " + num[num2] + " 等于 " + (num1 * num2));
        System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
        break;

    case '/': //除法转换
        if (num2 == 0) { //检查第二个输入的数是否为0

            System.out.println("错误:不能除以零"); //除以零错误消息
            break;
        }
        System.out.println(num[num1] + " 除以 " + num[num2] + " 等于 " + (num1 / num2));
        System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
        break;

    case '^':   //指数转换
        System.out.println(num[num1] + " 的 " + num[num2] + " 次方等于 " + Math.pow(num1, num2));
        System.out.println(num1 + " ^ " + num2 + " = " + Math.pow(num1, num2));
        break;

    default:
        System.out.println("错误:输入的操作无效");
}
英文:

Its very simple, I think you are getting confused. Consider following points:

  • You have original numbers saved in num1 and num2, so if you want to show the original numbers in integer form, then just use num1 instead of num[num1]
  • You are checking for zero division when finding exponent. You should shift this check to division case.

Replace your switch like this:

switch(operation){

	case '-': //Subtraction conversion
		System.out.println(num[num1]+" minus "+num[num2] + " is " + (num1-num2));
		System.out.println(num1+" - "+num2 + " = " + (num1-num2));
		break;

	case '+':   //Addition conversion
		System.out.println(num[num1]+" plus "+num[num2] + " is " + (num1+num2));
		System.out.println(num1+" + "+num2 + " = " + (num1+num2));
		break;   

	case '*':   //Multipication conversion
		System.out.println(num[num1]+" multiplied by "+num[num2] + " is " + (num1*num2));
		System.out.println(num1+" * "+num2 + " = " + (num1*num2));
		break;

	case '/': //Division conversion
		if(num2 == 0){ //checks to see if the second number entered is 0

			System.out.println("Error: Cannot divide by Zero"); //Divide by Zero error message
			break;
		}
		System.out.println(num[num1]+" divided by "+num[num2] + " is " + (num1/num2));
		System.out.println(num1+" / "+num2 + " = " + (num1/num2));
		break;

	case '^':   //Exponentiation conversion
		System.out.println(num[num1]+" to the power of "+num[num2] + " is " + Math.pow(num1,num2));
		System.out.println(num1+" ^ "+num2 + " = " + Math.pow(num1,num2));
		break;

	default:
		System.out.println("Error: Invalid Operation Entered"); 
}

答案2

得分: 0

看看下面的打印语句是否有帮助

case '-': // 减法转换
System.out.println(num[num1] + " 减去 " + num[num2] + " 是 " + (num1 - num2));
System.out.println(num1 + " " + operation + " " + num2 + " = " + (num1 - num2));
break;
case '+':   // 加法转换
System.out.println(num[num1] + " 加上 " + num[num2] + " 是 " + (num1 + num2));
System.out.println(num1 + " " + operation + " " + num2 + " = " + (num1 + num2));	
break;
英文:

See if the below print statements helps

case '-': //Subtraction conversion
System.out.println(num[num1]+" minus "+num[num2] + " is " + (num1-num2));
System.out.println(num1+" "+operation + "  "+num2+ "="+(num1 - num2));
break;
case '+':   //Addition conversion
System.out.println(num[num1]+" plus "+num[num2] + " is " + (num1+num2));
System.out.println(num1+" "+operation + "  "+num2+ "="+(num1 + num2));	
break;

答案3

得分: 0

这是你要的翻译内容:

这是你所寻找的吗?例如,对于 9 + 9,输出将为:

九加九等于十八
9 + 9 = 18

import java.util.Scanner;
public class Project {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char operation;
        int num1, num2;

        // 要求用户输入第一个数字
        System.out.println("请输入第一个数字(0-9)");
        num1 = input.nextInt();

        // 要求用户输入操作类型
        System.out.println("请输入要执行的操作类型");
        operation = input.next().charAt(0);

        // 要求用户输入第二个数字
        System.out.println("请输入第二个数字(0-9)");
        num2 = input.nextInt();


        // 限制数字在 0-9 范围内
        if(num1 > 9 || num2 > 9){

            System.out.println("无效数字!!");
            System.exit(0);
        }

        // 将数字转换为字符串的数组
        String num[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        switch(operation){

            case '-': // 减法转换
                System.out.println(num[num1]+" 减 "+num[num2] + " 等于 " + (num1-num2));
                System.out.println(num1+" "+operation + "  "+num2+ " = "+(num1 - num2));
                break;

            case '+':   // 加法转换
                System.out.println(num[num1]+" 加 "+num[num2] + " 等于 " + (num1+num2));
                System.out.println(num1+" "+operation + "  "+num2+ " = "+(num1 + num2));
                break;   

            case '*':   // 乘法转换
                System.out.println(num[num1]+" 乘以 "+num[num2] + " 等于 " + (num1*num2));
                System.out.println(num1+" "+operation + "  "+num2+ " = "+(num1 * num2));
                break;

            case '/': // 除法转换
                System.out.println(num[num1]+" 除以 "+num[num2] + " 等于 " + (num1/num2));
                System.out.println(num1+" "+operation + "  "+num2+ " = "+(num1 / num2));
                break;

            case '^':   // 指数转换

                if(num2 == 0){ // 检查第二个输入的数字是否为0

                    System.out.println("错误:不能除以零"); // 除以零错误消息
                    break;
                }

                System.out.println(num[num1]+" 的 "+num[num2] + " 次方等于 " + Math.pow(num1,num2));
                System.out.println(num1+" "+operation + "  "+num2+ " = "+(Math.pow(num1,num2)));
                break;

            default:
                System.out.println("错误:无效的操作输入");
        }
    }
}
英文:

is it this what you are looking for? for example for 9 + 9 your output will be:

> nine plus nine is 18<br> 9 + 9 = 18

import java.util.Scanner;
public class Project {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char operation;
int num1, num2;
//Asks for user to input first number
System.out.println(&quot;Please enter the first number (0-9)&quot;);
num1 = input.nextInt();
//Asks user for an operation
System.out.println(&quot;Please enter the type of operation that you would like to perform&quot;);
operation = input.next().charAt(0);
//Asks user to input the second number
System.out.println(&quot;Please enter the second number (0-9)&quot;);
num2 = input.nextInt();
//Limits the numbers to the range of 0-9 
if(num1 &gt; 9 || num2 &gt; 9){
System.out.println(&quot;Invalid Digit!!&quot;);
System.exit(0);}
//An array that converts number into a string
String num[] = {&quot;zero&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;};
switch(operation){
case &#39;-&#39;: //Subtraction conversion
System.out.println(num[num1]+&quot; minus &quot;+num[num2] + &quot; is &quot; + (num1-num2));
System.out.println(num1+&quot; &quot;+operation + &quot;  &quot;+num2+ &quot; = &quot;+(num1 - num2)); 
break;
case &#39;+&#39;:   //Addition conversion
System.out.println(num[num1]+&quot; plus &quot;+num[num2] + &quot; is &quot; + (num1+num2));
System.out.println(num1+&quot; &quot;+operation + &quot;  &quot;+num2+ &quot; = &quot;+(num1 + num2)); 
break;   
case &#39;*&#39;:   //Multipication conversion
System.out.println(num[num1]+&quot; multiplied by &quot;+num[num2] + &quot; is &quot; + (num1*num2));
System.out.println(num1+&quot; &quot;+operation + &quot;  &quot;+num2+ &quot; = &quot;+(num1 * num2)); 
break;
case &#39;/&#39;: //Division conversion
System.out.println(num[num1]+&quot; divided by &quot;+num[num2] + &quot; is &quot; + (num1/num2));
System.out.println(num1+&quot; &quot;+operation + &quot;  &quot;+num2+ &quot; = &quot;+(num1 / num2)); 
break;
case &#39;^&#39;:   //Exponentiation conversion
if(num2 == 0){ //checks to see if the second number entered is 0
System.out.println(&quot;Error: Cannot divide by Zero&quot;); //Divide by Zero error message
break;}
System.out.println(num[num1]+&quot; to the power of &quot;+num[num2] + &quot; is &quot; + Math.pow(num1,num2));
System.out.println(num1+&quot; &quot;+operation + &quot;  &quot;+num2+ &quot; = &quot;+(Math.pow(num1,num2))); 
break;
default:
System.out.println(&quot;Error: Invalid Operation Entered&quot;); }
}
}

huangapple
  • 本文由 发表于 2020年5月4日 17:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61588538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定