Java一段简单的代码,最好带上讲解~本人菜鸟
一个java方法,方法名叫sumSome(),有两个整形的参数:position和max;方法需要读入一串整形的数字,然后把这串数字中每个X位的数字加起来(X即为posi...
一个java方法,方法名叫sumSome(),有两个整形的参数:position和max;
方法需要读入一串整形的数字,然后把这串数字中每个X位的数字加起来(X即为position)直到加起来的总和超过max,此时停止读入数字。最后方法返回项加起来的总和total。
例如:answer=sumSome(3,10),把每三个读入的数加起来知道相加的和超过10,
如果读入一串数字“4,3,5,8,5,4,1,5,1,8,0,6……则返回total值为15(5+4+1+6) 展开
方法需要读入一串整形的数字,然后把这串数字中每个X位的数字加起来(X即为position)直到加起来的总和超过max,此时停止读入数字。最后方法返回项加起来的总和total。
例如:answer=sumSome(3,10),把每三个读入的数加起来知道相加的和超过10,
如果读入一串数字“4,3,5,8,5,4,1,5,1,8,0,6……则返回total值为15(5+4+1+6) 展开
展开全部
public static int sumSome(int position,int max){
int params[] = new int[]{4,3,5,8,5,3,1,5,1,8,0,6}; //读入的一串整数
int total = 0; //总和
for (int i = 0; i < params.length; i++) {
if((i+1)%position == 0){ //如果能整除postion,因为数组是从0开始,所以要加1
total += params[i];
}
}
return total;
}
int params[] = new int[]{4,3,5,8,5,3,1,5,1,8,0,6}; //读入的一串整数
int total = 0; //总和
for (int i = 0; i < params.length; i++) {
if((i+1)%position == 0){ //如果能整除postion,因为数组是从0开始,所以要加1
total += params[i];
}
}
return total;
}
追答
public static int sumSome(int position,int max){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串数字(以逗号分隔,如4,3,5,8,5,3,1,5,1,8,0,6):");
String params = sc.nextLine();
String nums[] = params.split(",");
int total = 0; //总和
for (int i = position-1; i max){
break;
}
}
return total;
}
public static void main(String[] args) {
System.out.println(sumSome(3, 10));
}
展开全部
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(sumSome(3,10));
}
public static int sumSome(int position, int max) {
int value = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
//Take input from keyboard
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()) {
list.add(scan.nextInt());
}
//Entering any letters rather than int numbers will end while loop above.
System.out.println(list);
//arraylist are indexed from 0, this is the reason i = position - 1
for(int i = position - 1; value < max; i += position) {
value += list.get(i);
}
return value;
}
}
/**
Now think what if the number of ints you have entered is quited limited,and quite a large max is considered, then Exception will occur. Solution:Add Try Catch.
**/
try {
for(int i = position - 1; value < max; i += position) {
value += list.get(i);
} catch (Exception e){
System.out.println(e);
System.out.println("value is constantly smaller than max");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static int sumSome(int pos,int max){
int[] args = {4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8};
int total = 0;
/**
* 数组下标从0开始,所以(pos - 1)
* 每次获取下标为 pos+pos的值,所以 i += pos
*/
for(int i = (pos - 1); i < args.length; i += pos){
total += args[i];
if(total > max){
break;
}
}
return total;
}
int[] args = {4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8};
int total = 0;
/**
* 数组下标从0开始,所以(pos - 1)
* 每次获取下标为 pos+pos的值,所以 i += pos
*/
for(int i = (pos - 1); i < args.length; i += pos){
total += args[i];
if(total > max){
break;
}
}
return total;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static int sumSome(int pos,int max, int ...args){
int result = 0;
for(int i = (pos - 1); i < args.length; i += pos){
result = result + args[i];
if(result > max){
break;
}
}
return result;
}
int result = 0;
for(int i = (pos - 1); i < args.length; i += pos){
result = result + args[i];
if(result > max){
break;
}
}
return result;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询