急急急! 在线等大神帮助解决个java小程序 写的好加分啊。
(3)任务分解1.字符串在Java中的存储,编写小程序实现输入字符串和输出字符串;2.字符串中特定位置字符的提取,比如提取第5,6位字符。(子字符串提取);3.特定字符串...
(3)任务分解
1. 字符串在Java中的存储,编写小程序实现输入字符串和输出字符串;
2. 字符串中特定位置字符的提取,比如提取第5,6位字符。(子字符串提取);
3. 特定字符串转换类型,比如将第5,6位子字符串转换为整数;
4. 字符串的匹配,比如一个字符串是否与另一个相同;
5. 整数的排序算法,给一列整数,按照从小到大排序。 展开
1. 字符串在Java中的存储,编写小程序实现输入字符串和输出字符串;
2. 字符串中特定位置字符的提取,比如提取第5,6位字符。(子字符串提取);
3. 特定字符串转换类型,比如将第5,6位子字符串转换为整数;
4. 字符串的匹配,比如一个字符串是否与另一个相同;
5. 整数的排序算法,给一列整数,按照从小到大排序。 展开
展开全部
import java.io.*;
import java.lang.*;
import java.util.regex.*;
public class Test
{
public static void main(String[] args) throws IOException
{
//输入输出字符串
System.out.println("请输入字符串:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine();
System.out.println("输入的字符串是:"+ input);
//字符串中特定位置字符的提取
Pattern isInteger = Pattern.compile("0|[1-9]+[0-9]*");//判断输入的是不是整数
Matcher mInteger;
Pattern isDigit = Pattern.compile("[+-]*[1-9]+[0-9]*.[0-9]*");//判断输入的是不是数字
Matcher mDigit;
int endLimit = input.length()-1;
System.out.println("请输入你想要提取字符的起始(包含)和终止(不包含)位置(>=0 && <="+ endLimit +"),并用断线连接(起始-终止),然后回车: ");
String locations = in.readLine();
String[] start_and_end = locations.split("-");
int start=0;
int end=0;
while(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches() && (start= Integer.parseInt(start_and_end[0]))<(end= Integer.parseInt(start_and_end[1])) && start>=0 && start <input.length()-1 && end>=1 && end <=input.length()-1 ))
{
if(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches()))
{
System.out.println("位置必须是整数,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else if(start >= end)
{
System.out.println("起始位置必须小于终止位置,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else
{
System.out.println("输入的位置必须 >=0 && <="+ endLimit+",请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
}
System.out.println(locations+"(不包含): "+input.substring(start, end));
//将特定字符串转换类型
System.out.println("请输入你想要转换类型的子字符串的起始(包含)和终止(不包含)位置,并用短线连接(起始-终止),然后回车:");
locations = in.readLine();
start_and_end = locations.split("-");
String sub;
while(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches() && (start= Integer.parseInt(start_and_end[0]))<(end= Integer.parseInt(start_and_end[1])) && start>=0 && start <input.length()-1 && end>=1 && end <=input.length()-1 && isDigit.matcher(sub=input.substring(start,end)).matches()))
{
if(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches()))
{
System.out.println("位置必须是整数,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else if(start>=end)
{
System.out.println("起始位置必须小于终止位置,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else if(!(start>=0 && start <input.length()-1 && end>=1 && end <=input.length()-1))
{
System.out.println("输入的位置必须 >=0 && <="+ endLimit+",请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else
{
System.out.println("要转换的子字符串不是数字形式(除非是单个数字,否则不能以0开头,或者包含非数字字符),请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
}
System.out.println("请输入想要转换的类型:1 Integer 2 Float 3 Double 4 Long");
String typeS = in.readLine();
int type = 0;
while(!(isInteger.matcher(typeS).matches() && (type=Integer.parseInt(typeS))>=1 && type<=4 && canTransfer(sub, type)))
{
if(!canTransfer(sub, type))
System.out.println("该字符串不能转换成此类型,请重新选择类型");
else
System.out.println("请输入1-4之间的整数:");
typeS = in.readLine();
}
switch(type){
case 1:
Integer i = Integer.parseInt(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ i.getClass());
break;
case 2:
Float f = Float.parseFloat(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ f.getClass());
break;
case 3:
Double d = Double.parseDouble(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ d.getClass());
break;
case 4:
Long l = Long.parseLong(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ l.getClass());
break;
default:
;
}
//字符串匹配
System.out.println("请输入想要比较的两个字符串");
String s1 = in.readLine();
String s2 = in.readLine();
if(s1.equals(s2))
System.out.println("相同!");
else
System.out.println("不相同!");
//整数排序
System.out.println("输入待排序数字,用逗号分隔:");
String numbers = in.readLine();
String[] numbersArray = numbers.split(",");
int[] integers = new int[numbersArray.length];
for(int i = 0; i< numbersArray.length; i++)
{
integers[i] = Integer.parseInt(numbersArray[i]);
}
/*for(int n: integers)
System.out.print(n+" ");*/
int[] result = mergeSort(integers, 0, numbersArray.length-1);
System.out.println("排序结果:");
for(int n : result)
System.out.print(n+" ");
System.out.println();
}
private static int[] mergeSort(int[] numbers, int start, int end)
{
if(start == end)
{
int[] result = {numbers[start]};
return result;
}
int[] first = mergeSort(numbers, start, start+((end-start)+1)/2-1);
int[] second = mergeSort(numbers, start+((end-start)+1)/2, end);
int totalLength = first.length+second.length;
int[] result = new int[totalLength];
int i = 0;
while(i<totalLength)
{
int fr = 0;
int se = 0;
while(fr<first.length && se<second.length)
{
if(first[fr]<second[se])
{
result[i++] = first[fr++];
}
else
result[i++] = second[se++];
}
while(fr<first.length)
result[i++] = first[fr++];
while(se<second.length)
result[i++] = second[se++];
}
return result;
}
private static boolean canTransfer(String s, int type)
{
switch(type){
case 1:
return canTransferInteger(s);
case 2:
return canTransferFloat(s);
case 3:
return canTransferDouble(s);
case 4:
return canTransferLong(s);
default:
return false;
}
}
private static boolean canTransferInteger(String s)
{
try
{
Integer.parseInt(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
private static boolean canTransferFloat(String s)
{
try
{
Float.parseFloat(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
private static boolean canTransferDouble(String s)
{
try
{
Double.parseDouble(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
private static boolean canTransferLong(String s)
{
try
{
Long.parseLong(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
}
import java.lang.*;
import java.util.regex.*;
public class Test
{
public static void main(String[] args) throws IOException
{
//输入输出字符串
System.out.println("请输入字符串:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine();
System.out.println("输入的字符串是:"+ input);
//字符串中特定位置字符的提取
Pattern isInteger = Pattern.compile("0|[1-9]+[0-9]*");//判断输入的是不是整数
Matcher mInteger;
Pattern isDigit = Pattern.compile("[+-]*[1-9]+[0-9]*.[0-9]*");//判断输入的是不是数字
Matcher mDigit;
int endLimit = input.length()-1;
System.out.println("请输入你想要提取字符的起始(包含)和终止(不包含)位置(>=0 && <="+ endLimit +"),并用断线连接(起始-终止),然后回车: ");
String locations = in.readLine();
String[] start_and_end = locations.split("-");
int start=0;
int end=0;
while(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches() && (start= Integer.parseInt(start_and_end[0]))<(end= Integer.parseInt(start_and_end[1])) && start>=0 && start <input.length()-1 && end>=1 && end <=input.length()-1 ))
{
if(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches()))
{
System.out.println("位置必须是整数,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else if(start >= end)
{
System.out.println("起始位置必须小于终止位置,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else
{
System.out.println("输入的位置必须 >=0 && <="+ endLimit+",请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
}
System.out.println(locations+"(不包含): "+input.substring(start, end));
//将特定字符串转换类型
System.out.println("请输入你想要转换类型的子字符串的起始(包含)和终止(不包含)位置,并用短线连接(起始-终止),然后回车:");
locations = in.readLine();
start_and_end = locations.split("-");
String sub;
while(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches() && (start= Integer.parseInt(start_and_end[0]))<(end= Integer.parseInt(start_and_end[1])) && start>=0 && start <input.length()-1 && end>=1 && end <=input.length()-1 && isDigit.matcher(sub=input.substring(start,end)).matches()))
{
if(!(isInteger.matcher(start_and_end[0]).matches() && isInteger.matcher(start_and_end[1]).matches()))
{
System.out.println("位置必须是整数,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else if(start>=end)
{
System.out.println("起始位置必须小于终止位置,请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else if(!(start>=0 && start <input.length()-1 && end>=1 && end <=input.length()-1))
{
System.out.println("输入的位置必须 >=0 && <="+ endLimit+",请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
else
{
System.out.println("要转换的子字符串不是数字形式(除非是单个数字,否则不能以0开头,或者包含非数字字符),请重新输入:");
locations = in.readLine();
start_and_end = locations.split("-");
}
}
System.out.println("请输入想要转换的类型:1 Integer 2 Float 3 Double 4 Long");
String typeS = in.readLine();
int type = 0;
while(!(isInteger.matcher(typeS).matches() && (type=Integer.parseInt(typeS))>=1 && type<=4 && canTransfer(sub, type)))
{
if(!canTransfer(sub, type))
System.out.println("该字符串不能转换成此类型,请重新选择类型");
else
System.out.println("请输入1-4之间的整数:");
typeS = in.readLine();
}
switch(type){
case 1:
Integer i = Integer.parseInt(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ i.getClass());
break;
case 2:
Float f = Float.parseFloat(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ f.getClass());
break;
case 3:
Double d = Double.parseDouble(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ d.getClass());
break;
case 4:
Long l = Long.parseLong(sub);
System.out.println("字符串"+sub+"已转换!转换后的类型为:"+ l.getClass());
break;
default:
;
}
//字符串匹配
System.out.println("请输入想要比较的两个字符串");
String s1 = in.readLine();
String s2 = in.readLine();
if(s1.equals(s2))
System.out.println("相同!");
else
System.out.println("不相同!");
//整数排序
System.out.println("输入待排序数字,用逗号分隔:");
String numbers = in.readLine();
String[] numbersArray = numbers.split(",");
int[] integers = new int[numbersArray.length];
for(int i = 0; i< numbersArray.length; i++)
{
integers[i] = Integer.parseInt(numbersArray[i]);
}
/*for(int n: integers)
System.out.print(n+" ");*/
int[] result = mergeSort(integers, 0, numbersArray.length-1);
System.out.println("排序结果:");
for(int n : result)
System.out.print(n+" ");
System.out.println();
}
private static int[] mergeSort(int[] numbers, int start, int end)
{
if(start == end)
{
int[] result = {numbers[start]};
return result;
}
int[] first = mergeSort(numbers, start, start+((end-start)+1)/2-1);
int[] second = mergeSort(numbers, start+((end-start)+1)/2, end);
int totalLength = first.length+second.length;
int[] result = new int[totalLength];
int i = 0;
while(i<totalLength)
{
int fr = 0;
int se = 0;
while(fr<first.length && se<second.length)
{
if(first[fr]<second[se])
{
result[i++] = first[fr++];
}
else
result[i++] = second[se++];
}
while(fr<first.length)
result[i++] = first[fr++];
while(se<second.length)
result[i++] = second[se++];
}
return result;
}
private static boolean canTransfer(String s, int type)
{
switch(type){
case 1:
return canTransferInteger(s);
case 2:
return canTransferFloat(s);
case 3:
return canTransferDouble(s);
case 4:
return canTransferLong(s);
default:
return false;
}
}
private static boolean canTransferInteger(String s)
{
try
{
Integer.parseInt(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
private static boolean canTransferFloat(String s)
{
try
{
Float.parseFloat(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
private static boolean canTransferDouble(String s)
{
try
{
Double.parseDouble(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
private static boolean canTransferLong(String s)
{
try
{
Long.parseLong(s);
return true;
}
catch(NumberFormatException numForEx)
{
return false;
}
}
}
展开全部
这个你给个for循环 把你开始的字符设置固定 然后没循环 就自加几 或者你看看 oracle的存储过程 就知道了
追问
我的意思是把前四个数相同的归为一类放到一起
追答
哦 你把前四个 String a=“2002····”这样定义一下 就好了 记得好评哦
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将首字符相同的放在一块?
追问
把前四位相同的放在一起
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
?????
追问
例如:订单信息如下所示:
20020301
19010702
20021003
01020801
19011103
20021901
19010302
01020501
20020203
19010504
05030901
输出为:
20020203
20020301
20021003
20021901
19010302
19010504
19010702
19011103
01020501
01020801
05030901
字符串的匹配,比如一个字符串是否与另一个相同。 问: 怎样编一个程序让它可以输出成下面的内容。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询