简体   繁体   English

Java-将变量传递到数字的末尾

[英]Java- Passing a variable to the end of a number

I'm trying to figure out how to pass in a variable into random parts of a math problem. 我试图弄清楚如何将变量传递到数学问题的随机部分中。 This is a simple example of my problem 这是我的问题的一个简单例子

public class EquationY { 公共类EquationY {

public static void main(String[] args) {

     double x = 5;

double y = 4.6x + 34.2 ;

 System.out.println("y is " + y);
}

} }

All I need to do is change the x to a 5 so it will read 4.65 + 34.2 我需要做的就是将x更改为5,这样它将显示为4.65 + 34.2


For any of you that might not be fed up with me yet, let me know if this code is acceptable or should I start looking to find an easier way 对于您可能还不满意我的任何人,请让我知道这段代码是否可以接受,或者我应该开始寻找一种更简单的方法

12.3x^4 - 9.1x^3 + 19.3x^2 - 4.6x + 34.2 12.3x ^ 4-9.1x ^ 3 + 19.3x ^ 2-4.6x + 34.2

public static void main(String[] args) { 公共静态void main(String [] args){

     double x = 5;

double y = Math.pow(12.3 + x/100, 4) - Math.pow(9.1 + x/100, 3) + Math.pow(19.3 + x/100, 2) - (4.6 + x/100) + 34.2 ;

 System.out.println("y is " + y);
}

y is 22901.02463125001 y是22901.02463125001

Assuming that you're trying to take a number and insert it to any location within any value in an equation, you probably should be treating all the values in the equation as strings. 假设您尝试取一个数字并将其插入方程式中任何值的任何位置,则可能应该将方程式中的所有值都视为字符串。 Then you can perform the insert easily and convert all the values to doubles afterwords. 然后,您可以轻松地执行插入操作,并将所有值转换为双倍后缀。

Use the following code 使用以下代码

public static void main(String[] args) {

    double x = 5;

    String z = 4.6 + String.valueOf((int)x);
    double y = Double.parseDouble(z) + 34.2 ;
    System.out.println("y is " + y);
}

This will give the answer you wanted. 这将给出您想要的答案。

you can do this way 你可以这样

double x=0.05;
double y = (4.6+x) + 34.2 ;
 double x = 0.05;
 double y = (4.6 + x) + 34.2 ;

instead of 代替

 double x = 5;
 double y = 4.6x + 34.2 ;

Try 尝试

public static void main(String[] args) {

        double x = 0.05;

        double y = (4.6 + x) + 34.2;

        System.out.println("y is " + y);

    }

output: 输出:

 y is 38.85

Without changing x value 不改变x

public static void main(String[] args) {

    double x = 5;

    double y = (4.6 + x/100) + 34.2;

    System.out.println("y is " + y);

}

output: 输出:

y is 38.85

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM