一,初版
package question20183;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 跳一跳:
* 三种情况:0没跳上->游戏结束
* 1没跳到中心->得1分
* 2跳到且上次没跳到或第一次跳就跳到中心->得2分
* 连续跳到中心->得(上次分数+2)分
* 得分:100
*
* */
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int result = 0; //总得分
int doubleKill = 0; //跳到中心累加2分
int score = 0; //接收输入(1/2/0)
while( (score = sc.nextInt()) != 0 ) {
if(score == 1){
result += 1;
doubleKill = 0;
} else {
doubleKill += 2;
result += doubleKill;
}
}
System.out.print(result);
}
}
二,简化版
package question20183;
import java.util.Scanner;
/*简化代码,不把输入的数据添加到数组,然后从数组中进行取值判断;而是直接对输入的各种情况进行判断!
* 因为这种方法数组长度未知,且不好对输入数据进行判断使数组输入完毕
*
*
* 得分:100
*
*
* */
public class test {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int sore=0;
int rank=0;
int num=0;
while((num=s.nextInt())!=0) {
if(num==1) {
sore+=1;
rank=0;
}else{
rank+=2;
sore+=rank;
}
}
System.out.println(sore);
}
}