写一个JAVA小程序。
发布网友
发布时间:2023-09-29 09:32
我来回答
共5个回答
热心网友
时间:2024-12-01 09:19
package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class TSceen {
private static Integer getInt() {
Integer i = null;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数长度不多于5位");
while (i == null) {
try {
int temp = new Integer(scanner.nextInt());
if (temp >= 100000 || temp <= -100000) {
System.out.println("请输入整数长度大于5位");
continue;
}
i = temp;
}catch (Exception e) {
System.out.println("请输入整数");
scanner = new Scanner(System.in);
}
}
return i;
}
private static void toChange(Integer i) {
String si = i.toString();
char str[] = si.toCharArray();
int len = str.length;
for (int j = 0; j < len; j++) {
switch (len - j) {
case 5:
System.out.println("万位:" + str[j]);
break;
case 4:
System.out.println("千位:" + str[j]);
break;
case 3:
System.out.println("百位:" + str[j]);
break;
case 2:
System.out.println("十位:" + str[j]);
break;
case 1:
System.out.println("个位:" + str[j]);
break;
}
}
System.out.println("参数: "+ i + "是" + len + "位数");
}
private static void outList() {
List<String> list = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);//ScreenReader.readLine();
String str = null;
for ( ; ; ) {
str = scanner.next();
if (str.equals("#end")){
break;
}
if (list.contains(str)) {
System.out.println("重复请重新输入:");
continue;
}
list.add(str);
}
Iterator<String> iter = list.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
public static void main(String[] args) {
Integer i = getInt();
toChange(i);
outList();
}
}
热心网友
时间:2024-12-01 09:20
代码仅供参考!第七题
import org.apache.log4j.Logger;
public class Test001 {
private Logger logger = Logger.getLogger(Test001.class);
public void printNum(Integer num){
try{
if(num==null){
logger.error("参数有误...");
return;
}
String nStr = num.toString();
if(nStr.length()>5){
logger.error("参数过长...");
return;
}
logger.info("输入的是:"+nStr.length()+"位数。");
Integer it = 0;
for(int i=nStr.length()-1;i>=0;i--){
it++;
if(it==1){
logger.info("个位:"+nStr.charAt(i));
}
if(it==2){
logger.info("十位:"+nStr.charAt(i));
}
if(it==3){
logger.info("百位:"+nStr.charAt(i));
}
if(it==4){
logger.info("千位:"+nStr.charAt(i));
}
if(it==5){
logger.info("万位:"+nStr.charAt(i));
}
}
} catch( Exception e ){
logger.error("error : ", e);
}
}
public static void main(String[] args) {
Test001 t001 = new Test001();
t001.printNum(123456);
}
}
热心网友
时间:2024-12-01 09:20
package com.test;
import java.util.ArrayList;
import java.util.Scanner;
public class Test005 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while(true) {
String input = scanner.next();
if("#end".equals(input)) {
break;
}else if(list.contains(input)){
System.out.println("已经包含,请重新输入:");
}else {
list.add(input);
}
}
for(String a : list) {
System.out.println(a);
}
}
}
追问可以用readLine调用的方法写一下吗?
热心网友
时间:2024-12-01 09:21
刚刚的回答 都不采纳 还答个毛线。。
热心网友
时间:2024-12-01 09:21
你需要哪一题?追问第七题