0

你能帮我这个代码吗?


非常感谢任何建议 - Newbie2Java 我有一些很好的意见 - 谢谢(缺少链接)

这是我的代码:

导入 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;
        }
4

4 回答 4

1
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int hourValue, minuteValue;
        Scanner in;

        while (true) {
            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 ");
            else
                break;
        }

        while(true) {
            System.out.println("Please enter the minutes (between 1 and 60): ");
            in = new Scanner(System.in);
            minuteValue = in.nextInt();
            if (minuteValue < 0 || minuteValue > 60)) 
                System.out.println("Minute Value should be between 0 and 60,please  try again ");//invalid minutes.
            else
                break;
        }   
    }
}
于 2013-11-13T09:04:41.723 回答
0

用一个while循环来做!像这样:

int hourValue; 
while(!(hourValue > 0 && hourValue <= 12)) {
    System.out.println("Please enter the hours (between 1 and 12): "); //The program prompts for the hour. 
    Scanner 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.
           return; 

    }
}

    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;

现在你只需要在几分钟内做同样的事情。

于 2013-11-13T08:36:06.250 回答
0

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

   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;

    }
}

}

于 2013-11-13T08:49:36.627 回答
0

导入 java.util.Scanner;

公共类测试{

公共静态无效主要(字符串[]参数){

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;
于 2013-11-14T21:06:29.297 回答