java - 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
}
}
}
解决方案
而不是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
}
}
}
}
推荐阅读
- flutter - Navigator.pop() - 如何传递 `context` 以供导航器读取 -
- vb.net - BFS 逻辑连接 6 点
- php - 从测试站点返回的 JSON 字符串包含阻止 json_decode 工作的额外字符
- uwp - 如何在 UWP 中的两个 InkCavas 对象上连续绘制墨水?
- datetime - 为什么 Google Bigquery 日期时间格式带有“T”分隔符?我想要一个空格分隔符
- docker - 通过别名获取 docker 容器 IP
- ios - 我可以找到从苹果商店下载我的应用程序的设备列表吗
- flutter - 如何在 Flutter 中每行放置 3 个项目
- python - 烧瓶错误:“NoneType”对象没有“永久”属性
- apache-flink - 使用流文件接收器标记 s3 对象