Java 毕达哥拉斯定理的代码优化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23499924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:25:13  来源:igfitidea点击:

Code optimization for pythagoras theorem

javamathmethods

提问by unexim

could anyone help me with the following code :

任何人都可以帮助我使用以下代码:

import java.math.*;
import java.util.Scanner;

public class Pitagor {
public static void main (String[] args){
    System.out.println(pitagor(in1(),in2()));

}
public static int in1 (){
    Scanner input = new Scanner(System.in);
    System.out.println("Eingabe von a  ");
    int a = input.nextInt();

    return a;
}
public static int in2 (){
    Scanner input = new Scanner(System.in);
    System.out.println("Eingabe von b");
    int b = input.nextInt();

    return b;
}
public static double pitagor (int x , int y){
    double c = Math.sqrt((x*x)+(y*y));
    return c;
}
}

My goal here is to make the code simpler by using separate methods for input and calculation, but i can't seem to understand how to make only one input method instead of in1()and in2().

我的目标是通过使用单独的输入和计算方法使代码更简单,但我似乎无法理解如何只使用一种输入方法而不是in1()in2()

What i tried was :

我试过的是:

public static void in (){
Scanner input = new Scanner(System.in);
System.out.println("Eingabe von a  ");
int a = input.nextInt();
Scanner input = new Scanner(System.in);
System.out.println("Eingabe von b");
int b = input.nextInt(); }

but i don't know how to get aand bfrom this method, so i can use them in

但我不知道如何从这个方法中得到ab,所以我可以在

pitagor (int x , int y)

pitagor (int x , int y)

Thanks in advance.

提前致谢。

采纳答案by Mike B

One way to do it:

一种方法:

import java.math.*;
import java.util.Scanner;

public class Pitagor {
    public static void main (String[] args){
        System.out.println(pitagor(getIntInput("Eingabe von a  "),getIntInput("Eingabe von b")));

    }
    public static int getIntInput(String prompt){
        Scanner input = new Scanner(System.in);
        System.out.println(prompt);
        return input.nextInt();
    }
    public static double pitagor (int x , int y){
        double c = Math.sqrt((x*x)+(y*y));
        return c;
    }
}