在Java中修正一个公式。

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

Correcting a formula in java

问题

以下是翻译好的部分:

第一个任务要求我编写一个表达式的程序。作业中的表达式如下:

    y   =   4x^3 + 8x^2 - 31x - 35 / (3x^2 + 1)^1/2  + 2 * | x - 1.5 |  

| 表示绝对值;(...)1/2 表示平方根

此外,我应该如何创建一个代码,以打印出公式输出正数、负数或零的次数?

以下是我创建作业程序的方式:

import java.io.*;

public class hw1 {

    public static void main(String[] args) throws IOException
    {
        System.out.println("My name is Jibran Khan and this is the output of my first program.");

        double x; double y;

        PrintWriter outFile = new PrintWriter("testfile.txt");

        for(x=-3.00; x <= 3.0; x = x + 0.50)
        { 

        y=((4*(x*x*x))) + (8*(x*x))-(31*x)-35/Math.sqrt(3*x*x+1)+2* Math.abs(x-1.5);
        System.out.println("X = " + x + "Y = " + y );

        if (y<=0.0) System.out.println("Y is negative");
        if (y>=0.0) System.out.println("Y is positive");
        if (y == 0.0) System.out.println("Y is zero");

        }

        System.out.println();
        System.out.println("My first program is complete");

        outFile.close();

    }

}

请注意,这是您提供的代码的翻译部分。如果您有其他问题或需要进一步帮助,请随时提问。

英文:

My first assignment requires me to write a program that expresses a formula. The formula as written in the assignment :

y   =   4x^3 + 8x^2 - 31x - 35 / (3x2 + 1)^1/2  + 2 * | x - 1.5 |  

| means absolute value; (...)1/2 means square root

Also how should I create a code that prints how many times the formula prints out a positive or negative number or zero?

Here is how I created my assignment's program:

import java.io.*;

public class hw1 {

	public static void main(String[] args) throws IOException
	{
		System.out.println(&quot;My name is Jibran Khan and this is the output of my first program.&quot;);
		
		double x; double y;
		
		PrintWriter outFile = new PrintWriter(&quot;testfile.txt&quot;);
	
		for(x=-3.00; x &lt;= 3.0; x = x + 0.50)
		{ 
		
		y=((4*(x*x*x))) + (8*(x*x))-(31*x)-35/Math.sqrt(3*x*x+1)+2* Math.abs(x-1.5);
		System.out.println(&quot;X = &quot; + x + &quot;Y = &quot; + y );
		
		if (y&lt;=0.0) System.out.println(&quot;Y is negative&quot;);
		if (y&gt;=0.0) System.out.println(&quot;Y is positive&quot;);
		if (y == 0.0) System.out.println(&quot;Y is zero&quot;);
		
		}
		
		
		System.out.println();
		System.out.println(&quot;My first program is complete&quot;);
		
		outFile.close();
		

	}
	
}

答案1

得分: 1

好的,以下是您要翻译的内容:

好的,您想要对一个简单的数学公式进行测试。您需要测试结果,并确定有多少结果是正数、负数或者等于0的……
您需要更新您的测试……如果结果等于0,您的测试返回正数、负数和零结果。

您还需要计算结果的数量。因此,在if语句中为每种类型的结果实现计数器,并在其中递增。

public static void main(String[] args) throws IOException {
    double x; double y;
    int positive = 0;
    int negative = 0;    
    int equalZero = 0;
    PrintWriter outFile = new PrintWriter("testfile.txt");

    for(x = -3.00; x <= 3.0; x = x + 0.50) { 
        y = Math.pow(4, 3) + ...您的公式
        System.out.println("X = " + x + "Y = " + y );
        if (y < 0.0) {
            System.out.println("Y is negative");
            negative++;
        } else if (y > 0.0) {
            System.out.println("Y is positive");
            positive++;
        } else { 
            System.out.println("Y is zero");
            equalZero++;
        }
    }

    System.out.println();
    System.out.println("Positive results: " + positive);
    System.out.println("Negative results: " + negative);
    System.out.println("Equal to zero: " + equalZero);

    outFile.close();
}
英文:

OK, you want to make a test on a simple math formula. You have to test the results and define how many results are positive, negative or equal to 0 ...
You have to update your tests... It result is equal to 0, your test returns positive, negative and null results.

You also have to count results. So implement counters for each type of results and increment them in the if statement.

public static void main(String[] args) throws IOException {
    double x; double y;
    int positive = 0;
    int negative = 0;    
    int equalZero = 0;
    PrintWriter outFile = new PrintWriter(&quot;testfile.txt&quot;);

    for(x=-3.00; x &lt;= 3.0; x = x + 0.50) { 
        y = Math.pow(4,3) + ... ( your formula) 
        System.out.println(&quot;X = &quot; + x + &quot;Y = &quot; + y );
        if (y &lt; 0.0) {
            System.out.println(&quot;Y is negative&quot;);
            negative++;
        } else if (y &gt; 0.0) {
            System.out.println(&quot;Y is positive&quot;);
            positive++;
        } else { 
            System.out.println(&quot;Y is zero&quot;);
            equalZero++;
        }
    }

    System.out.println();
    System.out.println(&quot;Positive results : &quot; + positive);
    System.out.println(&quot;Negative results : &quot; + negative);
    System.out.println(&quot;Equal to zero   : &quot; + equalZero);
   
    outFile.close();
}

答案2

得分: 0

将分母和分子分别存储在不同的变量中,这样可以增加可读性,您将能够更轻易地发现错误。

另外,您为 y 给出的条件:(y <= 0.0) and (y >= 0.0) 对于零也是成立的,所以最后的条件 y == 0.0 将永远不会被满足。

英文:

Calculate the Denominator and Numerator in different variables, that will give more readability and you will be able to spot the mistakes easily.

Also, the conditions you gave for y: (y &lt;= 0.0) and (y &gt;= 0.0) will be true for zero so the last condition y == 0.0 is not reachable.

huangapple
  • 本文由 发表于 2020年9月30日 09:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129844.html
匿名

发表评论

匿名网友

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

确定