在java中怎样从键盘输入数字(新手问题) java中从控制台输入(要怎样才能规定只能输入数字呢)希望能...
JAVA\u600e\u4e48\u5199\uff1a\u63a5\u6536\u4ece\u952e\u76d8\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765\uff1f\u65b0\u624b\u95ee\u9898\uff08\u975e\u8bda\u52ff\u6270\uff09Scanner scan = new Scanner(System.in);scan.nextLine();//\u63a5\u6536\u4e00\u884c\u5b57\u7b26\u4e32\uff0c\u4ee5\u56de\u8f66\u952e\u7ed3\u675f//\u5176\u4ed6\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u53ef\u4ee5\u67e5\u770bscan\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5scan.close();
public static void main(String[] args) {
/** \u5199\u4e00\u4e2a\u7a0b\u5e8f\uff1a\u4ece\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u4f7f\u4e4b\u628a\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u7ed9\u63d0\u70bc\u51fa\u6765
* \u5982\uff1a\u8f93\u5165\uff1a\u201cwa21ttw43p6ku5mm\u201d \u8f93\u51fa\uff1a214365 */
System.out.println("\u8bf7\u8f93\u5165\uff1a");
Scanner s = new Scanner(System.in);
String str = s.next();
String reg = "\\d";//\u6b63\u5219\u8868\u8fbe\u5f0f
StringBuffer result = new StringBuffer();
StringBuffer sb = new StringBuffer();
for(int i = 0;i < str.length();i++){
sb.append(str.charAt(i)).append("_");
}
String mes = sb.toString();
String[] data = mes.split("_");
for (String st : data) {
if(st.matches(reg)){
result.append(st);
}
}
System.out.println(result);
}
方便的从键盘输入数据:
使用方法举例: String s=Input.readString(); 读入字符串
int i=Input.readInt(); 读入整数
下面是java输入输出基本类Input类的源代码:
最后以从键盘输入10个整数为例说明之。
import java.io.*;
class Input
{static InputStreamReader in;
static BufferedReader reader;
static
{in=new InputStreamReader(System.in);
reader=new BufferedReader(in);
}
static String readString()
{String s="";
try
{ s=reader.readLine();
}
catch(IOException e)
{System.out.println(e);
System.exit(0);
}
return s;
}
static char readChar()
{char ch='a';
try
{
String s=readString();
ch=s.charAt(0);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return ch;
}
static int readInt()
{String s=readString();
int i=0;
try{
i=Integer.parseInt(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return i;
}
static double readDouble()
{String s=readString();
double d=0.0;
try
{d=Double.parseDouble(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return d;
}
static float readFloat()
{
String s=readString();
float f=0.0f;
try
{
f=Float.parseFloat(s);
}
catch(Exception e)
{ System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return f;
}
}
/*用法举例,从键盘输入十个整数:*/
class InoutData
{public static void main(String args[])
{ int a[]=new int[10];
for(int i=0;i<10;i++)
{ System.out.println("请输入第"+(i+1)+"个数:");
a[i]=Input.readInt();
}
for(int i=0;i<10;i++)
System.out.println("a["+i+"]="+a[i]);
}
}
以下的代码是你的版本,是可以执行并且成功的。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// 获取键盘输入
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("由大到小的顺序是:");
if (a < b)
if (b < c)
System.out.println(c + " " + b + " " + a);
else if (a < c)
System.out.println(b + " " + c + " " + a);
else
System.out.println(b + " " + a + " " + c);
else if (a < c)
System.out.println(c + " " + a + " " + b);
else if (b < c)
System.out.println(a + " " + c + " " + b);
else
System.out.println(a + " " + b + " " + c);
}
}
也可以用下面的,3个数的排序,可以用3目。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// 获取键盘输入
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("由大到小的顺序是:");
// 还可以这么做
System.out.println(a > b ? (b > c ? a + " " + b + " " + c : a > c ? a
+ " " + c + " " + b : c + " " + a + " " + b) : (c < a ? b + " "
+ c + " " + a : b > c ? b + " " + c + " " + a : c + " " + b
+ " " + a));
}
}
你的if else语句写得太乱了,基本上按照你的意思来写的 OK了
import java.util.*;
class Order1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);//获取键盘输入
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
System.out.println("由大到小的顺序是:");
if(a>b && b>c)
System.out.println(a+">"+b+">"+c);
if(a>c && c>b)
System.out.println(a+">"+c+">"+b);
if(b>a && a>c)
System.out.println(b+">"+a+">"+c);
if(b>c && c>a)
System.out.println(b+">"+c+">"+a);
if(c>a && a>b)
System.out.println(c+">"+a+">"+b);
if(c>b && b>a)
System.out.println(c+">"+b+">"+a);
}
}
使用Scanner类
Scanner scanner = new Scanner(System.in);
使用BufferedReader类
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
绛旓細鍙互鐢╯witch锛杈撳叆涓涓彉閲廰锛涚劧鍚巗witch锛坅锛夛紙鍚庨潰鏄簨浠讹紝娌″悗璇彞鍔犱笂print锛夈傘傘
绛旓細棣栧厛鎴戝厛璇存槑涓涓嬶紝System.in灏辨槸inputstream绫伙紝浣犲彲浠ラ氳繃鏌ョ湅System绫伙紝灏辫兘鎵惧埌in杩欎釜瀵硅薄锛岃涓嬪浘銆傜敱浜巌nputstream绫绘槸鎶借薄绫伙紝鎵浠ヤ笉鑳藉疄渚嬪寲瀵硅薄鐨勶紝鎵浠ラ渶瑕佷娇鐢⊿canner鏉ヨ緟鍔╁疄鐜般傛垜闄勪笂绋嬪簭婧愮爜鍜岃繍绠楃殑缁撴灉鍥俱備緵浣犲弬鑰冦俰mport java.util.Scanner;class setDataIntoArrary{ public static void...
绛旓細import java.util.Scanner;public class Test2 { public static void main(String[] args) { Scanner input = new Scanner(System.in);System.out.print("璇杈撳叆num鐨勫硷細");int num = input.nextInt();System.out.println("num鐨勫间负锛"+num);} } ...
绛旓細public class input10 { public static void main(String[] args) { int a[] = new int[10]; // 10涓暟鐨勬暟缁 Scanner scanner = new Scanner(System.in);System.out.println("璇疯緭鍏10涓鏁板瓧: ");for(int i = 0; i < 10; ++i)a[i] = scanner.nextInt(); // 浠庨敭鐩樿緭鍏10涓...
绛旓細鍙互閫氳繃鈥漇canner鈥滃嚱鏁 鐩存帴杈撳叆鍙傛暟鐨勫舰寮忥紝鏉ュ疄鐜拌緭鍏ヨ鍙ワ紝涓句緥锛歩mport java.util.Scanner;public class Test { public static void main(String[] args) { Scanner input=new Scanner(System.in);System.out.println("璇疯緭鍏ヤ竴涓暣鏁帮細");int length=input.nextInt();//杈撳叆涓涓暣鏁 System....
绛旓細import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in);System.out.print("杈撳叆0鈥99999涔嬮棿鐨勪换鎰忔暟锛");int n;n = in.nextInt();int b = Integer.toString(n).length();System.out.println("浣杈撳叆鐨勬暟瀛浣嶆暟涓...
绛旓細濡備綍鐢╦ava缂栫▼浠庨敭鐩樻帴鏀鏁板瓧骞惰繍绠? 鍓嶄袱涓槸鏁板瓧绗笁涓繍绠楃,瑕佹眰鍥涚杩愮畻+鈥*%,鍐嶆璇存槑鏄浠庨敭鐩樿緭鍏,涓嶉渶浠涔堢獥浣撶殑璁$畻鍣,鎬ョ敤鎴戝氨鏄笉浼氳浆鎹㈠晩,灏辨槸杩愮畻绗﹂偅閲,蹇憡璇夋垜,瀹屾暣鐨,澶氳阿... 鍓嶄袱涓槸鏁板瓧绗笁涓繍绠楃,瑕佹眰鍥涚杩愮畻+ 鈥 * %,鍐嶆璇存槑鏄粠閿洏杈撳叆,涓嶉渶浠涔堢獥浣撶殑璁$畻鍣,鎬ョ敤鎴戝氨鏄...
绛旓細Scanner input=new Scanner(System.in);System.out.println("杈撳叆绗竴涓暟:");double d1=input.nextDouble();System.out.println("杈撳叆绗簩涓暟:");double d2=input.nextDouble();System.out.println("杈撳叆杩愮畻绗﹀彿锛+-*/锛夛細");String s=input.next();if(s.length()!=1){ System.out....
绛旓細鍦↗ava涓鍦ㄦ暟缁勪腑淇濆瓨10涓暟,璋冪敤涓嬮潰鐨勬帓搴忓嚱鏁帮紝鐢ㄩ夋嫨娉曟寜闄嶅簭鎺掑簭锛歱ublic static void selectionSort(int[] arr){ for (int i = 0; i < arr.length - 1; i++) { int max = i; for (int j = i + 1; j < arr.length; j++) { if (arr[max] < arr[j]) ...
绛旓細浠庨敭鐩璇诲彇涓琛杈撳叆锛屽洖杞︾粨鏉熴侭ufferedReaderbr锛孲tringstr绛変簬0锛屼粠閿洏璇诲彇涓琛岃緭鍏ワ紝鍥炶溅缁撴潫锛屾鏃跺垱寤篵r鍜宻tr鐨勫垵濮嬪寲锛屽悓鏃朵娇鐢ㄦ祦鐨勮鍔熻兘锛屾渶鍚庤緭鍑簊tr瀵规槸鍚︿负鏁板瓧锛岃嚜琛屾娴嬶紝鍒╃敤杈撳叆瀛楄妭娴佸厛灏嗚緭鍏ワ紝甯﹀弬Systemin锛岃鍒癇ufferedReader涓紝鐒跺悗鍒╃敤鍏舵柟娉時eadLine璇诲彇涓琛岋紝杩斿洖鐨勬槸String绫诲瀷鐨勶紝鐩存帴...