首页 > 解决方案 > JAVA中的FOR循环数组,不起作用

问题描述

有人可以指出我的程序有什么问题吗?

我已经完成了大部分工作,但我似乎找不到它有什么问题。

它不会要求用户为每门课程提供“输入您的成绩”提示。

这是用于学校的数组分配。这是我的代码。

我很难弄清楚for我专门制作的for循环有什么问题。

该程序应该向用户询问他们的课程,然后用户输入他们对该课程的成绩。

如果可能的话,请给我提示我做错了什么。

import java.io.*;


public class StudentMarks {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException{
    // TODO code application logic here
    //Declare BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    //declare variables
    int x=0, y=0;
    double grade=0.0;
    String course;

    //ask user how many courses they completed
    System.out.println("How many courses have you completed?");
    //obtain answer
    int completed=Integer.parseInt(br.readLine());

    //declare array for course
    String courses[]=new String[completed];

    //ask user to enter the course names use a FOR loop for this
    for(int i=0;i<courses.length;i++)
    {
        i++;            
        System.out.println("Please enter course name " + i);
        course = br.readLine();

        for(int j=i--;j<i;j++)
        { 
            j++;
        System.out.println("What is the grade you got for " + course+ " " + j);
        //get their answer
        grade = Double.parseDouble(br.readLine());

        }
    }//end for loop

    //display to the user the high achievement award qualifiers:

    System.out.println("High-Ahcievement Award Qualifiers: \n");
    if(grade>93)
    {
        //output
    }
    else if(grade<70)
    {
        System.out.println("Needs Improvement:");
        //output
    }
  }
}

标签: javaarrays

解决方案


而不是i++使用

System.out.println("Please enter course name " + (i+1));

你不需要任何嵌套循环

for(int j=i--;j<i;j++)
        { 
            j++;
        System.out.println("What is the grade you got for " + course+ " " + j);
        //get their answer
        grade = Double.parseDouble(br.readLine());

        }

改为使用

 System.out.println("What is the grade you got for " + course);
        //get their answer
        grade = Double.parseDouble(br.readLine());

这是完整的代码,如果您仍然无法理解它,请告诉我。

 import java.io.*;

public class sort {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException{
    // TODO code application logic here
    //Declare BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    //declare variables
    int x=0, y=0;
    double grade=0.0;
    String course;

    //ask user how many courses they completed
    System.out.println("How many courses have you completed?");
    //obtain answer
    int completed=Integer.parseInt(br.readLine());

    //declare array for course
    String courses[]=new String[completed];

    //ask user to enter the course names use a FOR loop for this
    for(int i=0;i<courses.length;i++)
    {            
        System.out.println("Please enter course name " + (i+1));
        course = br.readLine(); 
        System.out.println("What is the grade you got for " + course);
        //get their answer
        grade = Double.parseDouble(br.readLine());

    //end for loop

    //display to the user the high achievement award qualifiers:

    System.out.println("High-Ahcievement Award Qualifiers: \n");
    if(grade>93)
    {
        //output
    }
    else if(grade<70)
    {
        System.out.println("Needs Improvement:");
        //output
    }
    }
}
}

推荐阅读