如何在Java中进行重写和压缩

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

How to rewrite and condense in Java

问题

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
      
        double f0 = scnr.nextDouble();
        double r = Math.pow(2, (1.0 / 12.0));
        double[] frequencies = new double[5];
        
        for (int i = 0; i < 5; i++) {
            frequencies[i] = f0 * Math.pow(r, i);
            System.out.printf("%.2f ", frequencies[i]);
        }
    }
}
英文:

I'm new to Java and I'm trying to write a function that will take an input, calculate the first 4 frequencies, and then out the input and next 4 frequencies (with 2 values after decimal place). The code I currently have written does exactly that but when I submit it for grading, it's telling me it's wrong. ( I can run it to test it and it works every time). Can someone help me maybe condense what I have to be neater, or just point me in the right direction? Thank you

import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      
      double f0 = scnr.nextDouble();
      double r = Math.pow(2, (1.0 / 12.0));
      double f1 = f0 * Math.pow(r, 1);
      double f2 = f0 * Math.pow(r, 2);
      double f3 = f0 * Math.pow(r, 3);
      double f4 = f0 * Math.pow(r, 4);
      double f5 = f0 * Math.pow(r, 5);
      
       
     System.out.printf(&quot;%.2f&quot;, f0);
     System.out.print(&quot; &quot;);
     System.out.printf(&quot;%.2f&quot;, f1);
     System.out.print(&quot; &quot;);
     System.out.printf(&quot;%.2f&quot;, f2);
     System.out.print(&quot; &quot;);
     System.out.printf(&quot;%.2f&quot;, f3);
     System.out.print(&quot; &quot;);
     System.out.printf(&quot;%.2f&quot;, f4);     

   }
}

答案1

得分: 1

这是使用数组和for循环的绝佳情况。由于您有多个相同类型的值,然后对它们进行某种模式的计算,您可以使用数组来存储这些数字,并使用for循环来执行计算。

import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      
      double f0 = scnr.nextDouble();
      double r = Math.pow(2, (1.0 / 12.0));
      int numberCount = 5; // 您想要保留多少个数字,在这种情况下是4个
      double[] nums = new double[numberCount]; // 使用数组来存储数字,而不是多个独立的变量
      for (int i = 0; i < nums.length; i++) {
         nums[i] = f0 * Math.pow(r, i + 1);
      }

      System.out.printf("%.2f", f0);
      for (int i = 0; i < nums.length; i++) {
         System.out.printf(" %.2f", nums[i]);
      }    
   }
}

这个好处是完全没有硬编码,因此您可以通过只更改变量numberCount来扩展或缩小要使用的数字数量,而不是多次复制粘贴代码。

英文:

This is a perfect situation to use an array and a for loop in. Since you have multiple of the same type of value, and then are doing some calculation on them which has some type of pattern, you can use an array to store the numbers, and use a for loop to do the calculations.

import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      
      double f0 = scnr.nextDouble();
      double r = Math.pow(2, (1.0 / 12.0));
      int numberCount = 5; // how many numbers you want to keep, in this case its 4 of them
      double[] nums = new double[numberCount]; // using an array to store the numbers, instead of multiple separate variables
      for (int i = 0; i &lt; nums.length; i++) {
         nums[i] = f0 * Math.pow(r, i + 1);
      }

      System.out.printf(&quot;%.2f&quot;, f0);
      for (int i = 0; i &lt; nums.length; i++) {
         System.out.printf(&quot; %.2f&quot;, nums[i]);
      }    
   }
}

The good part about this is that this is entirely not hardcoded, so you can extend or shrink how many numbers you want to use by just changing the variable numberCount, instead of copy-pasting your code many times over.

huangapple
  • 本文由 发表于 2020年8月23日 06:40:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63541786.html
匿名

发表评论

匿名网友

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

确定