Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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 - Fatal编程技术网

Java 到达最后一个索引时重置数组索引

Java 到达最后一个索引时重置数组索引,java,Java,我试图构建一个代码,其中日历的12个月在一个数组中,然后代码要求一个月,然后它在输入的月份之前输出10个月。(例如,输入是1月,输出是2月到11月。)一旦指数超过12,我就很难将指数恢复到起点,例如,如果我输入8月,它应该输出9月到6月,但它在12月停止,并表示超出范围。多谢各位 String months[]; String choice; months = new String[13]; months[0] = null ; months[1] = "January

我试图构建一个代码,其中日历的12个月在一个数组中,然后代码要求一个月,然后它在输入的月份之前输出10个月。(例如,输入是1月,输出是2月到11月。)一旦指数超过12,我就很难将指数恢复到起点,例如,如果我输入8月,它应该输出9月到6月,但它在12月停止,并表示超出范围。多谢各位

String months[];
   String choice;
    months = new String[13];
   months[0] = null ;
   months[1] = "January";
   months[2] = "February";
   months[3] = "March";
   months[4] = "April";
   months[5] = "May";
   months[6] = "June";
   months[7] = "July";
   months[8] = "August";
   months[9] = "September";
   months[10] = "October";
   months[11] = "November";
   months[12] = "December";
   System.out.print("Enter Month : ");
   choice= a.nextLine();

   if (choice.equals("August"))    {
       for(int i=8; i<i+10; i++) {
           String result= months[i];
           System.out.println(result);
       }
   }
字符串月份[];
字符串选择;
月份=新字符串[13];
月[0]=空;
月[1]=“一月”;
月[2]=“2月”;
月[3]=“三月”;
月[4]=“4月”;
月[5]=“五月”;
月[6]=“6月”;
月[7]=“7月”;
月[8]=“8月”;
月[9]=“9月”;
月[10]=“10月”;
月[11]=“11月”;
月[12]=“12月”;
系统输出打印(“输入月份:”;
choice=a.nextLine();
如果(选择等于(“八月”)){

对于(int i=8;i在选择打印月份时使用模数:

int start = 8;
if ("August".equals(choice)) {
    for(int i=start; i < start+10; i++) {
        String result= months[i % 12];
        System.out.println(result);
    }
}
这里的想法是环绕用于从数组中选择月份的索引。伪变量
i
在达到值12时,将再次环绕为零


旁注:最好将字符串文字与变量进行比较,方法是将文字放在比较的LHS上。我使用的版本不受空指针异常的影响;您使用的版本不受空指针异常的影响。

使用模数重置数组索引。上面给出的解决方案是正确的。但是如果您想继续使用自己定义的
months
数组,其中
months[0]=null;
,只需从输入月份的下一个索引开始for循环

if ("August".equals(choice)) {
    int start = 9;
    for(int i=start; i < start+11; i++) {
        if(i!=13){
            String result= months[i % 13];
            System.out.println(result);
        }
    }
}
if(“八月”。等于(选择)){
int start=9;
对于(int i=start;i
试试这个

     public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String months[];
        String choice;
        months = new String[13];
        months[0] = null;
        months[1] = "January";
        months[2] = "February";
        months[3] = "March";
        months[4] = "April";
        months[5] = "May";
        months[6] = "June";
        months[7] = "July";
        months[8] = "August";
        months[9] = "September";
        months[10] = "October";
        months[11] = "November";
        months[12] = "December";
        System.out.print("Enter Month : ");
        choice = scanner.nextLine();

        int startIndex = 0;
        for (int i = 0; i < months.length; i++) {
            if (choice.equalsIgnoreCase(months[i])) {
                startIndex = i + 1;
            }
        }

        for (int j = startIndex; j < months.length; j++) {
            String result = months[j];
            System.out.println(result);
        }
    }
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
字符串月份[];
字符串选择;
月份=新字符串[13];
月[0]=空;
月[1]=“一月”;
月[2]=“2月”;
月[3]=“三月”;
月[4]=“4月”;
月[5]=“五月”;
月[6]=“6月”;
月[7]=“7月”;
月[8]=“8月”;
月[9]=“9月”;
月[10]=“10月”;
月[11]=“11月”;
月[12]=“12月”;
系统输出打印(“输入月份:”;
choice=scanner.nextLine();
int startIndex=0;
对于(int i=0;i
     public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String months[];
        String choice;
        months = new String[13];
        months[0] = null;
        months[1] = "January";
        months[2] = "February";
        months[3] = "March";
        months[4] = "April";
        months[5] = "May";
        months[6] = "June";
        months[7] = "July";
        months[8] = "August";
        months[9] = "September";
        months[10] = "October";
        months[11] = "November";
        months[12] = "December";
        System.out.print("Enter Month : ");
        choice = scanner.nextLine();

        int startIndex = 0;
        for (int i = 0; i < months.length; i++) {
            if (choice.equalsIgnoreCase(months[i])) {
                startIndex = i + 1;
            }
        }

        for (int j = startIndex; j < months.length; j++) {
            String result = months[j];
            System.out.println(result);
        }
    }