如何使我的 if 语句再次运行,就像 if-else 语句运行一样?

目前,如果有人输入超出范围的日期,则会导致else语句运行,这一切都很好,但我希望if-else语句运行时,它会重新运行if语句。我真的不知道我应该做什么,所以任何帮助将不胜感激!


import java.util.Scanner;


public class Horoscope {

    public Horoscope() {

        Scanner sc = new Scanner(System.in);

        String output = "Please enter a valid date";

        int month;

        int day;


        System.out.println("What is your month of birth?");

        month = sc.nextInt();

        System.out.println("What is your day of birth?");

        day = sc.nextInt();


        if((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {

            System.out.println("You are an Aries");

        }

        else if((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {

            System.out.println("You are a Taurus");

        }

        else if((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {

            System.out.println("You are a Gemini");

        }

        else if((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {

            System.out.println("You are a Cancer");

        }

        else if((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {

            System.out.println("You are a Leo");

        }

        else if((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {

            System.out.println("You are a Virgo");

        }

        else if((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {

            System.out.println("You are a Libra");

        }

        else if((month == 10 && day >= 23 && day <= 30) || (month == 11 && day >= 1 && day <= 21)) {

            System.out.println("You are a Scorpio");

        }

        else if((month == 11 && day >= 22 && day <= 31) || (month == 12 && day >= 1 && day <= 21)) {

            System.out.println("You are a Sagittarius");

        }

        }

    }

}


守着一只汪
浏览 85回答 2
2回答

BIG阳

import java.util.Scanner;public class test {&nbsp; &nbsp; public test() {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; String output = "Please enter a valid date";&nbsp; &nbsp; &nbsp; &nbsp; int month;&nbsp; &nbsp; &nbsp; &nbsp; int day;&nbsp; &nbsp; &nbsp; &nbsp; int k = 0;// you can use do while loop. write some flag logic like I created k variable and it will be checked in while block&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("What is your month of birth?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("What is your day of birth?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are an Aries");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Taurus");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Gemini");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Cancer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Leo");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Virgo");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Libra");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 10 && day >= 23 && day <= 30) || (month == 11 && day >= 1 && day <= 21)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Scorpio");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 11 && day >= 22 && day <= 31) || (month == 12 && day >= 1 && day <= 21)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Sagittarius");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Capricorn");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are an Aquarius");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You are a Pisces");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(output);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while (k == 1);&nbsp; &nbsp; }}

炎炎设计

解决了!我创建了一个名为 k 的 int 并将其设置为 1,并在它等于 1 时运行 do,如果它产生 true if 语句,则返回 0 并结束代码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java