如何在if循环内部询问特定条件的输入

huangapple go评论69阅读模式
英文:

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[]{&quot;MATHS&quot;,&quot;PHYSICS&quot;,&quot;ENGLISH&quot;};
static int avg;
static int total_marks=0;
static Scanner sc=new Scanner(System.in);
void check_mark()
{
for (int j = 0; j &lt; 3; j++) 
{
System.out.print(&quot;Enter &quot;+b[j]+&quot; mark: &quot;);
a[j]=sc.nextInt();
if(a[j]&gt;100)
{
System.out.println(&quot;Please provide correct mark which is equal to or below 100&quot;);
check_mark();
}
}
}
public static void main( String[] args )
{
for (int i = 0; i &lt; no_of_students; i++) 
{    
averageMark check=new averageMark();
check.check_mark();
for (int k = 0; k &lt; no_of_subjects; k++) 
{
total_marks+=a[k];
}
avg=total_marks/no_of_subjects;
System.out.println();
System.out.println(&quot;The average is: &quot;+avg);
if(avg&gt;=70)
{
System.out.println();
System.out.println(&quot;!!!Qualified!!!&quot;);   
}
else
{
System.out.println();
System.out.println(&quot;!!!Not qualified!!!&quot;);
}
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 &lt; 3; j++) {
System.out.print(&quot;Enter &quot;+b[j]+&quot; mark: &quot;);
a[j]=sc.nextInt();
if(a[j]&gt;100) {
System.out.println(&quot;Please provide correct mark which is equal to or below 100&quot;);
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 &lt; 3; j++) 
{
System.out.print(&quot;Enter &quot;+b[j]+&quot; mark: &quot;);
a[j]=sc.nextInt();
while(a[j]&gt;100){
System.out.println(&quot;Please provide correct mark which is equal to or below 100&quot;);
a[j] = sc.nextInt(); 
}
}
}

huangapple
  • 本文由 发表于 2020年9月2日 21:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定