因此,我想要做的基本概念是循环这些用户定义日期的方法(年,月,日)。每个日期都将转换为一个字符串,该字符串将存储在一个数组中。数组的大小由用户决定。当然,数组的大小和循环运行的次数将彼此相等。使用我的代码,循环停止并返回第一个日期的字符串。如何在不打印的情况下存储值,并允许循环继续?
public class Assign4 {
public static void main(String[] args) {
//Refer to Ledger Class
Ledger InfoLedger = new Ledger();
//Use Ledger Methods
InfoLedger.UserArraySize(args);
InfoLedger.LoopInvoice(args);
}
下一课,用户在此定义数组大小
public class Ledger {
//Refer to Invoice Class
Invoice Info = new Invoice();
//Ledger Amount
public static int LedgerAmount;
//Create Ledger Array
public static String[] LedgerArray = new String[LedgerAmount];
//Create new array
public void UserArraySize(String[] args ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the amount of monthly invoices: ");
LedgerAmount = keyboard.nextInt();
}
public void LoopInvoice(String[] args ) {
for (int i = 0; i < LedgerAmount; i++) {
Info.OurDateYear(args);
Info.OurDateMonth(args);
Info.OurDateDay(args);
LedgerArray[i] = Info.DateString(args);
}
}
}
这个类只调用OurDate,稍后会更多地使用它
public class Invoice {
//Refer to OurDate Class
OurDate Date = new OurDate();
//Todays Year
public void OurDateYear(String[] args ) {
Date.setYearFromUser(args);
}
//Todays Month
public void OurDateMonth(String[] args ) {
Date.setMonthFromUser(args);
}
//Todays Day
public void OurDateDay(String[] args ) {
Date.setDayFromUser(args);
}
//Display the dates
public String DateString(String[] args ) {
return Date.toString();
}
}
此类包含用户的方法和toString方法
public class OurDate {
//Date variables and user requests
//integers for the month day and year of both set of inputs are set to defaults
public static Integer TodaysMonth = 1;
public static Integer TodaysDay = 1;
public static Integer TodaysYear = 1990;
//TodaysYear
//Prompt for the user to enter Todays Year
public void setYearFromUser(String[] args ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Today's year (1900 to 2015): ");
TodaysYear = keyboard.nextInt();
//Error Checking to check that the Year entered is within the range of 1900 to 2015
try{
if(TodaysYear > 1899 && TodaysYear < 2016){
}else{
System.out.println("Invalid choice! Please enter a valid Year");
setYearFromUser(args);
}
}catch(Exception e){
System.out.println("Invalid choice! Please enter a valid Year");
setYearFromUser(args);
}
}
//TodaysMonth
//Prompt for the user to enter Todays Month
public void setMonthFromUser(String[] args ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Today's month(from 1 to 12): ");
TodaysMonth = keyboard.nextInt();
//Error Checking to check that the Month entered is within the range of 1 to 12
try{
if(TodaysMonth > 0 && TodaysMonth < 13){
}else{
System.out.println("Invalid choice! Please enter a valid Month");
setMonthFromUser(args);
}
}catch(Exception e){
System.out.println("Invalid choice! Please enter a valid Month");
setMonthFromUser(args);
}
}
//TodaysDay
//Prompt for the user to enter Todays Day
public void setDayFromUser(String[] args ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Today's day (1 to 30): ");
TodaysDay = keyboard.nextInt();
//Error Checking to check that the Day entered is within the range of 1 to 30
try{
if(TodaysDay > 0 && TodaysDay < 31){
}else{
System.out.println("Invalid choice! Please enter a valid Day");
setDayFromUser(args);
}
}catch(Exception e){
System.out.println("Invalid choice! Please enter a valid Day");
setDayFromUser(args);
}
}
//Todays Date ToString
public String ToString(String[] args ) {
return TodaysDay + "/" + TodaysMonth + "/" + TodaysYear;
}
}