【问题标题】:How to make this code accept an input [duplicate]如何使此代码接受输入[重复]
【发布时间】:2016-06-05 16:10:27
【问题描述】:

我一直在尝试找到一种方法,让我的代码能够让指数接受 E 和 X 的输入。我知道如果我使用扫描仪工具,它就会成为可能。但到目前为止,我还没有找到一种方法来做到这一点。如果你能帮忙就太好了,谢谢!

import java.util.Scanner;

public class SimpleExponents {

public static void main(String[] args) {
    int e = 6; 
    int x = 4; 
    double result; 

    result = Math.pow(e,x);

    System.out.println( result );

}

}

【问题讨论】:

    标签: java


    【解决方案1】:
    Scanner scanner = new Scanner(System.in);
    int e = Integer.parseInt(scanner.next());
    int x = Integer.parseInt(scanner.next());
    

    或

    Scanner scanner = new Scanner(System.in);
    int e = scanner.nextInt();
    int x = scanner.nextInt();
    

    【讨论】:

    • 为了扩展OP的答案,也可以在提示答案之前使用System.out.print("Input a value for 'e':\t");。 +1,flkes。
    • 谢谢兄弟,你帮了大忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2017-01-10
    • 2022-08-11
    • 2012-07-10
    • 2020-07-02
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多