日期:2014-05-20 浏览次数:20783 次
List<Integer> list = new ArrayList<Integer>(); Scanner sc = new Scanner(System.in); boolean flag = true; while(flag){ Integer temp = sc.nextInt(); if(temp == 0){ flag = false; } list.add(temp); } int gtZero = 0; int sum = 0; for(Integer i : list){ if(i > 0){ gtZero ++; sum += i; } } System.out.println("大于零的个数:" + gtZero); System.out.println("总和:" + sum);
------解决方案--------------------
2、
public static void change(double args[], int i, double param){ if(args != null){ for(int index1=0; index1<args.length; index1++){ if(index1 == i){ for(int index2=args.length - 1; index2>=index1; index2--){ args[index2] = args[index2-1]; } args[index1] = param; } } for(double temp : args){ System.out.println(temp); } } }
------解决方案--------------------
import java.util.Scanner; public class Test6 { public static void main(String[] args){ int count = 0; int sum = 0; Scanner sc = new Scanner(System.in); int tmp = sc.nextInt(); while(tmp != 0){ if(tmp>0){ count++; sum += tmp; } tmp = sc.nextInt(); } System.out.println(count+" "+sum); } }
------解决方案--------------------
//[个人意见,供参考] class Main { public static void main(String [] args) { //编译运行时的格式:javac Main.java // java Main 1 2 3 -4 -5 6 0 //1.使用args接收输入数据, int count=0; int value=0; int len=0; for(int i=0;i<args.length-1;i++) { if((len=args[i].toCharArray()[0])>'0') { count++; value=value+(len-48); System.out.println((len-48)); } } System.out.println("个数为:"+count+",和为:"+value+"\r\n"); //2.需要新建一数组,且比原数组length大1 double [] d1 ={1.0,2.0,3.0,4.0,5.0}; int i=4; double d2=8.0; method(d1,i,d2); } public static void method(double[] d1,int i,double d2) { double [] dou=new double[d1.length+1]; for(int j=0;j<dou.length;j++) { if(j<i) dou[j]=d1[j]; else if(j==i) dou[j]=d2; else dou[j]=d1[j-1]; } for(int j=0;j<d1.length;j++) { System.out.print(d1[j]+" "); } System.out.println(); for(int j=0;j<dou.length;j++) { System.out.print(dou[j]+" "); } } }