2016-11-10 64 views
0

這是我爲我的班級工作的代碼。當我運行它時,它掛在歡迎信息上,不會再繼續,它甚至不會讓我輸入任何內容。我多次問過我的導師,他給出了很好的建議,但我得到的更正仍然沒有解決我所遇到的問題。我究竟做錯了什麼?爲什麼我的Java程序無法正常工作?

/* 
* File: ForEveryGame.java 
* Author: Darren Pirtle 
* Date: November 8, 2016 
* Purpose: Provide the amount of money a customer can win per 
* NBA game if they are betting X amount of money for X amount of games 
*/ 
// Import statements 
import java.util.Scanner; 
public class ForEveryGame { 
    public static void main(String[] args) { 
     //Declare Variables 
     int numberGames = 0; 
     int moneyAmount = 0; 
     //Display A Welcome Message 
     System.out.println(" "); 
     System.out.println("How much money can you win?!"); 
     System.out.println(" "); 
     //Scanner class 
     Scanner scannerIn = new Scanner(System.in); 
     //Prompt the user for the number of games during the night 
     while (numberGames > 15 || numberGames <= 0); { 
      System.out.println("Enter the number of games being played tonight"); 
      numberGames = scannerIn.nextInt(); 
      if (numberGames > 15) 
       System.out.println("Please re-enter a number less than 16"); 
      if (numberGames <= 0) 
       System.out.println("you cannot play 0 or less tha 0 games"); 
     } 
     System.out.println(" "); 
     //Display Results 
     System.out.println("There are this many games being played tonight: " + numberGames); 
     System.out.println(" "); 
     //Prompt the user for how much he wants to bet on every game 
     System.out.println("How much money are you willing to bet on " + numberGames + " games?"); 
     moneyAmount = scannerIn.nextInt(); 
     // Display results 
     System.out.println("You are willing to bet $" + moneyAmount + " on " + numberGames + " games."); 
     System.out.println(" "); 
     // While Loop 
     // Declare Variables 
     int count = 1; 
     while (count <= numberGames) { 
      int multiplyInt = (count * moneyAmount); 
      System.out.println("You can win: " + moneyAmount + " X " + count + " = $" + multiplyInt); 
      // Increment counter 
      count++; 
     } 
    } 
} 

Error

+1

while語句後刪除分號 – bcorso

+0

非常感謝! – DarrenPJr

回答

9
​​

別無分號

1

只是刪除分號在你第一次while。您的代碼:

import java.util.Scanner; 
public class ForEveryGame { 
    public static void main(String[] args) { 
     //Declare Variables 
     int numberGames = 0; 
     int moneyAmount = 0; 
     //Display A Welcome Message 
     System.out.println(" "); 
     System.out.println("How much money can you win?!"); 
     System.out.println(" "); 
     //Scanner class 
     Scanner scannerIn = new Scanner (System.in); 
     //Prompt the user for the number of games during the night 
     while (numberGames > 15 || numberGames <= 0) 
     { 
      System.out.println("Enter the number of games being played tonight"); 
      numberGames = scannerIn.nextInt(); 
      if (numberGames > 15) 
       System.out.println("Please re-enter a number less than 16"); 
      if (numberGames <= 0) 
       System.out.println("you cannot play 0 or less tha 0 games"); 
     } 
     System.out.println(" "); 
     //Display Results 
     System.out.println("There are this many games being played tonight: " + numberGames); 
     System.out.println(" "); 
     //Prompt the user for how much he wants to bet on every game 
     System.out.println("How much money are you willing to bet on " + numberGames + " games?"); 
     moneyAmount = scannerIn.nextInt(); 
     // Display results 
     System.out.println("You are willing to bet $" + moneyAmount + " on " + numberGames + " games."); 
     System.out.println(" "); 
     // While Loop 
     // Declare Variables 
     int count = 1; 
     while (count <= numberGames) { 
      int multiplyInt = (count * moneyAmount); 
      System.out.println("You can win: " + moneyAmount + " X " + count + " = $" + multiplyInt); 
      // Increment counter 
      count++; 
     } 
    } 
} 
1

刪除您的while循環標題末尾的罪魁禍首分號。

​​

這是大多數新手程序員犯的一般錯誤。在從C中繼承其語法的語言中,被視爲一個空的陳述。因此,從語法的角度來看,雖然循環中有空語句是完全有效的,但是從邏輯角度來看是一個錯誤

如果在if語句的末尾添加分號,也會出現類似的問題。

例如。

if (age > 10); 
     System.out.println("age > 10"); 

在這裏,年齡> 10將始終打印,就像語句包含空體一樣。

相關問題