Pregunta

Is there a way to shorthand System.out.println() as a method?

System.out.println() = short();

So I could change this main method to...

package Array;
import java.util.Random;

public class Array {
    public static void main(String args[]) {
        data();
        a();
        random();
        a();
        array();
        a();
        rows();
        a();
        System.out.println("average " + average(45,15,15,48,97,45));

    }
    static void a() {System.out.println();}
    static void data() {
        int array[] = {1,5,6};
        int alength = array.length;
        System.out.println("  Location\tData");
        for(int i=0;i<alength;i++) {
            System.out.println("  " + i + "\t\t" + array[i]);
        }
    }
    static void random() {
        Random rdm = new Random();
        int freq[] = new int[7];

        for(int i=1;i<1000;i++) {
            ++freq[1+rdm.nextInt(6)];
        }
        System.out.println("Face\tFrequency");
        int frequence = freq.length;
        for(int face=1;face<frequence;face++) {
            System.out.println(face+"\t"+freq[face]);
        }
    }
    static void array() {
        String po[] = {"lala","po","tinkiwinki","disty"};
        for(String lala: po) {
            System.out.print(lala + " ");
        }
        System.out.println();
    }
    static void rows() {
        int arrays[][]= {{1,5,78,15},{45,67},{875,15687,158,4515,23,2,2}};

        for(int i=0;i<arrays.length;i++) {
            for(int j=0;j<arrays[i].length;j++) {
                System.out.print(arrays[i][j]+"\t");
            }
            System.out.println();
        }
    }
    static int average(int...numbers) {
        int total=0;
        for(int x:numbers)
            total += x;
        return total/numbers.length;
    }
}

class time {
    int h, m, s;
    void setTime(int hour,int minute,int second) {
        h = ((hour>=0 && hour<=24) ? hour : 0);
        m = ((minute>=0 && minute<=60) ? minute : 0);
        s = ((second>=0 && second<=60) ? second : 0);
    }

This main method

 public class Array {
        public static void main(String args[]) {
            a(data());
            a(random());
            a(array());
            a(rows());
            a("average " + average(45,15,15,48,97,45));

It prints out this

      Location  Data
  0     1
  1     5
  2     6

Face    Frequency
1   154
2   186
3   161
4   168
5   179
6   151

lala po tinkiwinki disty 

1   5   78  15  
45  67  
875 15687   158 4515    23  2   2   

average 44
¿Fue útil?

Solución

So you want a() to print a newline, then what's passed to it?

static void a(Callable<T> f) {
    System.out.println();
    f.call();
}

I think this should do it.

If you don't want to mess with Callable:

Instead of having your methods return void and printing, have them return String.

Then have a take a String as an argument and print the newline followed by the String passed in.

Otros consejos

Your code almost makes me cry.

But to the point, i'am not sure what your trying to achieve here. I really dont see what wrong with System.out.println(); Try using a toString() method in your array class and try taking an object orientated approach.

I think you should use Object Oriented practices :

  • your data, random, array, etc. should a Classes
  • classes with a custom toString() or a prettyPrint() method

So your aim is something like :

Data data = new Data();
System.out.println(data.toString());

But I'm not sure what the question is really about...

In Eclipse you can type Syso and press Ctrl+Space at the same time if you want to make it easier to type it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top