- public class Demo {
- private String[] arr;
- public void getInput(){
- System.out.println("请输入一个数字串,并以逗号隔开:");
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- arr = input.split(",");
- sort();
- method();
- }
- //为数组冒泡排序
- public void sort(){
- for(int x=0;x<arr.length-1;x++){
- for(int y=0;y<arr.length-x-1;y++){//-x:让每一次比较的元素减少 -1:避免角标越界
- //要比较大小所以要先转换成Integer类型
- if(Integer.valueOf(arr[y])<Integer.valueOf(arr[y+1])){
- swap(y,y+1);//满足条件的换位置
- }
- }
- }
- printmethod();
- }
- //打印数组
- public void printmethod(){
- System.out.print("{");
- for (int x = 0;x<arr.length;x++){
- if(x!=arr.length-1)
- System.out.print(arr[x]+",");
- else
- System.out.println(arr[x]+"}");
- }
- }
- //统计所输入数字的正数,负数及零的个数
- public void method(){
- int count=0;
- int count1=0;
- int count2=0;
- for (int x = 0;x<arr.length;x++){
- if(Integer.valueOf(arr[x])<0)
- count++;
- else if(Integer.valueOf(arr[x])>0){
- count1++;
- }
- else
- count2++;
- }
- System.out.println("正数个数为:"+count1);
- System.out.println("负数个数为:"+count);
- System.out.println("零的个数为:"+count2);
- }
- //为数组换位置
- public void swap(int a,int b){
- String temp;
- temp = arr[a];
- arr[a] = arr[b];
- arr[b] = temp;
- }
- public static void main(String[] args)throws Exception {
- Demo d = new Demo();
- d.getInput();
- }
- }
复制代码 你看看有没有什么问题?
|