2011-10-28 23 views
-1

我有「compareMethod」和while循環的問題,所以如果任何人有任何idead如何幫助我,我將不勝感激,謝謝。我將Eclipse用作Ide。java中的方法和while循環的錯誤

我想輸入三個值然後打印最小的一個。

這裏是我的代碼:

import java.util.Scanner; 

public class CompareValues 
{ 
public static void main(String[] args) 
{ 
    System.out.println(); 
    System.out.println("The smallest number is: "); 
    int first; 
    int second; 
    int third; 
    checkMethod(first, second, third); 
} 

static int checkMethod(int firstNumber, int secondNumber, int thirdNumber) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter three nubmers between 1 - 100: "); 
    firstNumber = input.nextInt(); 
    secondNumber = input.nextInt(); 
    thirdNumber = input.nextInt(); 

    if ((0 < firstNumber) || secondNumber || (thirdNumber > 100)) 
    { 
     System.out.println("Invalid entry: enter numbers between 1 and 100 only: "); 
    } 
} 

static int compareMethod(int first, int second, int third) 
{ 
    if ((first < second) && (first < third)) 
    { 
     return first; 
    } 
    else if ((second < first) && (second < third)) 
    { 
     return second; 
    } 
    else 
    { 
     return third; 
    } 
} 
} 

當我整理我得到這個消息的代碼:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable first may not have been initialized The local variable second may not have been initialized The local variable third may not have been initialized at CompareValues.main(CompareValues.java:11)

+2

是什麼問題?請你也可以正確縮進代碼。 – kgautron

+1

你有什麼問題?你能否詳細說明一下? –

+1

哪個'while'循環? –

回答

1

看到在代碼中的註釋:

import java.util.Scanner; 
public class CompareValues { 
public static void main (String[] args) { 
    System.out.println(); 
    //print smallest number 
    System.out.println("The smallest number is: " + Integer.toString(checkMethod(first, second, third))); 
} 
static int checkMethod(int firstNumber, int secondNumber, int thirdNumber) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter three nubmers between 1 - 100: "); 
    firstNumber = input.nextInt(); 
    secondNumber = input.nextInt(); 
    thirdNumber = input.nextInt(); 

    //Correct validation of numbers 
    if (0 < firstNumber || firstNumber > 100 || 
     0 < secondNumber || secondNumber > 100 || 
     0 < thirdNumber || thirdNumber > 100) 
    { 
     System.out.println("Invalid entry: enter numbers between 1 and 100 only: "); 
     System.exit(0); 
    }  
    //return the smallest number here: 
    return compareMethod(firstNumber,secondNumber,thirdNumber); 
} 
static int compareMethod(int first, int second, int third) { 
    if (first < second && first < third) 
    { 
     return first; 
    } 
    else if (second < first && second < third) 
    { 
     return second; 
    } 
    else 
    { 
     return third; 
    } 
} 
} 
+0

謝謝Luchian Grigore – Kiril