2011-10-28 16 views
-1

"compareMethod"とwhileループに問題があります。だれかが私に手伝ってくれたらどうもありがとうと感謝しています。 EclipseをIdeとして使用しています。メソッドとwhileループでエラーが発生しました

私は3つの値を入力し、最小のものを印刷したいと考えています。ここで

は私のコードです:

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

ルチアン・グリゴアありがとう – Kiril

関連する問題