我如何制作这个if语句?

时间:2014-11-04 13:35:40

标签: java if-statement int

为这个愚蠢的问题道歉,我目前正在努力学习java。我需要这个代码才能工作,除非为studentNumber输入“0”,否则它会重复,我不确定当我必须在之前声明int时,如何获得“请输入学号”部分。如果声明?我不确定我是否接近这个完全错误或什么,但我需要能够重复数据输入,除非输入“0”作为studentNumber。谢谢你的帮助!

class Main { 
    public static void main( String args[] ) {
        int studentNumber = BIO.getInt();

        if(studentNumber > 0) {
            System.out.print("#Please enter the student number : ");

            System.out.print("#Please enter the coursework mark : ");
            int courseWork = BIO.getInt();

            System.out.print("#Please enter the exam mark : ");
            int examMark = BIO.getInt();

            double average = (double)(courseWork + examMark) / 2;

            System.out.printf("sn = " + studentNumber
                    + " ex = " + examMark + " cw = " + courseWork 
                    + " mark = " + average);
            } else {
                System.out.print("#End of data");
            }
        }
    }
}

4 个答案:

答案 0 :(得分:3)

使用while()

while(studentNumber > 0){
  studentNumber = BIO.getInt();
   .........
   ........
}

另见

答案 1 :(得分:1)

使用while()代替if,以及以下更改:

System.out.print("#Please enter the student number : ");
int studentNumber = BIO.getInt();

while(studentNumber > 0) {
    System.out.print("#Please enter the coursework mark : ");
    int courseWork = BIO.getInt();

    System.out.print("#Please enter the exam mark : ");
    int examMark = BIO.getInt();

    double average = (double)(courseWork + examMark) / 2;
    System.out.printf("sn = " + studentNumber
            + " ex = " + examMark + " cw = " + courseWork 
            + " mark = " + average);

    System.out.print("#Please enter the student number : ");
    studentNumber = BIO.getInt();
} 

System.out.print("#End of data");

与其他答案相反,这将确保即使在第一次迭代中,您也会执行检查(并向用户提供学生编号)。

答案 2 :(得分:1)

使用Scanner获取用户的输入并处理输入值

import java.util.Scanner;

public class ConditionCheck {
    public static void main(String[] args) {
       Scanner BIO = new Scanner(System.in);

       System.out.print("#Please enter the student number : ");
       int studentNumber = BIO.nextInt();

       if(studentNumber > 0) {
        System.out.print("#Please enter the coursework mark : ");
        int courseWork = BIO.nextInt();

        System.out.print("#Please enter the exam mark : ");
        int examMark = BIO.nextInt();

        double average = (double)(courseWork + examMark) / 2;
        System.out.printf("sn = " + studentNumber
                + " ex = " + examMark + " cw = " + courseWork 
                + " mark = " + average);
        } else {
            System.out.print("#End of data");
        }
   }

}

答案 3 :(得分:0)

您应该使用while语句并执行以下操作:

class Main
    { 
        public static void main( String args[] )
        {
                int studentNumber = 1;
                While(studentNumber > 0)
                {
                studentNumber = BIO.getInt();

                System.out.print("#Please enter the student number : ");


                System.out.print("#Please enter the coursework mark : ");
                int courseWork = BIO.getInt();

                System.out.print("#Please enter the exam mark : ");
                int examMark = BIO.getInt();

                double average = (double)(courseWork + examMark) / 2;

                System.out.printf("sn = " + studentNumber + " ex = " + examMark + " cw = " + courseWork + " mark = " + average);
              }

                else
              {
                  System.out.print("#End of data");

              }
        }
    }