gpt4 book ai didi

java - 数组计数器不适用于java

转载 作者:行者123 更新时间:2023-12-01 21:24:19 25 4
gpt4 key购买 nike

我正在制作一个使用 JOptionPane 大约几天的 Java 程序。我不是在谈论日期(10/14/2016 等),而是天数:周日、周一 等。

我的问题是在当天添加特定天数的函数(例如今天是星期二,然后添加 5 天)。

我使用数组来访问/引用特定日期以进行显示和输出。

我引用了周日到周六,数组索引分别为 0 到 6。

问题是,假设当前是星期六,如果用户添加 3 天,程序就会崩溃。

我相信这会崩溃,因为由于周六位于索引 6,并且算法尝试访问不存在的第 9 个索引。由于 Java 是一种安全运行的语言,因此它不会显示“null”,而是决定崩溃。

在这种情况下应该发生的是,从星期六开始,程序将显示星期二,而不是:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9

我使用 Netbeans 作为我的 IDE,并且我正在自学 Java,因为我的大学在我当前的年级和类(class)中教我 C++、Visual Basic、C#。如果您想要详细信息,那么,

这是主类的源代码:

package weekdaysprogram;

import java.util.*;

public class weekdayParametersProgramMain
{

static Scanner console = new Scanner (System.in);

public static void main(String[] args)
{
weekdayParametersProgram firstObject = new weekdayParametersProgram();
firstObject.inputMainMenu();
}
}

这是第二个类的源代码:

package weekdaysprogram;

import javax.swing.JOptionPane;

public class weekdayParametersProgram
{
int currentDay; //variable used for referencing a certain day

String[] day = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //Array of days

public void inputMainMenu() //Main menu function
{
int choice;

String inputStr;
String mainMenuStr = "Day Class\n"
+ "Enter choice: \n"
+ "1. Set Day \n"
+ "2. Print Day \n"
+ "3. Print Next Day \n"
+ "4. Print Previous Day \n"
+ "5. Calculate Day \n"
+ "6. Exit";

inputStr = JOptionPane.showInputDialog(mainMenuStr);
choice = Integer.parseInt(inputStr);

switch (choice)
{
case 1:
setDay();
break;
case 2:
printDay();
break;
case 3:
printNextDay();
break;
case 4:
printPreviousDay();
break;
case 5:
calculateDay();
break;
case 6:
exit();
break;
default:
JOptionPane.showMessageDialog(null, "Error Try Again", "Error", JOptionPane.ERROR_MESSAGE);
inputMainMenu();
break;
}
}


public void calculateDay() //the 5th function I'm getting an error at aka the culprit of my error
{
String message = "Current Day: " + currentDay + " - " + day[currentDay] + "\n"
+ "Please enter the amount of days to be added: ";

int tempNum1 = currentDay;
String tempNum2 = JOptionPane.showInputDialog(message);
int tempNum3 = Integer.parseInt(tempNum2);

for (int count=0; count <= tempNum3; count++)
{
if (count == 7) tempNum1 = 0;
else if (count==0) continue; //this is a patch, since there would be a confusion if the user inputted 1 for the addtional day
else tempNum1++;
}

String nextMessage = day[currentDay] + " + " + tempNum3 + " days = " + day[tempNum1];

JOptionPane.showMessageDialog(null, nextMessage, "New Day", JOptionPane.INFORMATION_MESSAGE);

inputMainMenu();
}

public void setDay() //1st function for set day
{

String inputStr;

String message = "Enter day index: \n"
+ "0 = " + day[0] + "\n"
+ "1 = " + day[1] + "\n"
+ "2 = " + day[2] + "\n"
+ "3 = " + day[3] + "\n"
+ "4 = " + day[4] + "\n"
+ "5 = " + day[5] + "\n"
+ "6 = " + day[6] + "\n";
inputStr = JOptionPane.showInputDialog(message);

switch (inputStr)
{
case "0":
currentDay = 0;
break;
case "1":
currentDay = 1;
break;
case "2":
currentDay = 2;
break;
case "3":
currentDay = 3;
break;
case "4":
currentDay = 4;
break;
case "5":
currentDay = 5;
break;
case "6":
currentDay = 6;
break;
default:
JOptionPane.showMessageDialog(null, "Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
setDay();
break;
}

inputMainMenu();
}

public void printDay() //2nd function for printing out the current day
{
JOptionPane.showMessageDialog(null, day[currentDay], "Current Day", JOptionPane.INFORMATION_MESSAGE);

inputMainMenu();
}

public void printNextDay() //3rd function for printing out the next day
{
int tempNum = currentDay;

if (tempNum == 6) tempNum = 0;
else tempNum += 1;

JOptionPane.showMessageDialog(null, day[tempNum], "Next Day", JOptionPane.INFORMATION_MESSAGE);

inputMainMenu();
}

public void printPreviousDay() //4th function for printing out the previous day
{
int tempNum = currentDay;

if (tempNum == 0) tempNum = 6;
else tempNum -= 1;

JOptionPane.showMessageDialog(null, day[tempNum], "Previous Day", JOptionPane.INFORMATION_MESSAGE);

inputMainMenu();
}

public void exit()
{
System.exit(0);
}
}

最佳答案

而不是

    for (int count=0; count <= tempNum3; count++)
{
if (count == 7) tempNum1 = 0;
else if (count==0) continue; //this is a patch, since there would be a confusion if the user inputted 1 for the addtional day
else tempNum1++;
}

你可以使用

tempNum1 = (currentDay + tempNum3) % 7;

% 是模运算符,因此结果始终在 0 到 6 之间。

关于java - 数组计数器不适用于java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com