用java语言编写:任意输入东西,,并判断所输入的内容是数字,最后排序输出
importjava.io.*;publicclassstring{publicstaticvoidmain(Stringarg[])throwsjava.io.IOEx...
import java.io. *;
public class string
{
public static void main(String arg[]) throws java.io.IOException
{
boolean JudgeIsNum = true;
int num;
int x[];
num=5;
x=new int[num];
DataInputStream inp=new DataInputStream(System.in);//定义输入数据流inp
while (JudgeIsNum)
{
System.out.println("请输入5个数字:");
for (int k=0;k<=num-1;k++)
{
String character;//定义字符串character
character=inp.readLine();//读入字符流
for (int j = 0; j < character.length(); j++)
{
if (!(character.charAt(j) >= 48 && character.charAt(j) <= 57))
{
System.out.println("您输入的不是数字!请重新输入.");
JudgeIsNum = true;
break;
}
else
{
JudgeIsNum = false;
}
}
if (!JudgeIsNum)
{
int data = Integer.parseInt(character);
System.out.println("您输入的数字是:" + data);
x[k]= new Integer(data).intValue();
}
}
}//输入内容,并判断是否为数字
for (int k=0;k<=num-1;k++)
for (int j=k+1;j<=num-1;j++)
{
int t;
if (x[k]>x[j])
{
t=x[k];
x[k]=x[j];
x[j]=t;
}
}//数字进行比较
System.out.println("按升序排序:");
for (int k=0;k<=num-1;k++)
System.out.println(+ x[k] );
}
}
我用的是输入5个数字,如何编写任意输入的,使其变为动态的. 展开
public class string
{
public static void main(String arg[]) throws java.io.IOException
{
boolean JudgeIsNum = true;
int num;
int x[];
num=5;
x=new int[num];
DataInputStream inp=new DataInputStream(System.in);//定义输入数据流inp
while (JudgeIsNum)
{
System.out.println("请输入5个数字:");
for (int k=0;k<=num-1;k++)
{
String character;//定义字符串character
character=inp.readLine();//读入字符流
for (int j = 0; j < character.length(); j++)
{
if (!(character.charAt(j) >= 48 && character.charAt(j) <= 57))
{
System.out.println("您输入的不是数字!请重新输入.");
JudgeIsNum = true;
break;
}
else
{
JudgeIsNum = false;
}
}
if (!JudgeIsNum)
{
int data = Integer.parseInt(character);
System.out.println("您输入的数字是:" + data);
x[k]= new Integer(data).intValue();
}
}
}//输入内容,并判断是否为数字
for (int k=0;k<=num-1;k++)
for (int j=k+1;j<=num-1;j++)
{
int t;
if (x[k]>x[j])
{
t=x[k];
x[k]=x[j];
x[j]=t;
}
}//数字进行比较
System.out.println("按升序排序:");
for (int k=0;k<=num-1;k++)
System.out.println(+ x[k] );
}
}
我用的是输入5个数字,如何编写任意输入的,使其变为动态的. 展开
2个回答
展开全部
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JOptionPane;
public class Number {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
while (true) {
String str = JOptionPane.showInputDialog("输入数字");
int isContinue = 0;
try {
int num = Integer.parseInt(str);
list.add(num);
isContinue = JOptionPane.showConfirmDialog(null, "是否继续", "提示", JOptionPane.OK_CANCEL_OPTION);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "输入错误,请输入数字");
}
if (isContinue == 2) {
break;
}
}
Collections.sort(list);
for (int nums : list) {
System.out.println(nums);
}
}
}
参考一下吧 我没在你代码里面修改
import java.util.Collections;
import javax.swing.JOptionPane;
public class Number {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
while (true) {
String str = JOptionPane.showInputDialog("输入数字");
int isContinue = 0;
try {
int num = Integer.parseInt(str);
list.add(num);
isContinue = JOptionPane.showConfirmDialog(null, "是否继续", "提示", JOptionPane.OK_CANCEL_OPTION);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "输入错误,请输入数字");
}
if (isContinue == 2) {
break;
}
}
Collections.sort(list);
for (int nums : list) {
System.out.println(nums);
}
}
}
参考一下吧 我没在你代码里面修改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询