用数组重复一个程序

时间:2014-08-14 05:03:00

标签: java arrays repeat

我试图让我的程序重复,但它运作得不好。这就是我到目前为止所做的:

import java.util.*;//to use scanner class
public class ArrayDemo
{
public static void main(String args[])
{
   int a[]=new int[5];
   Scanner sc=new Scanner(System.in);//use to take input
   int i;

   for(i=0;i<a.length;i++)//arrayname.length gives the length of array
   {
       System.out.println("Enter number:");
       a[i]=sc.nextInt();
   }

   int sum=0;
   long pro=1;
   int max=a[0];
   int min=a[0];
   for(i=0;i<a.length;i++)
   {
       sum=sum+a[i];
       pro=pro*a[i];
       if(a[i]>max)
       max=a[i];
       if(a[i]<min)
       min=a[i];



   }
   float avg=sum/a.length;
   System.out.print("You entered");
   for(i=0;i<a.length;i++)
   {
       System.out.print(", 1"+a[i]);

   }

       System.out.println("\nThe sum of those numbers is "+sum);
       System.out.println("The product of those numbers is "+pro);
       System.out.println("The largest number entered is "+max);
       System.out.println("The smallest number entered is "+min);
       System.out.println("The average of the numbers entered is "+avg);

}
}

我有另一个程序,我试图重复,但它说没有主要的applet或方法,所以它不会返回任何东西:

import java.util.*;//to use scanner class
public class ArrayDemo
{
public static void myMethod(Scanner scanner)
{

   int a[]=new int[5];
          int i;
   for(i=0;i<a.length;i++)//arrayname.length gives the length of array
   {
       System.out.println("Enter number:");
       a[i]=scanner.nextInt();
   }

   int sum=0;
   long pro=1;
   int max=a[0];
   int min=a[0];
   for(i=0;i<a.length;i++)
   {
       sum=sum+a[i];
       pro=pro*a[i];
       if(a[i]>max)
       max=a[i];
       if(a[i]<min)
       min=a[i];


   }
   float avg=sum/a.length;
   System.out.print("You entered");
   for(i=0;i<a.length;i++)
   {
       System.out.print(","+a[i]);

   }

       System.out.println("\nThe sum of those numbers is "+sum);
       System.out.println("The product of those numbers is "+pro);
       System.out.println("The largest number entered is "+max);
       System.out.println("The smallest number entered is "+min);
       System.out.println("The average of the numbers entered is "+avg);

       System.out.println();


do {
    myMethod (scanner);
    System.out.println("You want to continue : (Y/N) ");
} while("Y".equalsIgnoreCase(scanner.next().trim()));   

scanner.close ();



}

}

3 个答案:

答案 0 :(得分:1)

你需要一个像

这样的主要方法
public static void main(String args[])

  Scanner scanner = new Scanner(System.in);

  do {
    myMethod (scanner);
    System.out.println("You want to continue : (Y/N) ");
  } while("Y".equalsIgnoreCase(scanner.next().trim()));   

  scanner.close ();

}

所以将此代码从你的(my)`myMethod

移到main方法

答案 1 :(得分:0)

您的第一个代码有主要方法:

public static void main(String[] args) {// some code }

但第二个代码没有这个方法。在java语言中,程序必须从具有上述签名的main方法开始。

答案 2 :(得分:0)

在你的第二种方法中,你没有主要的方法。因此,您无法运行该文件。 另一件事是不要将所有逻辑放在main方法中。尽可能地将它们分开。然后它更清楚。 将计算逻辑放入方法并从while调用它或执行while循环

e.g。

class A{
     public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          int[] a= new int[5];
          boolean bRepeat = true;
          while(bRepeat){
              for(i=0;i<a.length;i++){
                  System.out.println("Enter number:");
                  a[i]=sc.nextInt();
              }
              System.out.println("Sum is " + getSum(a));
              System.out.println("Average is " + getAvergae(a));
              .....
              System.out.println("Do you wanna repeat? T/F");
              String response = scanner.nextLine();
              if(response.equalsIgnoreCase("T"))
                  bRepeat = true;
              else
                  bRepeat = false;                      // or break
          }
     }

     public static int getSum(int[] number){
         //logic
     }
     public static double getAverage(int[] number){
         //logic
     }
     public static int getProduct(int[] number){
         //logic
     }
     public static int getMax(int[] number){
         //logic
     }
     ................
}