日期:2014-05-20 浏览次数:20893 次
import javax.swing.*; import java.awt.*; import java.util.Scanner; public class T { public static void main(String[]args)throws Exception{ System.out.println("请输入10个数字,以逗号分隔"); Scanner scanner=new Scanner(System.in); String inputStr=scanner.nextLine(); String[] ary=inputStr.split(","); int[] nums=new int[ary.length]; for(int i=0;i<ary.length;i++){ nums[i]=Integer.parseInt(ary[i]); } int num1Ouput=0; int num2Ouput=0; int num3Ouput=0; for(int i:nums){ if(i==1){ num1Ouput++; } if(i==2){ num2Ouput++; } if(i==3){ num3Ouput++; } } System.out.println("1出现次数"+num1Ouput); System.out.println("2出现次数"+num2Ouput); System.out.println("3出现次数"+num3Ouput); } }
------解决方案--------------------
import java.io.*; public class test2 { public static void main(String args[]) throws IOException { System.out.println("Please input:"); byte buffer[] = new byte[512]; int ct[]=new int[4]; int ins=System.in.read(buffer); for(int i=0;i<ins;i++) { int c=buffer[i]-'0'; switch(c) { case 1: ct[c]++; break; case 2: ct[c]++; break; case 3: ct[c]++; break; } } System.out.printf("1 count=%d ,2 count=%d,3 count=%d ",ct[1],ct[2],ct[3]); } }