Java if else语句对你有帮助吗?

时间:2015-11-17 01:23:01

标签: java if-statement

我正在尝试输出它,以便在num1,num2,num3,num4或num5 = 999时打印并停止扫描仪输入继续,因为它不是我的代码。希望这是有道理的,你可以帮助它将非常感激。

package a5q4;
import java.util.Scanner;
/**
 *
 * @author Drew
 */
public class A5Q4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1;
        int num2;
        int num3;
        int num4;
        int num5;

        System.out.println("Enter different numbers between 1 and 100");
        num1 = input.nextInt();
        if (num1 == 999) {
            num2 = 0;
            num3 = 0;
            num4 = 0;
            num5 = 0; 
        }
        else {
            num2 = input.nextInt();
        }
        if (num2 == 999) {
            num3 = 0;
            num4 = 0;
            num5 = 0;
        }  
        else {
            num3 = input.nextInt();
        }
        if (num3 == 999) {
            num4 = 0;
            num5 = 0;   
        }
        else {
            num4 = input.nextInt();
        }
        if (num4 == 999) {
            num5 = 0;   
        }
        else {
            num5 = input.nextInt();
        }

1 个答案:

答案 0 :(得分:2)

首先,您可以在一行上声明初始化多个int(s)。如果我理解了您的问题,您希望测试给定的num值是否为999 ,如果它不是,则读取{{1 }}。您需要嵌套nextInt(s)以使用单个if值进行嵌套。像,

num

没有int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0; System.out.println("Enter different numbers between 1 and 100"); num1 = input.nextInt(); if (num1 != 999) { num2 = input.nextInt(); if (num2 != 999) { num3 = input.nextInt(); if (num3 != 999) { num4 = input.nextInt(); if (num4 != 999) { num5 = input.nextInt(); } } } } 否定(boolean),它必须是嵌套的!(s),

else