Java:将数学方程式转换为String表达式?

时间:2022-10-17 21:24:37

I'm new to Java and I'm trying to figure out how I would write a math expression that displays the values of the variables on one line, then on the next line does the math?

我是Java的新手,我正在试图弄清楚如何编写一个数学表达式,在一行上显示变量的值,然后在下一行做数学运算?

Here is what I thought would work, but it just prints out the answers instead of the string representations on the top line of the addStuff() method.

这是我认为可行的,但它只打印出答案而不是addStuff()方法顶行的字符串表示。

public class DoSomeMath {
    int num1C = 3;
    int num1A = 7;
    public void addStuff(){
        //Show the Equation//
        System.out.println("Adding num1C + num1A: " + Integer.toString(num1C) + Integer.toString(num1A));
        //Show the Answer//
        System.out.println("Adding num1C + num1A: " + num1C + num1A);
    }
}

5 个解决方案

#1


Achieving the effect you want for this is even easier than you are making it:

实现您想要的效果比制作它更容易:

//Show the Equation//
System.out.println("Adding num1C + num1A: " + num1C + "+" + num1A);
//Show the Answer//
System.out.println("Adding num1C + num1A: " + (num1C + num1A));

The first line concatenates them as strings, while the second line forces the integer addition via parenthesis.

第一行将它们连接为字符串,而第二行通过括号强制添加整数。

#2


You are using a + operator in System.out.println(String str) When you use + sign for string's it normally does the task of appending the string in the string pool.

您在System.out.println中使用+运算符(String str)当您对字符串使用+符号时,它通常会执行在字符串池中附加字符串的任务。

//Show the Equation//
System.out.println("Adding num1C + num1A: " + Integer.toString(num1C) + 
"+"+ Integer.toString(num1A));
//Show the Answer//
System.out.println("Adding num1C + num1A: " + " " + (num1C + num1A));

So understand the use of + arithmetic operator with Strings and integer value.

因此理解使用带有字符串和整数值的+算术运算符。

#3


Your num1C and num1A are getting converted to Strings and appended as Strings. Use parentheses so the math happens first, then the String append last.

您的num1C和num1A将转换为字符串并作为字符串附加。使用括号,以便首先进行数学运算,然后将String追加到最后。

System.out.println("Adding num1C + num1A: " + (num1C + num1A));

#4


Try this:

public class DoSomeMath {
    int num1C = 3;
    int num1A = 7;
    public void addStuff(){
        //Show the Equation//
        System.out.println("Adding num1C + num1A: " + num1C + " + " + num1A);
        //Show the Answer//
        System.out.println("Adding num1C + num1A: " + (num1C + num1A));
    }
}

#5


Try making it + Integer.toString(num1C) + " + " + Integer.toString(num1A)

尝试制作+ Integer.toString(num1C)+“+”+ Integer.toString(num1A)

Any static characters you can enter as a string, and then concatenate with the variables.

您可以作为字符串输入的任何静态字符,然后与变量连接。

#1


Achieving the effect you want for this is even easier than you are making it:

实现您想要的效果比制作它更容易:

//Show the Equation//
System.out.println("Adding num1C + num1A: " + num1C + "+" + num1A);
//Show the Answer//
System.out.println("Adding num1C + num1A: " + (num1C + num1A));

The first line concatenates them as strings, while the second line forces the integer addition via parenthesis.

第一行将它们连接为字符串,而第二行通过括号强制添加整数。

#2


You are using a + operator in System.out.println(String str) When you use + sign for string's it normally does the task of appending the string in the string pool.

您在System.out.println中使用+运算符(String str)当您对字符串使用+符号时,它通常会执行在字符串池中附加字符串的任务。

//Show the Equation//
System.out.println("Adding num1C + num1A: " + Integer.toString(num1C) + 
"+"+ Integer.toString(num1A));
//Show the Answer//
System.out.println("Adding num1C + num1A: " + " " + (num1C + num1A));

So understand the use of + arithmetic operator with Strings and integer value.

因此理解使用带有字符串和整数值的+算术运算符。

#3


Your num1C and num1A are getting converted to Strings and appended as Strings. Use parentheses so the math happens first, then the String append last.

您的num1C和num1A将转换为字符串并作为字符串附加。使用括号,以便首先进行数学运算,然后将String追加到最后。

System.out.println("Adding num1C + num1A: " + (num1C + num1A));

#4


Try this:

public class DoSomeMath {
    int num1C = 3;
    int num1A = 7;
    public void addStuff(){
        //Show the Equation//
        System.out.println("Adding num1C + num1A: " + num1C + " + " + num1A);
        //Show the Answer//
        System.out.println("Adding num1C + num1A: " + (num1C + num1A));
    }
}

#5


Try making it + Integer.toString(num1C) + " + " + Integer.toString(num1A)

尝试制作+ Integer.toString(num1C)+“+”+ Integer.toString(num1A)

Any static characters you can enter as a string, and then concatenate with the variables.

您可以作为字符串输入的任何静态字符,然后与变量连接。