Java中从键盘输入多个整数的方法 Java中从键盘输入多个整数的方法是Java基础编程中的一种常见操作。今天,我们将为您分享两种从键盘输入多个整数的方法,分别使用`sc.hasNextInt()`函数和`sc.trim()`函数。 使用`sc.hasNextInt()`函数 在使用`sc.hasNextInt()`函数时,我们可以使用`Scanner`类读取键盘输入的整数。`sc.hasNextInt()`函数用于判断当前输入的是否是整数,如果是整数则返回`true`,否则返回`false`。在读取整数时,我们使用`sc.nextInt()`函数读取下一个整数。 下面是一个使用`sc.hasNextInt()`函数的示例代码: ```java import java.util.Scanner; import java.lang.Math.*; class Test1{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); int m; double n,result; while(sc.hasNextInt()){ n=sc.nextInt(); m=sc.nextInt(); result=0; for(int i=0; i<m; i++){ result += n; n = Math.sqrt(n); } System.out.printf("%.2f",result); } } } ``` 使用`sc.trim()`函数 在使用`sc.trim()`函数时,我们可以使用`Scanner`类读取键盘输入的字符串,然后使用`sc.trim()`函数去掉字符串首尾空格,最后使用`sc.split()`函数按照指定字符串分割某个字符串,并以字符串数组形式返回。 下面是一个使用`sc.trim()`函数的示例代码: ```java import java.util.Scanner; import java.lang.Math.*; class Test{ public static void main(){ Scanner sc=new Scanner(System.in); String input=sc.nextLine(); input=input.trim();//去掉字符串首尾空格 String[] temp=input.split(" ");//按照指定字符串分割某个字符串并以字符串数组形式返回 double n=Integer.parseInt(temp[0]); int m=Integer.parseInt(temp[1]); double result=0; for(int i=0; i<m; i++){ result += n; n = Math.sqrt(n); } System.out.println(result); } } ``` 总结 以上代码示例展示了两种从键盘输入多个整数的方法。使用`sc.hasNextInt()`函数可以直接读取键盘输入的整数,而使用`sc.trim()`函数可以读取键盘输入的字符串,然后去掉字符串首尾空格并按照指定字符串分割某个字符串。这些方法可以满足不同的需求,例如计算数列的和等。
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 9
- 资源: 948
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)