2015-09-10 82 views
0

我有一個問題,寫一個彩票遊戲的Java程序,我已經寫了作品,但我不能得到的總和部分正常工作。我需要它添加每一個數字,例如如果結果打印234,456,345我需要添加2 + 3 + 4 + 4 + 5 + 6 + 3 + 4 + 5然後返回總和,但我得到的是234+ 456 + 345任何幫助都會很好。在java中的彩票遊戲

import java.util.Scanner; 
public class LotteryDraw { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int game; 
     int times; 
     int randNum; 
     int lotNum; 
     int sum = 0; 
     System.out.println("Which lottery game do you want to play!"); 
     System.out.println("Enter 3 for Pick 3"); 
     System.out.println("Enter 4 for Pick 4"); 
     System.out.println("Enter 5 for Pick 5"); 
     game = input.nextInt(); 

     System.out.print("How many games would you like to play? "); 
     times = input.nextInt(); 
     System.out.println("Thank you! The numbers selected were: "); 
     for(int i = 0; i < times; i++) { 
      lotNum = 0; 
      for(int j = 0; j < game; j++) 
      { 
       randNum = (new java.util.Random()).nextInt(10); 
       lotNum = (lotNum * 10) + randNum; 
       System.out.print(randNum); 
      } 
      System.out.println(); 
      sum += lotNum; 
     } 
     System.out.println("Sum of the numbers of all games: " + sum); 
    } 
} 
+0

爲什麼要乘以lotNum * 10?這就是你以234 + 456 + 345而不是2 + 3 + 4 + 4 + 5 + 6 + 3 + 4 + 5結束的原因。 – georgeofallages

+0

請檢查問題中包含的代碼。它似乎已損壞。 – sanastasiadis

+0

非常感謝你的幫助...... – master2503

回答

0

未經測試的代碼:

試試這個

import java.util.Scanner; 
public class LotteryDraw { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int game; 
     int times; 
     int randNum; 
     int lotNum; 
     int sum = 0; 
     System.out.println("Which lottery game do you want to play!"); 
     System.out.println("Enter 3 for Pick 3"); 
     System.out.println("Enter 4 for Pick 4"); 
     System.out.println("Enter 5 for Pick 5"); 
     game = input.nextInt(); 

     System.out.print("How many games would you like to play? "); 
     times = input.nextInt(); 
     System.out.println("Thank you! The numbers selected were: "); 
     for(int i = 0; i < times; i++) { 
      lotNum = 0; 
      for(int j = 0; j < game; j++) 
      { 
       randNum = (new java.util.Random()).nextInt(10); 
       lotNum = (lotNum * 10) + randNum; 
       System.out.print(randNum); 
       sum+=randNum; 
      } 
      System.out.println(); 
     } 
     System.out.println("Sum of the numbers of all games: " + sum); 
    } 
}