首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初级弧度/度转换程序

初级弧度/度转换程序
EN

Code Review用户
提问于 2018-10-28 14:59:38
回答 2查看 135关注 0票数 3

在Java方面,我是个初学者,我创建了这个小代码来帮助我掌握一些小的主题。它允许它的用户将一个激进度量转换为一个度度量,将一个度度量转换为一个激进度量。这是我的最后代码:

代码语言:javascript
复制
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader;
import java.util.ArrayList; 

public class MainClass {

    public static void dtr (BufferedReader dRead) throws IOException {

        ArrayList<Integer> divisors = new ArrayList<Integer>();
        ArrayList<Integer> gcd = new ArrayList<Integer>();


        int d = Integer.parseInt(dRead.readLine());
        for (int pd = 1; pd <= d; pd++) {
            if (d % pd == 0) {
                divisors.add(pd);               
            }
        }

        for (int index = 0; index < divisors.size(); ++index) {
            if (180.0 % divisors.get(index) == 0) {
                gcd.add(divisors.get(index));
            }

        }

        int dem = (180 / gcd.get(gcd.size() - 1));
        if ((d / (gcd.get(gcd.size() - 1))) == 1) {
            System.out.print("Radical: pi / " + dem);
        } else {
            System.out.print("Radical: " + (d / (gcd.get(gcd.size() - 1))) + "pi / " + dem);
        }

    }

    public static void rtd (BufferedReader rnRead, BufferedReader rdRead) throws IOException {

        int rn = Integer.parseInt(rnRead.readLine());
        int rd = Integer.parseInt(rdRead.readLine());

        int dividend = rn * 180;

        System.out.print("Degrees: " + (dividend / rd));

    }

    public static void main(String[] args) throws IOException {

        System.out.println("Do you want to convert from radicals to degrees, or degrees to radicals?");
        System.out.print("Use \"R\" for radials to degrees, and \"D\" for degrees to radicals: ");
        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
        String userChoice = userInput.readLine();

        if (userChoice.contains("D") || userChoice.contains("d")) {

            System.out.print("Degrees: ");
            BufferedReader dRead = new BufferedReader(new InputStreamReader(System.in));
            dtr(dRead);

        } else if (userChoice.contains("R") || userChoice.contains("r")) {

            System.out.println("Radical numerator (omit pi): ");
            BufferedReader rnRead = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Radical denominator: ");
            BufferedReader rdRead = new BufferedReader(new InputStreamReader(System.in));
            rtd(rnRead, rdRead);

        } else {

            System.out.println("Invalid response. Please restart the program.");

        }

    }

}

我知道这段代码有多糟糕,但我只想知道是否有任何方法可以优化这些代码的功能,因为我想将这些概念应用到将来的所有工作中。

EN

回答 2

Code Review用户

发布于 2018-10-28 18:51:51

现在,通常忽略赋值右侧的泛型类型,而不是

代码语言:javascript
复制
 ArrayList<Integer> divisors = new ArrayList<Integer>(); 

你可以写

代码语言:javascript
复制
 ArrayList<Integer> divisors = new ArrayList<>(); 

由于您没有使用任何ArrayList特定的功能,所以将divisors声明为List (gcd也是如此)是比较干净的。

代码语言:javascript
复制
 List<Integer> divisors = new ArrayList<>();

调用gcd.get(gcd.size() )需要一个具有合理名称的单独方法。

对于BufferedReader userInput ...,虽然在本例中不关闭IO资源是无害的,但通常这样做是很好的做法。查看“尝试使用资源语句”:https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

同样常见的做法是重用Reader,而不是每次需要执行读取操作时都创建一个新的阅读器。考虑到这一点,您将得到如下内容:

代码语言:javascript
复制
public static void main(String[] args) throws IOException {
    System.out.println("Do you want to convert from radicals to degrees, or degrees to radicals?");
    System.out.print("Use \"R\" for radials to degrees, and \"D\" for degrees to radicals: ");
    try (BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in))) {
        String userChoice = userInput.readLine();

        if (userChoice.contains("D") || userChoice.contains("d")) {

            System.out.print("Degrees: ");
            dtr(userInput);

        } else if (userChoice.contains("R") || userChoice.contains("r")) {

            System.out.println("Radical numerator (omit pi): ");
            System.out.println("Radical denominator: ");
            rtd(userInput);

        } else {

            System.out.println("Invalid response. Please restart the program.");
        }
    }
}

注意,现在rtd只需要一个参数。因为您在读取用户输入之前进行打印,所以用户可以看到

代码语言:javascript
复制
 Radical numerator (omit pi): 
 Radical denominator: 

在她有机会提供任何意见之前。这些语句应该移动到与读取用户输入交织的rtd中。

代码语言:javascript
复制
 if (userChoice.contains("D") || userChoice.contains("d"))

可以简化如下:

代码语言:javascript
复制
if (userChoice.toLowerCase().contains("d")) {     
票数 2
EN

Code Review用户

发布于 2018-10-28 19:35:46

欧氏算法是计算两个整数最大公因子的一种著名而有效的方法.

与您的方法相比,欧几里德算法速度更快,代码更少,并且不需要额外的存储。

例如,使用

代码语言:javascript
复制
// https://rosettacode.org/wiki/Greatest_common_divisor#Iterative_Euclid.27s_Algorithm
public static int gcd(int a, int b) {
    while (b > 0) {
        int c = a % b;
        a = b;
        b = c;
    }
    return a;
}

将分数简化为

代码语言:javascript
复制
int degrees = Integer.parseInt(dRead.readLine());
int commonDivisor = gcd(degrees, 180);
int numerator = degrees / commonDivisor;
int denominator = 180 / commonDivisor;
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/206436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档