2016-10-01 81 views
0

你好我真的卡住了,它關於一個隨機產生一個四位數號碼並提示用戶輸入一個四位數字而不使用數組並遵循這個規則的彩票程序 -if用戶輸入的獎金數量與獎金的確切順序相同,獎金爲$ 10000 - 如果用戶輸入的數字與四位數字不同,獎勵金額爲$ 5000 - 如果用戶輸入的數字與三位數字的順序不同,獎勵爲$ 2000 - 如果用戶輸入中的任何1位或2位數與彩票匹配,獎勵爲$ 500沒有陣列與java的彩票程序

我爲3位數字編號,但我不知道如何執行四位數字沒有陣列。這就是我這樣做:

import java.util.Scanner; 

public class Programming { 

    public static void main(String[] args) { 
     // Generate a lottery 
     int lottery = (int) (Math.random() * 1000); 
     // Prompt the user to enter a guess 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter your lottery pick (three digits): "); 
     int guess = input.nextInt(); 

     // Get digits from lottery 
     int lotteryDigit1 = lottery/100; 
     int lotteryDigit2 = (lottery % 100)/10; 
     int lotteryDigit3 = lottery % 10; 

     // Get digits from guess 
     int guessDigit1 = guess/100; 
     int guessDigit2 = (guess % 100)/10; 
     int guessDigit3 = guess % 10; 

     System.out.println("The lottery number is " + lotteryDigit1 
       + lotteryDigit2 + lotteryDigit3); 

     // Check the guess 
     if (guess == lottery) { 
      System.out.println("Exact match: you win $10,000"); 
     } else if ((guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit3) 
       || (guessDigit1 == lotteryDigit2 
       && guessDigit1 == lotteryDigit3 && guessDigit3 == lotteryDigit1) 
       || (guessDigit1 == lotteryDigit3 
       && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2) 
       || (guessDigit1 == lotteryDigit3 
       && guessDigit2 == lotteryDigit2 && guessDigit3 == lotteryDigit1) 
       || (guessDigit1 == lotteryDigit1 
       && guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit2)) { 
      System.out.println("Match all digits: you win $5,000"); 
     } else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 
       || guessDigit1 == lotteryDigit3 || guessDigit2 == lotteryDigit1 
       || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3 
       || guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 
       || guessDigit3 == lotteryDigit3) { 
      System.out.println("Match one digit: you win $1,000"); 
     } else { 
      System.out.println("Sorry, no match"); 
     } 
    } 
} 

感謝你的幫助

+0

@trooper這似乎是有點多的人請前奏編程的問題 – jcolemang

回答

1

另一個編輯

對不起,我需要一些時間來回復你。過了幾天忙碌。按要求,這是完整的程序。請注意,這不完全是您的原始程序。我刪除了一些東西,並添加了一些內容。也許最重要的是,我改變了我以前給你的功能。我原本回答你的問題,試圖只給你四位數的匹配,但如果我寫出整個事情,那麼概括一點就更有意義了。因爲您的彩票獎勵是根據匹配數字的數量進行的,所以製作一個計算有多少數字匹配的函數是非常有意義的。這大大簡化了你的if-else代碼。

import java.util.Scanner; 

public class Programming { 

    /* 
    * get the number of matching digits between the guess and the 
    * answer, ignoring repeated matches 
    */ 
    public static int numberDigitsMatch(String guess, String answer) { 

     int numberMatch = 0; 
     int currentIndex = 0; 
     int matchingIndex; 
     while (currentIndex < guess.length()) { 

      // check if the current digit of the guess is in the answer 
      matchingIndex = answer.indexOf(guess.charAt(currentIndex)); 

      if (matchingIndex < 0) { 
       currentIndex++; 
      } 
      else { 

       currentIndex++; 
       numberMatch++; 

       // remove the no longer relevant character from the answer 
       answer = answer.substring(0, matchingIndex) + 
        answer.substring(matchingIndex + 1); 
      } 


     } 

     return numberMatch; 
    } 




    public static void main(String[] args) { 

     Scanner in = new Scanner(System.in); 

     // generate the winning number 
     int lotteryNumber = (int) (Math.random() * 10000); 
     String lotteryString = "" + lotteryNumber; 

     System.out.println("The lottery number is " + lotteryString); 

     // Prompt the user to enter a guess 
     System.out.print("Enter your lottery pick (four digits): "); 

     // get the user's guess 
     int guessNumber = in.nextInt(); 
     String guessString = "" + guessNumber; 

     // NOTE: these guessDigit numbers are not necessary. I am leaving 
     // them here so you can see how to pick out individual digits 
     int guessDigit1 = guessNumber % 10; 
     int guessDigit2 = (guessNumber/10) % 10; 
     int guessDigit3 = (guessNumber/100) % 10; 
     int guessDigit4 = (guessNumber/1000) % 10; 

     int lotteryDigit1 = lotteryNumber % 10; 
     int lotteryDigit2 = (lotteryNumber/10) % 10; 
     int lotteryDigit3 = (lotteryNumber/100) % 10; 
     int lotteryDigit4 = (lotteryNumber/1000) % 10; 

     System.out.println("The lottery number is " + lotteryString); 

     int numMatchingDigits = numberDigitsMatch(guessString, lotteryString); 

     if (guessNumber == lotteryNumber) { 
      System.out.println("Exact match: you win $10,000"); 
     } 
     else if (4 == numMatchingDigits) { 
      System.out.println("Match all digits: you win $5,000"); 
     } 
     else if (3 == numMatchingDigits) { 
      System.out.println("Match three digits: you win $2,000"); 
     } 
     else if (2 == numMatchingDigits) { 
      System.out.println("Match two digits: you win $500"); 
     } 
     else if (1 == numMatchingDigits) { 
      System.out.println("Match two digit: you win $500"); 
     } 
     else { 
      System.out.println("Sorry, no match"); 
     } 
    } 
} 

編輯

看起來像我一樣正確理解。我相信有多種方式可以得到一個數字的給定數字,但我認爲這是最容易想到的。你除以1000將千位移到1的位置,然後使用模運算符來擺脫任何不在1的位置。這隻會讓你只有號碼的第四位數字。

public static int getFourthDigit(int num) { 
    return (num/1000) % 10; 
} 

原始

如果我理解正確的,你的困難與匹配不同訂購的4位數字。最明顯的做法是檢查所有可能的四位數字排序;只有15個地方不完全相同。但是,輸入15個條件很容易出錯,只是簡單的無聊。如果你需要用五位數字來做這件事,那將是無聊而乏味的兩倍。

這是一個通過使用String而不是int來避免這種情況的功能。它重複檢查猜測的第一個字符並檢查字符是否在答案中。然後,它從答案中刪除匹配的字符,以避免像11111112這樣的情況,其中猜測中的每個字符也在答案中,但它們不匹配。

+0

我的意思是這樣組合的所有可能的猜測和彩票爲4位和第四抽公式像我詮釋lotteryDigit1 =開獎/ 100; int lotteryDigit2 =(lottery%100)/ 10; int lotteryDigit3 = lottery%10; int lotteryDigit4 =? int guessDigit1 = guess/100; int guessDigit2 =(guess%100)/ 10; int guessDigit3 = guess%10; int guessDigit 4 =? – Snooky

+0

哇,男孩,我誤解了這個問題。我會編輯它來回答你要找的東西。 – jcolemang

+0

但我怎麼知道所有可能的組合 – Snooky