importjava.util.Scanner;publicclassJJArrangedNumbers1{publicstaticvoidmain(Stringargs[]){Scannerscanner=newScanner(System.in);booleanrepeatInput=true;do{Stringline;System...
import java.util.Scanner;
public class JJArrangedNumbers1
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
boolean repeatInput = true;
do
{
String line;
System.out.println("请任意输入数字,以空格间隔,以回车结束!");
line = scanner.nextLine();
ArrangedNumbers arrangeNumbers = new ArrangedNumbers();
arrangeNumbers .isNumber(line);
arrangeNumbers .getNumber();
arrangeNumbers .compareNumber();
arrangeNumbers .outputNumber();
String noInput = " ";
String input = scanner.nextLine();
if (noInput.equals(input))
{
System.exit(0);
}
}
while (repeatInput);
}
}
/**
*任意输入数字,并按升序排列输出
*
*@author Yang
*/
class ArrangedNumbers
{
int[] arrayNumbers;
StringBuffer stringbuffer = new StringBuffer();
/**
*判断字符串是否数字
*
*@return 返回是数字
*/
public boolean isNumber(String line)
{
char[] figures = line.toCharArray();
boolean isNumber = true;
for(int i=0;i
{
if(!(figures[i]>='1' && figures[i]<='9') && figures[i]!=32)
{
isNumber = true;
break;
}
isNumber = false;
}
if(!isNumber)
{
stringbuffer.append(line);
}
return isNumber;
}
/**
*将字符串转换为数字
*/
public void getNumber()
{
/*
数组中的整数总数
*/
int count = 0;
String inputContent = stringbuffer.toString();
String[] numbers = inputContent.split(" ");
arrayNumbers = new int[numbers.length];
for (int i=0;i
{
int memoryNumbers;
memoryNumbers=Integer.parseInt(numbers[i]);
arrayNumbers[count] = new Integer(memoryNumbers).intValue();
count++;
}
}
/**
*对数字进行排序
*/
public void compareNumber()
{
for (int i=0;i
{
for (int j=0;j
{
if(arrayNumbers[j]>arrayNumbers[j+1])
{
int temp;
temp = arrayNumbers[j];
arrayNumbers[j] = arrayNumbers[j+1];
arrayNumbers[j+1] = temp;
}
}
}
}
/**
*按升序排列输出
*/
public void outputNumber()
{
System.out.println("按升序排序:");
for(int i=0;i
{
System.out.println(arrayNumbers[i]);
}
}
}
问题:1.第一次输入66d不能进行判断 ;
询问是否继续输入时结果仍然是第一次的,也不能判断
如何改if(!(figures[i]>='1' && figures[i]<='9') && figures[i]!=32) ?
2.String[] numbers = inputContent.split(" ");
如何将其该为使用String类的index()和substring()函数?
3.每次排序完要回车,才能进行输入;输入空格时要输回车才能退出,如何避免回车,让其输入空格直接退出?
展开