如何循环以下代码?

时间:2017-11-09 19:38:00

标签: java loops if-statement

我是java的新手,我无法找到解决方法。我想创建一个程序,告诉你一个数字是正数还是负数,无论它是int还是double。但是在执行程序之后,我希望它循环并再次询问用户的输入,一次又一次地执行代码,只要有用户输入。我可以在java中做到吗?

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String userInput = "Input your number: ";
        System.out.println(userInput);

        if (in.hasNextInt()) {
            int z = in.nextInt();
            if (z > 0) {
                System.out.println(z + " is positive.");
            } else if (z < 0) {
                System.out.println(z + " is negative.");
            } else {
                System.out.println(z + " is equal to 0.");
            }
        } else if (in.hasNextDouble()) {
            double x = in.nextDouble();
            if (x > 0) {
                System.out.println(x + " is positive.");
            } else if (x < 0) {
                System.out.println(x + " is negative.");
            } else {
                System.out.println(x + " is equal to 0.");
            }
        } else {
            System.out.println("Hey! Only numbers!");
        }
    }
}

3 个答案:

答案 0 :(得分:1)

这是一种良好的开端,让您了解模式匹配在Java中可以做些什么,并且可以通过针对详尽的数据点进行测试来改进它。

这也展示了如何使用while循环,重载方法和三元运算符,而不是嵌套的if-then-else。

在学习时,您还应该使用编辑器的调试功能,并使用system.out.println来了解正在执行的代码。

当用户按下输入(空字符串)时,我结束程序。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String userInput = "Input your number: ";
            System.out.print(userInput);
            String input = scanner.nextLine();
            // look for integer (+ve, -ve or 0)
            if (input.matches("^-?[0-9]+$")) {
                int z = Integer.parseInt(input);
                System.out.println(display(z));
            // look for double (+ve, -ve or 0)
            } else if (input.matches("^-?([0-9]+\\.[0-9]+|[0-9]+)$")) {
                double z = Double.parseDouble(input);
                System.out.println(display(z));
            // look for end of program by user
            } else if (input.equals("")) {
                System.out.println("Good Bye!!");
                break;
            // look for bad input
            } else {
                System.out.println("Hey! Only numbers!");
            }
        }
        scanner.close();
    }

    // handle integer and display message appropriately
    private static String display(int d) {
        return (d>0) ? (d + " is positive") : (d<0) ? (d + " is negative") : (d + " is equal to 0");
    }

    // handle double and display message appropriately    
    private static String display(double d) {
        return (d>0) ? (d + " is positive") : (d<0) ? (d + " is negative") : (d + " is equal to 0");
    }
}

示例运行:

Input your number: 0
0 is equal to 0
Input your number: 0.0
0.0 is equal to 0
Input your number: -0
0 is equal to 0
Input your number: -0.0
-0.0 is equal to 0
Input your number: 12
12 is positive
Input your number: -12
-12 is negative
Input your number: 12.0
12.0 is positive
Input your number: -12.0
-12.0 is negative
Input your number: 12-12
Hey! Only numbers!
Input your number: ---12
Hey! Only numbers!
Input your number: 

答案 1 :(得分:0)

使用此代码!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Console console = new Console();
        while(true) {
            // Take your input
            Scanner in = new Scanner(System.in);
            String userInput = "Input your number: ";
            System.out.println(userInput);

            if (in.hasNextInt()) {
                int z = in.nextInt();
                if (z > 0) {
                    System.out.println(z + " is positive.");
                } else if (z < 0) {
                    System.out.println(z + " is negative.");
                } else {
                    System.out.println(z + " is equal to 0.");
                }
            } else if (in.hasNextDouble()) {
                double x = in.nextDouble();
                if (x > 0) {
                    System.out.println(x + " is positive.");
                } else if (x < 0) {
                    System.out.println(x + " is negative.");
                } else {
                    System.out.println(x + " is equal to 0.");
                }
            } else {
                System.out.println("Hey! Only numbers!");
            }
            // Ask for exit
            System.out.print("Want to quit? Y/N")
            String input = console.readLine();
            if("Y".equals(input))
            {
               break;
            }
        }
    }
}

答案 2 :(得分:-1)

您可以使用function getListingID(){ global $servername, $username, $password, $dbname; $conn = mysqli_connect($servername, $username, $password, $dbname); $result = $conn->query("SELECT MAX(position)+1 FROM listings"); $row = mysqli_fetch_row($result); return $row[0]; } 循环。像这样:

do while
相关问题