2016-11-10 6 views
0

Dies ist der Code, den ich für meine Klasse arbeite. Wenn ich es starte, hängt es bei der Willkommensnachricht und wird nicht weiter gehen, es lässt mich gar nichts tippen. Ich habe meinen Lehrer mehrmals gefragt, und er gibt gute Ratschläge, aber die Korrekturen, die mir gegeben werden, beheben das Problem, das ich habe, immer noch nicht. Was mache ich falsch?Warum funktioniert mein Java-Programm nicht richtig?

/* 
* 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

Entfernen Semikolon nach while Anweisung – bcorso

+0

Vielen Dank !!! – DarrenPJr

Antwort

9
while (numberGames > 15 || numberGames <= 0); 

kein Semikolon

1

einfach das Semikolon in Ihrem ersten while entfernen. Ihr Code:

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

Entfernen Sie das Täter-Semikolon am Ende Ihrer While-Schleife.

while (numberGames > 15 || numberGames <= 0); 

Dies ist ein allgemeiner Fehler, den die meisten Anfänger Programmierer machen. In Sprachen, die ihre Syntax von C erben,; wird als leere Aussage betrachtet. Daher ist while loop mit einer leeren Anweisung aus der Syntaxperspektive perfekt gültig, aber ein Fehler aus der logischen Perspektive.

Ähnliche Art von Problem tritt auch auf, wenn Sie Semikolon am Ende der if-Anweisung setzen.

z.

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

Auch hier wird alters> 10 immer gedruckt, als ob eine Aussage einen leeren Körper enthält.

Verwandte Themen