日期:2014-05-20 浏览次数:20823 次
import java.util.Scanner; public class Test{ public static void main (String[]args){ String book[] = new String[]{a,c,d,s,f}; String newbook[] = (book.length+1); int index = book.length; //储存输入的新书名的位置 for (int i = 0;i<book.length;i++){ //复制数组 newbook[i]=book[i]; } for (int i =0;i<book.length;i++){ //输出原数组 System.out.print(book[i]+""); } Scanner input = new Scanner (System.in); System.out.println ("请输入新书的名称(英文):") String bookname = input.next(); for (int i = book.length-1;i>=0;i--){ //查找新书名称所在的位置 if (book[i].compareToIgnoreCase(bookname)>0){ //通过比较得出新书名称的合适位置 index = i ; break; } } newbook[index]=bookname; for ( int i = book.length-1;i>index;i-- ){ //让index后的所有变量后退一位 newbook[i]=newbook[i-1]; } for (int c=0;c<newbook.length;c++){ //输出新数组 System.out.print(newbook[c]+"") } } }
String book[] = {"","","","",""}; String newbook[] = new String[book.length+1]; int index = book.length; //储存输入的新书名的位置 for (int i = 0;i<book.length;i++){ //复制数组 newbook[i]=book[i]; } for (int i =0;i<book.length;i++){ //输出原数组 System.out.print(book[i]+""); } Scanner input = new Scanner (System.in); System.out.println ("请输入新书的名称(英文):"); String bookname = input.next(); for (int i = book.length-1;i>=0;i--){ //查找新书名称所在的位置 if (book[i].compareToIgnoreCase(bookname)>0){ //通过比较得出新书名称的合适位置 index = i ; break; } } newbook[index]=bookname; for ( int i = book.length-1;i>index;i-- ){ //让index后的所有变量后退一位 newbook[i]=newbook[i-1]; } for (int c=0;c<newbook.length;c++){ //输出新数组 System.out.print(newbook[c]+""); } }
------解决方案--------------------
确实 是要改的 改了后 输出结果为:
D:\>java Test
请输入新书的名称(英文):
cary
cary