简体   繁体   English

带输入的简单Java 2D数组

[英]simple Java 2D Array with input

I'm new to Java and got some work at school. 我是Java的新手,在学校有一些工作。 Now I don't know how to continue and I hope you can help me. 现在我不知道如何继续,希望您能帮助我。

That's my code till now 到目前为止,这是我的代码

    package bp;
import java.util.Scanner;

public class BenzinP {

    private int row = 4;
    private int col = 4;
    private int[][] matrix;

    public BenzinP(int trow, int tcol) {

        this.row = trow;
        this.col = tcol;
    }

    public BenzinP(int trow, int tcol, int[][] m) {

        this.row = trow;
        this.col = tcol;
        this.matrix = m;
    }
    public int[][] fill(){ 
        int[][] data = new int[row][col]; 
        Scanner in = new Scanner(System.in);
        for(int row = 0; row< matrix.length; row++){ 
              for(int col = 0 ;col< matrix.length; col++){ 
                  System.out.println("Date"); 
                  data[row][0] = in.nextInt(); 
                  System.out.println("Price"); 
                  data[row][1] = in.nextInt(); 
               }
              System.out.println(); 
          } 

           for(int row = 0; row< matrix.length; row++){
       for(int col = 0 ;col< matrix[row].length; col++){ 
             System.out.print(data[row][col]);
       } 
      System.out.println(); 
   }
         return data; 
    }



    public static void main(String[] args){
        int[][] ma = new int[3][2];
        BenzinP q2 = new BenzinP(3, 2,ma);
        q2.fill();
    }
}

The task is: Create a Java program which can save the prices for gas for different days and can be outputted in different formats (sort for highes price or date). 任务是:创建一个Java程序,该程序可以保存不同日期的汽油价格,并可以以不同的格式输出(按最高价格或日期排序)。 You should use a 2D Array which can safe data for at least 30 days. 您应该使用可以安全保存至少30天数据的2D阵列。 Also it should show the average, min. 还应显示平均值(最小值)。 and max price. 和最高价格。 Also the program should be outsourced in methodes etc. 程序也应该外包在方法等中。

I hope you can help and keep it simple. 希望您能提供帮助并保持简单。

I will make some assumptions in order to be able to answer this: 我将做一些假设以便能够回答这个问题:

if BenzinP is a class, that should not contain the main method. 如果BenzinP是一个类,则不应包含main方法。 Only the main class in your project should contain the main, since that is what the JVM expects to be the "point of entry" in your code. 只有项目中的主类才应包含主类,因为这是JVM期望成为代码中的“入口点”。

public static void main(String[] args)

Since this is an exercise, dont worry so far about this. 由于这是一种练习,因此请不要为此担心。

Then, the BenzinP class has 2 constructors that are not really needed. 然后, BenzinP类具有2个确实不需要的构造函数。

I suggest the following: 我建议以下内容:

BenzinP(){
   this.matrix=new int[this.row][this.col];
}

Create the rest of the methods you want, such as query the highest value, lowest, etc.. and start your main 创建所需的其余方法,例如查询最大值,最小值等,然后启动主方法

public static void main(String[] args){
    BenzinP q2 = new BenzinP();
    q2.fill();
    q2.getHighest();
    // q2.getLowest();
    // q2.getEtc();
}

As a suggestion, in order to search the highest and lowest value, iterate your array and compare that value with an initialized one, if its bigger/smaller, update the value, at the end of the array, return the initialized value: 作为建议,为了搜索最高和最低值,请对数组进行迭代,并将该值与已初始化的值进行比较,如果其值更大或更小,请在数组末尾更新该值,并返回已初始化的值:

public int getHighest(){
   int max = -2147483648; // the lowest possible integer.
   for(int i=0 i<this.row;i++){
      if(matrix[i][1]>max){
         max = matrix[i][1];
      }
   }
   return max;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM