需要帮助缩短我在java中的代码(netbeans)

时间:2015-09-12 06:42:12

标签: arrays

我在缩短代码方面遇到了问题。 样本输出: 你会输入多少个整数? 4 输入4个整数,每行一个: 2 1 1 2 总和是6。 数字是: 2是总和的33.33%。 1是总和的16.67%。 1是总和的16.67%。 2这是总和的33.33%。

示例输出应为此。我已经完成并执行任务,但我的代码很长。

包labexperiment4;

import java.util.Scanner; 公共课LabExperiment4 {

public static void main(String[] args) {
Scanner input = new Scanner (System.in);

double num; 
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
double num7;
double num8;
double num9;
double num10;
double sum;




System.out.print("How many integers you will enter: ");
num=input.nextInt();

if (num == 1){
    num1=input.nextInt();
    System.out.println("The sum is: " +(num1));
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/num1)*100));
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 2){
    num1=input.nextInt();
    num2=input.nextInt();
    sum=num1+num2;

    System.out.println("The sum is: " +(sum));
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print((+(num2/sum)*100));
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 3){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    sum=num1+num2+num3;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 4){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    sum=num1+num2+num3+num4;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 5){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    num5=input.nextInt();
    sum=num1+num2+num3+num4+num5;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num5);
    System.out.print(" which is ");
    System.out.print(+((num5/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");

} else if (num == 6) {
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    num5=input.nextInt();
    num6=input.nextInt();
    sum=num1+num2+num3+num4+num5+num6;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num5);
    System.out.print(" which is ");
    System.out.print(+((num5/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num6);
    System.out.print(" which is ");
    System.out.print(+((num6/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 7){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    num5=input.nextInt();
    num6=input.nextInt();
    num7=input.nextInt();
    sum=num1+num2+num3+num4+num5+num6+num7;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num5);
    System.out.print(" which is ");
    System.out.print(+((num5/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num6);
    System.out.print(" which is ");
    System.out.print(+((num6/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num7);
    System.out.print(" which is ");
    System.out.print(+((num7/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 8){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    num5=input.nextInt();
    num6=input.nextInt();
    num7=input.nextInt();
    num8=input.nextInt();
    sum=num1+num2+num3+num4+num5+num6+num7+num8;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
     System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
     System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
     System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
     System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
     System.out.print(+num5);
    System.out.print(" which is ");
    System.out.print(+((num5/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
     System.out.print(+num6);
    System.out.print(" which is ");
    System.out.print(+((num6/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
     System.out.print(+num7);
    System.out.print(" which is ");
    System.out.print(+((num7/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num8);
    System.out.print(" which is ");
    System.out.print(+((num8/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 9){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    num5=input.nextInt();
    num6=input.nextInt();
    num7=input.nextInt();
    num8=input.nextInt();
    num9=input.nextInt();
    sum=num1+num2+num3+num4+num5+num6+num7+num8+num9;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num5);
    System.out.print(" which is ");
    System.out.print(+((num5/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num6);
    System.out.print(" which is ");
    System.out.print(+((num6/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num7);
    System.out.print(" which is ");
    System.out.print(+((num7/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num8);
    System.out.print(" which is ");
    System.out.print(+((num8/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num9);
    System.out.print(" which is ");
    System.out.print(+((num9/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} else if (num == 10){
    num1=input.nextInt();
    num2=input.nextInt();
    num3=input.nextInt();
    num4=input.nextInt();
    num5=input.nextInt();
    num6=input.nextInt();
    num7=input.nextInt();
    num8=input.nextInt();
    num9=input.nextInt();
    num10=input.nextInt();
    sum=num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
    System.out.println("The sum is: " +sum);
    System.out.println("The Numbers are: ");
    System.out.print(+num1);
    System.out.print(" which is ");
    System.out.print(+((num1/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num2);
    System.out.print(" which is ");
    System.out.print(+((num2/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num3);
    System.out.print(" which is ");
    System.out.print(+((num3/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num4);
    System.out.print(" which is ");
    System.out.print(+((num4/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num5);
    System.out.print(" which is ");
    System.out.print(+((num5/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num6);
    System.out.print(" which is ");
    System.out.print(+((num6/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num7);
    System.out.print(" which is ");
    System.out.print(+((num7/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num8);
    System.out.print(" which is ");
    System.out.print(+((num8/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num9);
    System.out.print(" which is ");
    System.out.print(+((num9/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
    System.out.print(+num10);
    System.out.print(" which is ");
    System.out.print(+((num10/sum))*100);
    System.out.print("%");
    System.out.println(" of the sum");
} 
}  

}

1 个答案:

答案 0 :(得分:1)

我认为这可能会对你有所帮助,但我认为这是你的功课,所以最好先理解代码,然后自己尝试。

@Test
public void myTest()
{
    // Prepare the input parameters:
    ...

    // Perform the test:
    try
    {
       int result=countFieldEventPoints(row, centimeters);
       // Check the results:
       assert...
    }
    catch (SomeException e)
    {
        fail(messsageInCaseOfFail);
    }
}