java 标准输入输出整数 |
您所在的位置:网站首页 › java输入十个整数从小到大输出 › java 标准输入输出整数 |
问题 我可以用什么类在Java中读取整数变量? #1 热门回答(125 赞) 你可以使用java.util.Scanner(API): import java.util.Scanner; //... Scanner in = new Scanner(System.in); int num = in.nextInt(); 它还可以使用正则表达式等对输入进行标记.API具有示例,并且此站点中还有许多其他示例(例如,196565178)。 #2 热门回答(31 赞) 如果你使用的是Java 6,则可以使用以下oneliner从控制台读取整数: int n = Integer.parseInt(System.console().readLine()); #3 热门回答(17 赞) 这里我提供2个示例来从标准输入读取整数值 例1 import java.util.Scanner; public class Maxof2 { public static void main(String args[]) { //taking value as command line argument. Scanner in = new Scanner(System.in); System.out.printf("Enter i Value: "); int i = in.nextInt(); System.out.printf("Enter j Value: "); int j = in.nextInt(); if(i > j) System.out.println(i+"i is greater than "+j); else System.out.println(j+" is greater than "+i); } } 例2 public class ReadandWritewhateveryoutype { public static void main(String args[]) throws java.lang.Exception { System.out.printf("This Program is used to Read and Write what ever you type \nType quit to Exit at any Moment\n\n"); java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); String hi; while (!(hi=r.readLine()).startsWith("quit"))System.out.printf("\nYou have typed: %s \n",hi); } } 我更喜欢第一个例子,它很容易理解。 你可以在以下网站编译和运行JAVA程序:http://ideone.com |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |