2016-11-10 45 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将始终打印,就像语句包含空体一样。

相关问题