在if语句中返回循环

时间:2013-11-13 08:30:42

标签: java

你能帮我解释一下这段代码吗?


非常感谢任何建议 - Newbie2Java 我有一些很棒的意见 - 谢谢(错过链接)

这是我的代码:

import 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 个答案:

答案 0 :(得分: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;
        }   
    }
}

答案 1 :(得分: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;

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

答案 2 :(得分: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;

    }
}

}

答案 3 :(得分:0)

import java.util.Scanner;

公共课测试{

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;