对题目所给的,10个、整数做了严格的限定,很严谨,供lz参考吧!
public class Test3 {
public static void main(String[] args){
System.out.println("请输入十个整数,中间以逗号隔开");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String reg="^[0-9]*[1-9][0-9]*$";//判定是否是整数的正则
Pattern p = Pattern.compile(reg);
String word="";
try {
word = br.readLine();
String [] arrTmp = word.split(",");//以“,”为依据,截取输入的整数致String[]数组中
int l = arrTmp.length;
if(l!=10){
System.out.println("输入不足10个数字,请重新输入:");
}else{
int[] arr = new int[arrTmp.length];
boolean b =false;
for(int i = 0 ;i
b = m.matches();
if(b){//此处如果b=true证明输入合法,均为整数
arr[i] = Integer.parseInt(arrTmp[i]);
}else{
System.out.println("输入有误,非整数!");
break;
}
}
if(b){//如果输入不合法,b=false,则此处不会执行,程序结束
for(int i = arr.length-1 ;i>-1;i--){
System.out.println("逆序输出为:"+arr[i]);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
既然是JAVA 就要有面向对象的思维方式 我写了一个小程序 贴上。
import java.util.Scanner;
public class Test {
public int [] enter()
{ System.out.println("输入N个整数以空格区别");
Scanner input =new Scanner(System.in);
String [] num=input.nextLine().split(" ");
int [] num1=new int[num.length];
int t=0;
for (String i : num) {
num1[t++]=Integer.parseInt(i);
}
return num1;
}
public int[] exchang(int[] num1)
{
int e=0;
for (int i = 0; i < num1.length/2; i++) {
e=num1[i];
num1[i]=num1[num1.length-i-1];
num1[num1.length-i-1]=e;
}
return num1;
}
public static void main(String[] args) {
Test t=new Test();
int []num=t.enter();
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
System.out.println();
num= t.exchang(num);
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
}
}
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int [10];
for(int i=0;i<10;i++){
a[i] = sc.nextInt();
}
for(int j=a.length-1;j>=0;j-- ){
System.out.print(a[j]+" ");
}
}
} 这个应该是你要的吧
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class QuestionOne {
/**
* 用 JAVA 编程 .. 从键盘输入10个整数存入整型数组中,逆序输出这10个数。要求输出逆序前和逆序后的数列.... 感谢
*
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//输入的整数以空格分隔,输入完成后回车
BufferedReader bReader = new BufferedReader(new InputStreamReader(
System.in));
String inputString = bReader.readLine();
String[] nums = inputString.split(" ");
int[] numInts = new int[nums.length];
int index = 0;
for (String i : nums) {
numInts[index++] = Integer.parseInt(i);
}
System.out.println("正序");
for (int j = 0; j < numInts.length; j++) {
System.out.print(numInts[j] + "");
}
System.out.println();
System.out.println("倒序");
for (int j = numInts.length - 1; j >= 0; j--) {
System.out.print(numInts[j] + "");
}
System.out.println();
}
}
这么简单的问题自己做吧,给个思路就是遍历数组时下标从最后一个开始就行了