英文:
How to ask input for the particular condition inside of if loop
问题
在check_mark()方法中,如果我对物理课程(在第二个位置a[1])的分数进行错误标记,即分数大于100,它会进入if循环,并且必须要求重新输入相同的物理课程分数,但它会再次从a[0]即数学开始...... 对于英语也是一样,它会再次从数学开始。
*请帮我解决这个问题,让它能够要求重新输入错误输入的特定科目分数......*
**错误代码:**
import java.util.Scanner;
class averageMark
{
static int no_of_students=2;
static int a[]=new int[3];
static int no_of_subjects=a.length;
static String b[]=new String[]{"MATHS","PHYSICS","ENGLISH"};
static int avg;
static int total_marks=0;
static Scanner sc=new Scanner(System.in);
void check_mark()
{
for (int j = 0; j < 3; j++)
{
System.out.print("请输入"+b[j]+"的分数: ");
a[j]=sc.nextInt();
if(a[j]>100)
{
System.out.println("请提供不大于100的正确分数");
check_mark();
}
}
}
public static void main( String[] args )
{
for (int i = 0; i < no_of_students; i++)
{
averageMark check=new averageMark();
check.check_mark();
for (int k = 0; k < no_of_subjects; k++)
{
total_marks+=a[k];
}
avg=total_marks/no_of_subjects;
System.out.println();
System.out.println("平均分: "+avg);
if(avg>=70)
{
System.out.println();
System.out.println("!!!合格!!!");
}
else
{
System.out.println();
System.out.println("!!!不合格!!!");
}
System.out.println();
}
sc.close();
}
}
英文:
In check_mark() method if I put wrong mark for PHYSICS ( which is in 2nd position a[1] ) i.e mark > 100 it goes to the if loop and it has to ask input for the same PHYSICS mark but, it again starts from a[0] i.e MATHS...... Same as for ENGLISH it again starts from MATHS.
Please help me to solve this that it has to ask input for that particular mark which is wrongly entered....
Fault code:
import java.util.Scanner;
class averageMark
{
static int no_of_students=2;
static int a[]=new int[3];
static int no_of_subjects=a.length;
static String b[]=new String[]{"MATHS","PHYSICS","ENGLISH"};
static int avg;
static int total_marks=0;
static Scanner sc=new Scanner(System.in);
void check_mark()
{
for (int j = 0; j < 3; j++)
{
System.out.print("Enter "+b[j]+" mark: ");
a[j]=sc.nextInt();
if(a[j]>100)
{
System.out.println("Please provide correct mark which is equal to or below 100");
check_mark();
}
}
}
public static void main( String[] args )
{
for (int i = 0; i < no_of_students; i++)
{
averageMark check=new averageMark();
check.check_mark();
for (int k = 0; k < no_of_subjects; k++)
{
total_marks+=a[k];
}
avg=total_marks/no_of_subjects;
System.out.println();
System.out.println("The average is: "+avg);
if(avg>=70)
{
System.out.println();
System.out.println("!!!Qualified!!!");
}
else
{
System.out.println();
System.out.println("!!!Not qualified!!!");
}
System.out.println();
}
sc.close();
}
}
答案1
得分: 1
如果标记不正确,您不应调用check_mark方法。您应该减少索引。
void check_mark() {
for (int j = 0; j < 3; j++) {
System.out.print("输入第" + b[j] + "个标记:");
a[j] = sc.nextInt();
if (a[j] > 100) {
System.out.println("请提供正确的标记,应小于或等于100");
j--;
}
}
}
英文:
You should not call check_mark method if mark was incorrect. You should to decrement index
void check_mark() {
for (int j = 0; j < 3; j++) {
System.out.print("Enter "+b[j]+" mark: ");
a[j]=sc.nextInt();
if(a[j]>100) {
System.out.println("Please provide correct mark which is equal to or below 100");
j--;
}
}
}
答案2
得分: 1
你也可以使用while()循环来实现;
void check_mark()
{
for (int j = 0; j < 3; j++)
{
System.out.print("输入" + b[j] + "分数:");
a[j] = sc.nextInt();
while (a[j] > 100) {
System.out.println("请提供正确的分数,分数应为100或以下");
a[j] = sc.nextInt();
}
}
}
英文:
You can also do it with while() loop;
void check_mark()
{
for (int j = 0; j < 3; j++)
{
System.out.print("Enter "+b[j]+" mark: ");
a[j]=sc.nextInt();
while(a[j]>100){
System.out.println("Please provide correct mark which is equal to or below 100");
a[j] = sc.nextInt();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论