如何再次执行代码

时间:2014-07-03 16:33:58

标签: java

此代码是正确的,但如果输入的数字大于1520且小于3999,我想添加一条消息,例如“good”

import java.util.Scanner;
public class date
{
    public static void main (String args [])
    {
        int x;
        Scanner in = new Scanner (System.in);
        System.out.print("Enter a date ");
        x = in.nextInt();

        while (x < 1520 || x > 3999)
        {
            System.out.println ("Invalid Gregorian Calendar date.");
            System.out.print ("Please Input a valid Gregorian Calendar date: ");
            x = in.nextInt();
        }
    }   
}

1 个答案:

答案 0 :(得分:2)

您只需在循环后添加一个打印语句(即System.out.println("Good");)。

*注意:我将类名从date更改为Date,这是命名类的正确Java约定。当您使用此代码时(如果您复制并粘贴它),请将您的文件从date.java更改为Date.java,以防止编译问题。如果我没记错的话,类名和文件名必须匹配,区分大小写,如果我没记错的话。

import java.util.Scanner;

public class Date{
   public static void main (String args []){
      int x;

      Scanner in = new Scanner (System.in);
      System.out.print("Enter a date ");
      x = in.nextInt();


      while (x < 1520 || x > 3999)
      {
          System.out.println ("Invalid Gregorian Calendar date.");
          System.out.print ("Please Input a valid Gregorian Calendar date: ");
          x = in.nextInt();
      }
      System.out.println("Good");
  }
}