简体   繁体   English

有效分解Java代码?

[英]Efficiently Factor Java Code?

So I have this program that reads a Java textfile and prints out the numbers, which are electric bills. 因此,我有一个程序,该程序读取Java文本文件并打印出数字,即电费单。 Then it finds out the maximum and prints that out along with what month the max came from. 然后,找出最大值并打印出来以及最大值来自哪个月份。 My teacher looks for efficiency of code, and I was wondering if there was an easier or possible way to factor the months of the year, instead of using an if else statement. 我的老师在寻找代码的效率,我想知道是否有一种更简便或可行的方法来计算一年中的几个月,而不是使用if else语句。 I read about it and I'm pretty sure that Java has the months stored somewhere, but I'm not sure how to get to it. 我读了一下,我很确定Java存放了几个月的时间,但是我不确定如何使用它。 (I'm just beginning to learn java so please use basic terms/code) (我才刚刚开始学习Java,所以请使用基本术语/代码)

My code is: 我的代码是:

  if (count == 0)
     System.out.println("File had no numbers");
  else {
     String month="";
     if (count==1) month="January";
     else if (finalcount==2) month="February";
     else if (finalcount==3) month="March";
     else if (finalcount==4) month="April";
     else if (finalcount==5) month="May";
     else if (finalcount==6) month="June";
     else if (finalcount==7) month="July";
     else if (finalcount==8) month="August";
     else if (finalcount==9) month="September";
     else if (finalcount==10) month="October";
     else if (finalcount==11) month="November";
     else if (finalcount==12) month="December";
     System.out.println("Largest Bill: "+max+ " (" +month+")"); 
     System.out.println("Total Yearly Sum: $"+((int)sum*100)/100.0);    
  }

Thanks! 谢谢!

The simplest way would be to use an array storing the months, create an array like so: 最简单的方法是使用存储月份的数组,像这样创建一个数组:

String months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

Once you have done that, the code you have shown us can be changed to the following: 完成此操作后,可以将您显示给我们的代码更改为以下代码:

if (count > 0) {
    month = months[count-1];
    System.out.println("Largest Bill: "+ max + " (" + month + ")"); 
    System.out.println("Total Yearly Sum: $" + ((int)sum*100)/100.0); 
}
else {
    System.out.println("File had no numbers");
}

As other has said, you can use the built in Calendar class, but it is not as simple as this for beginners. 就像其他人所说的,您可以使用内置的Calendar类,但是对于初学者来说,它并不那么简单。

I'm not sure why you changed your variable from count to finalcount. 我不确定为什么将变量从count更改为finalcount。 As far as another way to do the problem could be a switch statement (if you wanted to execute it in a similar manner), an array, or using Java's Calendar. 解决该问题的另一种方法可能是switch语句(如果您想以类似方式执行它),数组或使用Java的Calendar。 An easy way is this: 一个简单的方法是这样的:

public static String getMonth(int count) { //using the same variable you used
    //I'm going to abbreviate for sake of finishing this faster
    String answer[] = {"File had no numbers","Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
    return answer[count];
}// all this will do what you did.

To Use this you would just call it like any other method and pass your 'count' variable. 要使用它,您只需像其他方法一样调用它并传递您的'count'变量即可。

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

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