JAVA中如何实现从键盘中输入一个整数? [JAVA]从键盘输入一个任意整数,求各位数字之和。
JAVA\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u53ef\u4ee5\u901a\u8fc7\u201dScanner\u201c\u51fd\u6570
\u76f4\u63a5\u8f93\u5165\u53c2\u6570\u7684\u5f62\u5f0f\uff0c\u6765\u5b9e\u73b0\u8f93\u5165\u8bed\u53e5\uff0c\u4e3e\u4f8b\uff1a
import
java.util.Scanner;
public
class
Test
{
public
static
void
main(String[]
args)
{
Scanner
input=new
Scanner(System.in);
System.out.println("\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff1a");
int
length=input.nextInt();//\u8f93\u5165\u4e00\u4e2a\u6574\u6570
System.out.println("\u8f93\u5165\u7684\u6574\u6570\u662f\uff1a"+length);}
}
\u5907\u6ce8\uff1aScanner\u51fd\u6570\u5c31\u662f\u7528\u6765\u8fdb\u884c\u8bed\u53e5\u8f93\u5165\u7684\uff0c\u4e0a\u9762\u4e3e\u4f8b\u7684\u662f\u6574\u6570\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u7528\u4ee5\u4e0b\u8bed\u53e5\u201cString
length=input.next()\u201d\uff0c\u8f93\u5165\u7684\u5c31\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7b49\u3002
\u6c42\u5404\u4f4d\u6570\u5b57\u4e4b\u548c\u7684\u7a0b\u5e8f\u5982\u4e0b\uff1a
import java.util.Scanner;
public class N {
public static int sumDigits(long n){
int sum=0;
while(n>0){
int m=(int)(n%10);
sum=sum+m;
n=n/10;
}
return sum;
}
public static void main(String[] args) {
try{
System.out.println("\u8bf7\u8f93\u5165\u6570\u5b57\uff1a");
Scanner sc=new Scanner(System.in);
long l=sc.nextLong();
System.out.println("\u8f93\u5165\u6570\u5b57\u7684\u5404\u4f4d\u6570\u4e4b\u548c\u4e3a"+sumDigits(l));
}catch(Exception e){
System.out.println("\u60a8\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\uff01");
}
}
}
\u8fd0\u884c\u7ed3\u679c\uff1a
\u8bf7\u8f93\u5165\u6570\u5b57\uff1a
49291
\u8f93\u5165\u6570\u5b57\u7684\u5404\u4f4d\u6570\u4e4b\u548c\u4e3a25
import java.io.InputStreamReader;
public class Test{
public static void main(String[] args){
int num ;
String str ;
BufferedReader brd=new BufferedReader(new InputStreamReader(System.in)) ;
while(true)
{
System.out.print("请输入数字:") ;
try
{
str=brd.readLine() ;
num=Integer.parseInt(str) ;
break ;
}
catch(Exception e)
{
System.out.println("对不起,只能输入整数,请重新输入。") ;
}
}
System.out.println("你输入的整数是: "+num) ;
}
}
要输出的是整数,当然要来个判断如果输入的不是时就重新提示输入的说。
首先需要用到Scanner 类,所以要在最开始import java.util.Scanner;
然后在需要用到的方法里面创建个Scanner 物体,Scanner sc = new Scanner(System.in);
然后就可以用Scanner的方法nextInt()了: sc.nextInt();
举例如下:
import java.util.Scanner; //import the Scanner class
public class sampleInput{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); //create the new Scanner object sc
System.out.println("please input an integer");
int a = sc.nextInt(); //use the nextInt() method to read in
System.out.println("Thanks for your help!" + a); //print the input integer
}
}
import java.util.*
public class test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数: ");
int num = input.nextInt();
System.out.println("你输入的整数是:"+num);
}
}
system.in InputStream
你可以用java I/O中的Stream类,也可以用scranner类。
相关知识:I/O和nI/o,包括InputStream与OutputStream。
贴上代码:
ublic class Test{
public static void main(String[] args){
int num ;
String str ;
BufferedReader brd=new BufferedReader(new InputStreamReader(System.in)) ;
while(true)
{
System.out.print("请输入数字:") ;
try
{
str=brd.readLine() ;
num=Integer.parseInt(str) ;
break ;
}
catch(Exception e)
{
System.out.println("对不起,只能输入整数,请重新输入。") ;
}
}
System.out.println("你输入的整数是: "+num) ;
}
}
绛旓細import java.io.BufferedReader;import java.io.InputStreamReader;public class Test{ public static void main(String[] args){ int num ;String str ;BufferedReader brd=new BufferedReader(new InputStreamReader(System.in)) ;while(true){ System.out.print("璇杈撳叆鏁板瓧:") ;try { str=brd....
绛旓細public static void main(String[] args) { Scanner s = new Scanner(System.in);String str = null;System.out.println("璇疯緭鍏ユ偍鎯宠緭鍏ョ殑瀛椾覆锛");str = s.next();System.out.println("鎮ㄨ緭鍏ョ殑鏄細");System.out.println(str);} } 鍦java涓,濡備綍浠庨敭鐩樹腑杈撳叆鏁板瓧,瀛楀厓,瀛椾覆 鎻愪緵涓ょ...
绛旓細浠g爜濡備笅锛歩mport java.util.Scanner;public class ScannerDemo{ public static void main(String[] args) throws Exception{ Scanner scan=new Scanner(System.in);System.out.println("璇杈撳叆鍐呭锛");String str=scan.nextLine();char[] s=str.toCharArray();for(int i=s.length-1;i>=0;i--...
绛旓細step1:鏂板缓涓涓紨绀虹被demo step2:瀵煎叆 鍖呮枃浠讹紝鍦ㄥ寘鍚嶄笅锛岀被鍚嶄箣涓杈撳叆濡備笅浠g爜銆俰mport java.util.Scanner;step3:鍦ㄧ被涓殑浠g爜濡備笅锛歱ublic static void main(String[] args) { //鍒涘缓涓涓閿洏褰曞叆瀵硅薄input Scanner input = new Scanner(System.in); System.out.println("please input...
绛旓細绋嬪簭寮鍙戣繃绋嬩腑锛岄渶瑕浠庨敭鐩鑾峰彇杈撳叆鍊兼槸甯告湁鐨勪簨锛屼絾Java瀹冨亸鍋忓氨娌℃湁鍍廲璇█缁欐垜浠彁渚涚殑scanf()锛孋++缁欐垜浠彁渚涚殑cin()鑾峰彇閿洏杈撳叆鍊肩殑鐜版垚鍑芥暟!Java娌℃湁鎻愪緵杩欐牱鐨勫嚱鏁颁篃涓嶄唬琛ㄩ亣鍒拌繖绉嶆儏鍐垫垜浠氨鏉熸墜鏃犵瓥锛岃浣犵湅浠ヤ笅涓夌瑙e喅鏂规硶鍚э細浠ヤ笅灏嗗垪鍑哄嚑绉嶆柟娉曪細鏂规硶涓锛氫粠鎺у埗鍙版帴鏀朵竴涓瓧绗︼紝鐒跺悗灏...
绛旓細ava鍒濆鑰,涓瀹氬浠庨敭鐩樿緭鍏鏁版嵁鎰熷埌鍥伴毦,浣跨敤涓嬮潰鐨勭被Input,鍙互 鏂逛究鐨勪粠閿洏杈撳叆鏁版嵁:浣跨敤鏂规硶涓句緥: String s=Input.readString(); 璇诲叆瀛楃涓 int i=Input.readInt(); 璇诲叆鏁存暟 涓嬮潰鏄java杈撳叆杈撳嚭鍩烘湰绫籌nput绫荤殑婧愪唬鐮:鏈鍚庝互浠庨敭鐩樿緭鍏10涓暣鏁颁负渚嬭鏄庝箣銆俰mport java.io.*;class Input...
绛旓細棣栧厛锛屽鍏java.util.*鍖呫俰mport java.util.*;鐒跺悗锛屼綘闇瑕佹柊寤轰竴涓鍙栨爣鍑嗚緭鍏ワ紙閿洏锛夌殑鎵弿鍣ㄥ璞°係canner in = new Scanner(System.in);鐜板湪锛屼綘鍙互浠庨敭鐩樿緭鍏瀛楃涓蹭簡銆係tring s = in.nextLine();浠ヤ笂杩欎竴琛屾妸閿洏杈撳叆鐨勪竴琛屽瓧绗︿覆璇诲彇鍒板彉閲 s 涓傝鐪嬩竴涓畬鏁寸殑绠鍗曠ず渚嬶細import java...
绛旓細鍦Java涓疄鐜鑷姩杈撳叆閫氬父娑夊強鍒版ā鎷熼敭鐩樻搷浣溿傝繖鍙互閫氳繃浣跨敤Java鐨刞java.awt.Robot`绫绘潵瀹屾垚銆俙Robot`绫诲彲浠ョ敓鎴愭湰鍦扮郴缁熻緭鍏ヤ簨浠讹紝妯′豢鐢ㄦ埛涓庨敭鐩樺拰榧犳爣鐨勪氦浜掋備緥濡傦紝浠ヤ笅浠g爜娈靛睍绀轰簡濡備綍浣跨敤`Robot`绫绘ā鎷閿洏杈撳叆锛歚``java import java.awt.Robot;import java.awt.event.KeyEvent;public class ...
绛旓細鍙互閫氳繃鈥漇canner鈥滃嚱鏁 鐩存帴杈撳叆鍙傛暟鐨勫舰寮忥紝鏉瀹炵幇杈撳叆璇彞锛屼妇渚嬶細import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner input=new Scanner(System.in);System.out.println("璇疯緭鍏ヤ竴涓瓧绗﹀拰瀛楃涓诧細");String str2=input.next()//杈撳叆涓涓瓧绗 Str...
绛旓細{ while (true){ try//---鐢╥o搴撳氨涓瀹氳鍔犱笂寮傚父澶勭悊 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("璇杈撳叆涓涓瓧绗︿覆锛");string s = br.readLine();//readLine()璇诲叆绋嬪簭涓殑鏄竴涓瓧绗︿覆銆//java璨屼技浠庢帶鍒跺彴璇诲叆鐨勯兘鏄瓧绗︿覆锛屼竴鑸...