Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组计数器不存在';我不能使用java_Java_Arrays_Netbeans_Indexoutofboundsexception - Fatal编程技术网

数组计数器不存在';我不能使用java

数组计数器不存在';我不能使用java,java,arrays,netbeans,indexoutofboundsexception,Java,Arrays,Netbeans,Indexoutofboundsexception,我正在制作一个Java程序,它使用了JOptionPaneabout days。我说的不是日期(2016年10月14日等),而是日期:周日、周一等 我的问题是,该函数将特定天数添加到当前日期(例如,今天是星期二,然后添加5天) 我使用数组访问/引用特定的一天,以便显示和输出 我引用了星期天到星期六,数组索引分别为0到6 问题是,假设今天是星期六,如果用户在其中添加了3天,程序就会崩溃 我相信这会崩溃,因为星期六位于索引6,算法试图访问第9个索引,而第9个索引并不存在。因为Java是一种安全的语言

我正在制作一个Java程序,它使用了
JOptionPane
about days。我说的不是日期(2016年10月14日等),而是日期:
周日、周一等

我的问题是,该函数将特定天数添加到当前日期(例如,今天是星期二,然后添加5天)

我使用数组访问/引用特定的一天,以便显示和输出

我引用了星期天到星期六,数组索引分别为0到6

问题是,假设今天是星期六,如果用户在其中添加了3天,程序就会崩溃

我相信这会崩溃,因为星期六位于索引6,算法试图访问第9个索引,而第9个索引并不存在。因为Java是一种安全的语言,所以它不显示“null”,而是决定崩溃

在这种情况下应该发生的是,从周六开始,该节目将在周二播出,而不是:

线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:9

<>我用NETBeBes作为IDE,我是自学习java,因为我的大学教我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);
    }
}
套餐工作日计划;
导入javax.swing.JOptionPane;
公共类WeekdayParameters程序
{
int currentDay;//用于引用特定日期的变量
String[]day={“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”};//天数数组
public void inputMainMenu()//主菜单函数
{
智力选择;
字符串输入str;
字符串mainMenuStr=“日类\n”
+“输入选项:\n”
+“1.设置日期\n”
+“2.打印日期\n”
+“3.第二天打印\n”
+“4.打印前一天\n”
+“5.计算日期\n”
+“6.退出”;
inputStr=JOptionPane.showInputDialog(mainMenuStr);
choice=Integer.parseInt(inputStr);
开关(选择)
{
案例1:
设定日();
打破
案例2:
printDay();
打破
案例3:
printNextDay();
打破
案例4:
打印前一天();
打破
案例5:
calculateDay();
打破
案例6:
退出();
打破
违约:
showMessageDialog(null,“错误重试”,“错误”,JOptionPane.Error\u消息);
inputMainMenu();
打破
}
}
public void calculateDay()//第五个函数我在aka处出错,这是我的错误的罪魁祸首
{
String message=“当前日期:“+currentDay+”-“+Day[currentDay]+”\n”
+“请输入要添加的天数:”;
int tempNum1=当前日期;
字符串tempNum2=JOptionPane.showInputDialog(消息);
int tempNum3=Integer.parseInt(tempNum2);
对于(int count=0;count而不是

    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++;
    }
%
是模运算符,因此结果总是介于0和6之间。

而不是

    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++;
    }
%
是模运算符,因此结果将始终在0和6之间。

calculateDay()
方法中,某些输入的执行可能会失败

  • 如果用户输入“6”,
    tempNum3
    将是整数6
  • 在for循环中,
    count
    将从0开始循环,直到
    tempNum3
    ,即6
  • 对于从1到6的计数值,
    tempNum1
    将每次递增
  • tempNum1
    已使用
    currentDay
    (可能也是6)初始化,
    tempNum1
    的值将大于6,从而在生成字符串
    nextMessage
    时产生
    索引自动边界异常
编辑:Loris的回答还提供了一个解决方案(使用模运算符),我建议您每次更改
数组的索引时都这样做。(但要小心负值)

calculateDay()
方法中,某些输入的执行可能会失败

  • 如果用户输入“6”,
    tempNum3
    将是整数6
  • 在for循环中,
    count
    将从0开始循环,直到
    tempNum3
    ,即6
  • 对于从1到6的计数值,
    tempNum1
    将每次递增
  • tempNum1
    已使用
    currentDay
    (可能也是6)初始化,
    tempNum1
    的值将大于6,从而在生成字符串
    nextMessage
    时产生
    索引自动边界异常

编辑:Loris的回答也提供了一个解决方案(使用模运算符),我建议您每次更改
数组的索引时都这样做。(但要小心负值)

问题出在
calculateDay()
方法中

让我们逐行讨论您的代码

假设
currentDay=5
然后输入
calculateDay()
方法

现在仔细阅读代码中的注释

    String message = "Current Day: " + currentDay + " - " + day[currentDay] + "\n"
            + "Please enter the amount of days to be added: ";//here currentDay =6

    int tempNum1 = currentDay;//tempNum1=6
    String tempNum2 = JOptionPane.showInputDialog(message);//suppose input "3"
    int tempNum3 = Integer.parseInt(tempNum2);//tempNum3 = 3

    for (int count=0; count <= tempNum3; count++)
    {
        if (count == 7) tempNum1 = 0;
        else if (count==0) continue; 
        else tempNum1++;//for count value 1,2,3 tempNum1 will increase so it will be 7, 8 finally 9. So tempNum1=9
    }

    String nextMessage = day[currentDay] + " + " + tempNum3 + " days = " + day[tempNum1];//here tempNum1=9 So ArrayIndexOutOfBound occurs
String message=“当前日期:“+currentDay+”-“+Day[currentDay]+”\n”
+请输入要添加的天数:;//此处currentDay=6
int tempNum1=currentDay;//tempNum1=6
字符串tempNum2=JOptionPane.showInputDialog(消息);//假设输入为“3”
int tempNum3=Integer.parseInt(tempNum2);//tempNum3=3

对于(int count=0;count问题在于
calculateDay()
方法

让我们逐行讨论您的代码

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


        for (int count=0; count <= tempNum3; count++)
        {
            if (count == 7) tempNum1 = 0;
            else if (count==0) continue; 
            else {
                tempNum1++

                if(tempNum1=7) tempNum=0;
            }
        }