Java布尔值,用于测试唯一的整数

时间:2015-04-10 15:43:57

标签: java boolean try-catch

我正在尝试编写一个简单的java代码,要求用户输入三个UNIQUE整数,然后系统将检查它们是否实际上是三个不同的整数,如果两个是相同的,那么程序将启动方法再一次。我让用户输入整数,但boolean不检查唯一的整数,而只是结束程序。相关区域是public static boolean

我得到的错误是第66行:

  

布尔值语法错误,删除此标记

,在第68行:

  

getNumbers无法解析为变量

import java.util.Scanner;                     //PRE-WRITTEN CODE FROM SCANNER CLASS

public class NumbersPetrantoni {

    public static int num1;
    public static int num2;
    public static int num3;
    public static boolean verifyDiff;

    public static void main(String args[]) {     // MAIN METHOD USED TO CALL OTHER METHODS
        getNumbers();                               // USER INPUTS THREE UNIQUE NUMBERS TO BE CALCULATED
        boolean verifyDiff;                         // COMPUTER VERIFIES THAT THREE UNIQUE INTEGERS EXIST
    }             // END OF MAIN METHOD

    public static void getNumbers() {
        boolean done = false;
        Scanner input = new Scanner(System.in);
        while (done != true) {
            try {
                System.out.print("Let's do some basic math!");
                System.out.print("\nPlease enter three unique integers: ");
                System.out.print("\nNumber 1: ");
                num1 = input.nextInt();                  // METHOD USED TO INPUT FIRST INTEGER
                System.out.print("Number 2: ");
                num2 = input.nextInt();                  // "" SECOND INTEGER
                System.out.print("Last but not least, Number 3: ");
                num3 = input.nextInt();                  // "" THIRD INTEGER 
                done = true;
            } catch(Exception message) {
                input.nextLine();
                System.out.println("\nDo you know what an integerer is?"); 
                System.out.println("The program threw a " + message + " message at me, NOT a real integer.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex){};         
                System.out.println("\nREBOOTING...\n");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex){};
            }                // END - CATCH SECTION 
        }                    // END - WHILE LOOP        
    }       // END - GET NUMBERS METHOD

    public static boolean verifyDiff(int num1, int num2, int num3) {
        boolean allDiff = false;
        if(num1 != num2 && num1 != num3 && num2 != num3)                           
            allDiff = true;                  
        return allDiff;
        if(boolean allDiff = false)
            System.out.println("Thee UNIQUE integers, please.");
        getNumbers;
    }                                     // END - VERIFY DIFF METHOD
}       // END - NumbersPetrantoni CLASS

2 个答案:

答案 0 :(得分:1)

而不是:

boolean verifyDiff;   // COMPUTER VERIFIES THAT THREE UNIQUE INTEGERS EXIST

你想要:

verifyDiff(num1, num2, num3);

main方法中。

答案 1 :(得分:1)

您永远不会验证您的值不相等,您只需创建一个名为verifyDiff的变量。这种递归很容易避免。使用:

do{
    getNumbers();
}while(verifyDiff(num1 , num2 , num3));

并从getNumbers()的末尾删除verifyDiff()。顺便说一下。你的递归无论如何都不会起作用,因为你只从getNumbers()调用verifyDiff(),而不是从另一个方向调用{{1}}。你应该总是检查你是否使用变量或方法!!!