2017-04-17 7 views
0

宝くじの回答を格納するランダムに生成された配列とユーザーの入力を比較する宝くじゲームを作ろうとしています(100%答えは時々余分な答えを拾うでしょう。例えば、Total Counterが壊れていますか?)しかし、私の検証方法は間違っていますし、これを修正する方法は不明です。私の方法は、私がしたいと思ったものに従っていない理由は分かりません

import java.util.Random; 
import java.util.Scanner; 

public class TestingClassDemo { 
    public static void main(String[] args) { 

     int input; //Holds keyboard input 
     final int TOTAL_ANSWERS = 10; //Number of answers 
     int[] guesses = new int[TOTAL_ANSWERS]; //Array to hold answers 

     //Creates a Scanner object for keyboard input 
     Scanner userInput = new Scanner(System.in); 

     //Gets the users Input 
     System.out.println("Welcome to the Lottery!"); 

     for(int i = 0; i < guesses.length; i++) 
     { 
      System.out.print("Enter digit " + (i+1) + ": "); 
      input = userInput.nextInt(); 

      while(!validate(guesses[i])) 
      { 
       System.out.println("Invalid input. Please enter a digit between 1-9"); 
       System.out.print("Enter digit " + (i+1) + ": "); 
       input = userInput.nextInt(); 
      } 
     } 

     //Creates a Lottery Object 
     TestingClass myLottery = new TestingClass(guesses); 


     System.out.print("\nCorrect Numbers: " + (myLottery.totalCorrect())); 

    }//End of Main Method 
    public static boolean validate(int a) 
    { 
     boolean status; 

     if(a == 1 || a == 2 || a == 3 || a == 4 || a == 5 || a == 6 || a == 7 || a == 8 || a == 9) 
      status = true; 
     else 
      status = false; 
     return status; 

    } 
}//End of TestingClassDemo 

他のクラス

public class TestingClass { 

    private int[] lotteryNumbers = new int[10]; 

    for(int i = 0; i < lotteryNumbers.length; i++) 
    { 
     lotteryNumbers[i] = (int) (Math.random()*10); 
     System.out.print(lotteryNumbers[i]+ " "); 
    } 
    }//End of LOOP 

    //Creates a Private Array for lotteryNumbers the user Guesses 
    private int[] lotteryGuesses = new int[10]; 

    //Creates a Private int totalCorrect whether correct guesses are stored 
    private int totalCorrect = 0; 

    //Appends lotteryGuesses to lotteryNumbers for comparison 
    public TestingClass(int[] lotteryNumbers) 
    { 
     lotteryGuesses = lotteryNumbers; 
    } 

    /** Compares whether the uses LotteryGuess are correct 
    and if they are increases total Correct 
    */ 
    public int totalCorrect() 
    { 
     for(int x = 0; x < lotteryNumbers.length; x++) 
     { 
      if (lotteryGuesses[x] == lotteryNumbers[x]) 
      { 
       totalCorrect += 1; 
      } 
     } 
     return totalCorrect; 
    } 
}//End of Public Class TestingClass 
+0

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim

+0

あなたがプログラムを行うことを期待したものを知らずに質問に答えるのは難しい、と実際にどのようなこと代わりにしました。 –

答えて

0
input = userInput.nextInt(); 

while(!validate(guesses[i])) 
{ 
} 

あなたはここで何かが欠けています。あなたはあなたの入力を検証し、それが有効であれば推測配列にコピーするべきです。

input = userInput.nextInt(); 

while(!validate(input)) 
{ 
    ... 
} 
guesses[i] = input; 
+0

同じエラーがスローされます。私がbreakステートメントを追加すると、それはループを壊してiをインクリメントします。そして、私が推測[i] = inputを追加すると、検証基準を満たしていないときに無限ループを開始します。 – Dreeww

+0

私のために働きます:https://ideone.com/M3alpR – greyfairer

+0

何らかの理由でええ、それは動作していませんでしたが、あなたの仕事:) – Dreeww

関連する問題