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

Java 用于打印阵列的循环出现问题

Java 用于打印阵列的循环出现问题,java,arrays,loops,Java,Arrays,Loops,有人能帮我找出为什么我的for循环无法打印出正确的答案吗。 它就像跳过第一个数组编号[0]。但是,如果我试图让它打印出我的数组nr[1],它工作得很好。 我的柜台顶部的ans应答器肯定有问题 package assignment9.pkg1; import java.util.Scanner; /** * * @author Anders */ public class Assignment91 { /** * @param args the command lin

有人能帮我找出为什么我的for循环无法打印出正确的答案吗。 它就像跳过第一个数组编号[0]。但是,如果我试图让它打印出我的数组nr[1],它工作得很好。 我的柜台顶部的ans
应答器肯定有问题

package assignment9.pkg1;

import java.util.Scanner;

/**
 *
 * @author Anders
 */
public class Assignment91 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String studName = "Anders";
        int counter = 1;// i think the problem is here
        int answer = 1; // same
        System.out.println(" welcome to student database, show informations about student" + studName);

        Scanner courseScan = new Scanner(System.in);
        Scanner gradeScan = new Scanner(System.in);
        Scanner answerScan = new Scanner(System.in);

        System.out.println(" Enter the name of courses");

        String[] courseArray = new String[counter];
        int[] gradeArray = new int[counter];

        for (int k = 0; k <= counter; k++) {

            if (counter < 20) {

                while (answer != 0) {  // what have i done here with that 0 answer??

                    System.out.println(" enter name");
                    courseArray[k] = courseScan.nextLine();
                    System.out.println(" Enter grade");
                    gradeArray[k] = gradeScan.nextInt();
                    System.out.println(" Do you want to add one more course enter 1, if not enter 0");
                    answer = answerScan.nextInt();

                }
            } else {

                System.out.println("Sorry, there is no more memory");

            }
        }

        int n = gradeArray.length;
        int temp = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - i); j++) {

                if (gradeArray[j - 1] > gradeArray[j]) {
                    //swap the elements!
                    temp = gradeArray[j - 1];
                    gradeArray[j - 1] = gradeArray[j];
                    gradeArray[j] = temp;

                    // Swap the course array
                    String gradeArrayTemp;

                    gradeArrayTemp = courseArray[j - 1];
                    courseArray[j - 1] = courseArray[j];
                    courseArray[j] = gradeArrayTemp;

                }

            }

        }

        for (int l = 0; l < courseArray.length; l++) {

            System.out.println("grade " + gradeArray[l] + "name " + courseArray[l]); // why does it not print all the array out
        }

        Scanner request = new Scanner(System.in);

        System.out.println(" what do you want to do. Enter 1 to rename a course");
        System.out.println(" enter 2 to change a grade ");
        int regNumber = request.nextInt();
        switch (regNumber) {

            case 1: // rename a course

                Scanner search = new Scanner(System.in);
                System.out.println("Enter the name of the course you want to rename");
                String searchCourse = search.nextLine();

                for (int i = 0; i < courseArray.length; i++) {
                    if (searchCourse.equals(courseArray[i])) {

                        System.out.println("Yes there is a course named " + courseArray[i]);
                        System.out.println(" to change coursename insert new name");
                        // here i change the coursename
                        Scanner newName = new Scanner(System.in);
                        courseArray[i] = newName.nextLine();

                    } else {

                        System.out.println(" no record of this course");

                    }

                    System.out.println(" you have chosen to rename course into " + courseArray[i]);

                }


        }
    }
}
package assignment9.pkg1;
导入java.util.Scanner;
/**
*
*@作者安德斯
*/
公共课堂作业91{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
字符串studName=“Anders”;
int counter=1;//我想问题在这里
int answer=1;//相同
System.out.println(“欢迎访问学生数据库,显示学生信息”+studName);
扫描器路径扫描=新扫描器(System.in);
扫描仪等级扫描=新扫描仪(System.in);
扫描仪应答扫描=新扫描仪(System.in);
System.out.println(“输入课程名称”);
字符串[]courserray=新字符串[计数器];
int[]gradeArray=新的int[计数器];
对于(int k=0;k数组[j]){
//交换元素!
温度=梯度阵列[j-1];
gradeArray[j-1]=gradeArray[j];
梯度阵列[j]=温度;
//交换课程数组
字符串gradeArrayTemp;
gradeArrayTemp=Coursarray[j-1];
courserray[j-1]=courserray[j];
courseArray[j]=成绩表;
}
}
}
对于(int l=0;l
在本节代码中:

 for (int k = 0; k <= counter; k++) {

            if (counter < 20) {

                while (answer != 0) {  // what have i done here with that 0 answer??

                    System.out.println(" enter name");
                    courseArray[k] = courseScan.nextLine();
                    System.out.println(" Enter grade");
                    gradeArray[k] = gradeScan.nextInt();
                    System.out.println(" Do you want to add one more course enter 1, if not enter 0");
                    answer = answerScan.nextInt();

                }
            } else {

                System.out.println("Sorry, there is no more memory");

            }
        }


你的预期产出是什么?请澄清你的问题。注意,有很多有效的方法可以做到这一点。请用谷歌搜索。
 int k = 0;

            if (counter < 20) {

                while (answer != 0) {  // what have i done here with that 0 answer??

                    System.out.println(" enter name");
                    courseArray[k] = courseScan.nextLine();
                    System.out.println(" Enter grade");
                    gradeArray[k] = gradeScan.nextInt();
                    System.out.println(" Do you want to add one more course enter 1, if not enter 0");
                    answer = answerScan.nextInt();
                    k++;
                }
            } else {

                System.out.println("Sorry, there is no more memory");

            }
for (int i = 0; i < courseArray.length; i++) {
            if(courseArray[i] != null)
            System.out.println("grade " + gradeArray[i] + "name " + courseArray[i]); // why does it not print all the array out
        }
 String[] courseArray = new String[20];
        int[] gradeArray = new int[20];