您能帮我提供这个代码吗?



任何建议都非常感谢-New​​bie2Java
我提供了很多建议-谢谢(缺少链接)

这是我的代码:

导入java.util.Scanner;

公共课程Question5WIP {

/**
 * @param args


public static void main(String[] args) {
    // TODO Auto-generated method stub
    // boolean correctInput = false; // assume we dont have correct input
    //  while (!correctInput){       // continue as long as not correct input

    int hourValue = 0;
    Scanner in;
    while (!(hourValue > 0 && hourValue <= 12)) {
        System. out.println("Please enter the hours (between 1 and 12): ");
        in = new Scanner(System.in); // User input hour value.

        hourValue = in.nextInt();

        if (!(hourValue > 0 && hourValue <= 12)) {
            System.out.println("Hour Value should be between 1 and 12, please try again "); // invalid hours.

        }
    }

    int minuteValue = 0;
    // Scanner in;
    while (!(minuteValue > 0 && hourValue <= 60)) {
        System. out.println("Please enter the minutes (between 1 and 60): ");
        in = new Scanner(System.in); // User input hour value.

        minuteValue = in.nextInt();

        if (!(minuteValue > 0 && minuteValue <= 60)) {
            System.out.println("Invalid entry, please try again: "); // invalid hours.
            return;
        }

最佳答案

试试这个它给出正确的答案

   import java.util.Scanner;

   public class Test {

public static void main(String[] args) {

    int hourValue = 0;
    Scanner in;
    while (!(hourValue > 0 && hourValue <= 12)) {
        System. out.println("Please enter the hours (between 1 and 12): ");
        in = new Scanner(System.in); // User input hour value.

        hourValue = in.nextInt();

        if (!(hourValue > 0 && hourValue <= 12)) {
            System.out.println("Hour Value should be between 1 and 12,
                        please try again "); // invalid hours.

        }
    }

    int minuteValue;
    System.out.println("Please enter the minutes (between 1 and 60): ");
        // The program prompts for the hour.
    in = new Scanner(System.in); // User input hour value.

    minuteValue = in.nextInt();

    if (!(minuteValue >= 0 && minuteValue <= 60)) {
        System.out.println("Minute Value should be between 0 and 60,
                     please  try again "); // invalid minutes.
        return;

    }
}


}

07-26 09:29
查看更多