当前位置: 代码迷 >> Eclipse >> eclipse多行输入多行输出有关问题和限制输入有关问题
  详细解决方案

eclipse多行输入多行输出有关问题和限制输入有关问题

热度:97   发布时间:2016-04-23 01:24:44.0
eclipse多行输入多行输出问题和限制输入问题
用户先输入一个整数n,表示接下来有n行输入数据。
接着输入的n行数据,经过程序转变后。
输出n行数据。

这个要怎么做?

如果想限制它只能输入大写字母或小写字母和数字要怎么做?
如果想限制特定的几个大写字母或小写字母呢?如A,C,R,L,M; 随便的
Eclipse java

------解决方案--------------------

package com.ydb.lianxi;

import java.util.Scanner;

public class shuru {

/**
 * @param args
 */
public static void main(String[] args) {
System.out.println("input a number:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
String a [] = new String[number];
System.out.println("begin to input the content:");
for (int i = 0; i < number; i++) {
Scanner sc1 = new Scanner(System.in);
String s = sc1.next();
a[i] =s;
}
System.out.println("print the list: ");
for (int i = 0; i < number; i++) {
System.out.println(a[i]);
}

}

}

如果要只能输入大小写字母或者固定输入,那么就要对输入的值进行判断,即可。这个判断你可以上网搜下,很多例子

------解决方案--------------------
// 判断输入的是否是小写字母
// 如果是小写字母则返回true,否则返回false
public static boolean isXiaoXie(String input) {
boolean flg = false;
// 首先这样是获取输入的值,
// 判断小写字母
char a[] = new char[input.length()];
for (int i = 0; i < input.length(); i++) {
a[i] = input.charAt(i);
}
for (int i = 0; i < input.length(); i++) {
if (a[i] >= 'a' && a[i] <='z') {
                       //判断是否是大写字母的条件
                       //if (a[i] >= 'A' && a[i] <='Z') {
flg = true;
} else {
flg = false;
break;
}
}
return flg;
}

  相关解决方案