简体   繁体   English

程序不打印出除一个以外的整数

[英]Program does not print out integers except for one

So I made this program before without the methods and it worked.所以我之前在没有方法的情况下制作了这个程序并且它有效。 However, I made the methods and then things stop working.然而,我做了这些方法,然后事情就停止了。 The only line that works is the print array and greaterThanAverage.唯一有效的行是打印数组和 greaterThanAverage。 I will show the original code in case anyone wants to see it.我会展示原始代码,以防有人想看。 The program only prints 0 for every other thing.该程序只为所有其他内容打印 0。

public static void printArray (int n)
{
  int[] arr = new int[n];
  System.out.println("Array on one line: ");
  for(int i = 0; i < arr.length; i++)
  {
     arr[i] = (int) (Math.random() * 500) + 1;
     System.out.print(arr[i] + " ");
  }
}
public static void getMax(int n)
{  
  int[] arr = new int[n];
  int max = arr[0];

  for(int i = 1; i < arr.length; i++)
  {
     if(arr[i] > max)
     {
        max = arr[i];
     }
  }
  System.out.println();
  System.out.println("Maximum: " + max);
}
public static void getMin(int n)
{
  int[] arr = new int[n];
  int min  = arr[0];

  for(int i = 1; i < arr.length; i++)
  {
     if(arr[i] < min)
     {
        min = arr[i];
     }
  }
     System.out.println("Minimum: " + min);
}
public static void getAverage(int n)
{
  double x = 0;
  double y;
  int[] arr = new int[n];
  for(int i = 0; i < arr.length; i ++)
  {
     x = arr[i] + x;
  }
     y = x / arr.length;
    System.out.println("Average: " + y);
}
public static void sumValues(int n)
{
  int sum = 0;
  int[] arr = new int[n];
  for(int num : arr)
  {
     sum = sum + num;
  }
    System.out.println("Sum: " + sum);
}
public static void greaterThanAverage (int n)
{
  int[] arr = new int[n];
  int count = 1;
  double y;
  for(int i = 0; i < arr.length; i ++)
  {
     {         
        count ++;              
     }   
  }       
     System.out.println("Number of integers greater than average: " + count);
}
public static void countInc (int n)
{
  int[] arr = new int[n];
  int count2 = 0;
  for(int i = 0; i < arr.length - 1; i++)
  {       
     if (arr[i] < arr[i + 1])
     {         
        count2++;
     }
  }
     System.out.println("Number of times subsequent value increases: " + count2);
}
public static void countDec (int n)
{
  int[] arr = new int[n];
  int count3 = 0;
  for(int i = 0; i < arr.length - 1; i++)
  {
     if (arr[i] > arr[i + 1])
     {
        count3 ++;
     }
  }
     System.out.println("Number of times  subsequent value decreases: " + count3);
}
public static void printArray3PerLn (int n)
{
  System.out.println("Array with 3 elements per line: ");
  int[] arr = new int[n];
  for(int i = 0; i < arr.length; i++)
  { 
     System.out.print(arr[i]+ " "); 
     if(i % 3 == 2)
     {
        System.out.println();
     }
  }
}
public static void main(String[] args)
{
   Scanner kbd = new Scanner(System.in);
   int sum = 0;
   int n;
   do
   {
     System.out.print("Enter integer n, greater than 0: ");
     n = kbd.nextInt();
     }  while(n < 1);      
     System.out.println();

     printArray(n);

     getMax(n);

     getMin(n);

     sumValues(n);

     getAverage(n);

     greaterThanAverage(n);

     countInc(n);

     countDec(n);

     printArray3PerLn(n);
 }
}

It is because you are not filling arrays with numbers这是因为你没有用数字填充 arrays

**public static void  countDec (int n)
{
  int[] arr = new int[n]; -<<**

example if u call the method countDec(5);例如,如果您调用方法 countDec(5); It will fill this array with 0 not with random numbers as u did in the printArrayMethod它将用 0 填充此数组,而不是像您在 printArrayMethod 中那样使用随机数

Try this.试试这个。

 public static void getAverage(int n)
    {
    double x = 0;
    double y =0;
    anArray = new double[n];
        for(int i=0;i<anArray.length;i++)
        {
            anArray[i] = randomFill();
        }
    for(int ji = 0; ji < anArray.length; ji ++)
      {
         x = arr[ji] + x;
      }
         y = x / arr.length;
        System.out.println("Average: " + y);
    }

Method for random Numbers随机数方法

public static double randomFill(){

    Random rand = new Random();
    int randomNum = rand.nextInt();
    return randomNum;
    }

The problem here is that you are recreating a new local array inside each method instead of passing an array you create once as an argument to each method.这里的问题是您在每个方法中重新创建一个新的本地数组,而不是将您创建一次的数组作为参数传递给每个方法。 To rectify this, create a method to create and return your array from an integer size and change the method signature of each of your other methods to have an int array as a parameter.要纠正此问题,请创建一个方法来创建和返回大小为 integer 的数组,并将每个其他方法的方法签名更改为将 int 数组作为参数。

See the working code here: https://ideone.com/ycZbNX请在此处查看工作代码: https://ideone.com/ycZbNX

import java.util.Scanner;

public class EntryPoint{
    public static int[] getArray(final int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (int)(Math.random() * 500) + 1;
        }
        return arr;
    }
    public static void printArray(final int[] arr) {
        System.out.println("Array on one line: ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static void getMax(final int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println();
        System.out.println("Maximum: " + max);
    }
    public static void getMin(final int[] arr) {
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("Minimum: " + min);
    }
    public static void getAverage(final int[] arr) {
        double x = 0;
        double y;
        for (int i = 0; i < arr.length; i++) {
            x = arr[i] + x;
        }
        y = x / arr.length;
        System.out.println("Average: " + y);
    }
    public static void sumValues(final int[] arr) {
        int sum = 0;
        for (int num: arr) {
            sum = sum + num;
        }
        System.out.println("Sum: " + sum);
    }
    public static void greaterThanAverage(final int[] arr) {
        double average = 0;
        for(int num: arr){
            average += num;
        }
        average /= arr.length;
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > average){
                ++count;
            }
        }
        System.out.println("Number of integers greater than average: " + count);
    }
    public static void countInc(final int[] arr) {
        int count2 = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] < arr[i + 1]) {
                count2++;
            }
        }
        System.out.println("Number of times subsequent value increases: " + count2);
    }
    public static void countDec(final int[] arr) {
        int count3 = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                count3++;
            }
        }
        System.out.println("Number of times  subsequent value decreases: " + count3);
    }
    public static void printArray3PerLn(final int[] arr) {
        System.out.println("Array with 3 elements per line: ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
            if (i % 3 == 2) {
                System.out.println();
            }
        }
    }
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        int sum = 0;
        int n;
        do {
            System.out.print("Enter integer n, greater than 0: ");
            n = kbd.nextInt();
        } while (n < 1);
        System.out.println();
        final int[] arr = getArray(n);
        printArray(arr);

        getMax(arr);

        getMin(arr);

        sumValues(arr);

        getAverage(arr);

        greaterThanAverage(arr);

        countInc(arr);

        countDec(arr);

        printArray3PerLn(arr);
    }
}

So I made this program before without the methods and it worked.所以我之前在没有方法的情况下制作了这个程序并且它有效。 However, I made the methods and then things stop working.但是,我制定了这些方法,然后事情就停止了。 The only line that works is the print array and greaterThanAverage.唯一有效的行是打印数组和更大的ThanAverage。 I will show the original code in case anyone wants to see it.我将显示原始代码以防万一有人想看到它。 The program only prints 0 for every other thing.该程序只为其他所有内容打印 0。

public static void printArray (int n)
{
  int[] arr = new int[n];
  System.out.println("Array on one line: ");
  for(int i = 0; i < arr.length; i++)
  {
     arr[i] = (int) (Math.random() * 500) + 1;
     System.out.print(arr[i] + " ");
  }
}
public static void getMax(int n)
{  
  int[] arr = new int[n];
  int max = arr[0];

  for(int i = 1; i < arr.length; i++)
  {
     if(arr[i] > max)
     {
        max = arr[i];
     }
  }
  System.out.println();
  System.out.println("Maximum: " + max);
}
public static void getMin(int n)
{
  int[] arr = new int[n];
  int min  = arr[0];

  for(int i = 1; i < arr.length; i++)
  {
     if(arr[i] < min)
     {
        min = arr[i];
     }
  }
     System.out.println("Minimum: " + min);
}
public static void getAverage(int n)
{
  double x = 0;
  double y;
  int[] arr = new int[n];
  for(int i = 0; i < arr.length; i ++)
  {
     x = arr[i] + x;
  }
     y = x / arr.length;
    System.out.println("Average: " + y);
}
public static void sumValues(int n)
{
  int sum = 0;
  int[] arr = new int[n];
  for(int num : arr)
  {
     sum = sum + num;
  }
    System.out.println("Sum: " + sum);
}
public static void greaterThanAverage (int n)
{
  int[] arr = new int[n];
  int count = 1;
  double y;
  for(int i = 0; i < arr.length; i ++)
  {
     {         
        count ++;              
     }   
  }       
     System.out.println("Number of integers greater than average: " + count);
}
public static void countInc (int n)
{
  int[] arr = new int[n];
  int count2 = 0;
  for(int i = 0; i < arr.length - 1; i++)
  {       
     if (arr[i] < arr[i + 1])
     {         
        count2++;
     }
  }
     System.out.println("Number of times subsequent value increases: " + count2);
}
public static void countDec (int n)
{
  int[] arr = new int[n];
  int count3 = 0;
  for(int i = 0; i < arr.length - 1; i++)
  {
     if (arr[i] > arr[i + 1])
     {
        count3 ++;
     }
  }
     System.out.println("Number of times  subsequent value decreases: " + count3);
}
public static void printArray3PerLn (int n)
{
  System.out.println("Array with 3 elements per line: ");
  int[] arr = new int[n];
  for(int i = 0; i < arr.length; i++)
  { 
     System.out.print(arr[i]+ " "); 
     if(i % 3 == 2)
     {
        System.out.println();
     }
  }
}
public static void main(String[] args)
{
   Scanner kbd = new Scanner(System.in);
   int sum = 0;
   int n;
   do
   {
     System.out.print("Enter integer n, greater than 0: ");
     n = kbd.nextInt();
     }  while(n < 1);      
     System.out.println();

     printArray(n);

     getMax(n);

     getMin(n);

     sumValues(n);

     getAverage(n);

     greaterThanAverage(n);

     countInc(n);

     countDec(n);

     printArray3PerLn(n);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么该程序打印出“ 4 2 1 0”? - Why does this program print out “4 2 1 0”? Java:如何在单个文本文件中打印升序列,然后在该列的旁边打印同一组整数,除了按降序排列 - Java : How do I print an ascending column and next to that column the same set of integers except in descending order all in one single text file 编写一个程序,打印一个包含从 1 到 40 的整数及其平方、平方根和倒数的表格 - Write a program that will print a table containing the integers from one to forty along with their squares, square roots, and reciprocals 为什么程序打印高度值0而不是我设置的高度值? - Why does the program print the height value 0 instead of the one I set? 无法打印出读取整数:java - Cannot print out read integers: java 为什么我的程序将数组中的数字打印为零,但仍然打印出数组中的最大值? - why does my program print the numbers in my array as zeros but still print the largest value out of the array? 程序将编译,但表不会打印出来 - The program will compile but the table will not print out 为什么打印出0? - Why does it print out 0? 将一列整数打印成多列 - Print one column of integers into multiple columns 为什么我的质数java程序不打印出质数? - Why does my prime number java program not print out prime numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM