I have an assignment where I need to make a Roulette program using a java program (Im using Eclipse). The program has a requirment that states: Given the following method heading , you must write the corresponding definition for a int return method that reads, validates, and returns a number to bet on, which must be between 0 and 36. (The heading is public static int getNumber(Scanner stdIn) ). The problem I'm having is with the while statement, because it doesnt recognize the greater/less than symbol. How do I go about fixing this?
import java.util.Scanner;
public class Program08
{
public static void main(String[]args)
{
welcome();
Scanner stdIn = new Scanner(System.in);
int number;
number = getNumber(stdIn);
}
public static void welcome()
{
System.out.println("Welcome to Roulette!");
System.out.println("Number bets payout: 35:1");
}
public static int getNumber(Scanner stdIn)
{
do
{
//int play;
System.out.println("Enter the number to bet on [0-36]: ");
//play = stdIn.nextInt;
}
while (stdIn<0 || stdIn>36);
}
}//end of program
You have compilation errors in your program. Here is the code that compiles correctly,
import java.util.*;
public class Program08{
public static void main(String[] args){
welcome();
Scanner stdIn = new Scanner(System.in);
int number;
number = getNumber(stdIn);
}
public static void welcome(){
System.out.println("Welcome to Roulette!");
System.out.println("Number bets payout: 35:1");
}
public static int getNumber(Scanner stdIn){
int play = 0;
do{
System.out.println("Enter the number to bet on [0-36]: ");
play = stdIn.nextInt();
} while (play < 0 || play > 36);
return play;
}
}
What were the fixes:
getNumber()
declare play
(the integer variable where you are going to store the choice of the user) outside of the do-while loop. You do that so the while section of the do-while statement can access that variable.Scanner
object ( stdIn
) to read a number from the user and store it in play
. This does that: play = stdIn.nextInt()
(assuming the user will enter a number and not some arbitrary text which would produce an error or more precisely raise an exception ).play
in the while section of the do-while statement. What you wrote, stdIn < 0
is fundamentally wrong since you're comparing a Scanner
object, which is not a number, to a number. Imagine in place of a Scanner
having another object, say a Person
. Could you say that George (an instance of Person
) is greater than zero? Obviously you cannot. You could say though that George's age (which is a number) is greater than zero, George.age > 0
.That's because you have to call the method nextInt()
on the scanner to get the actual value.
So outside your loop, declare an int input
, set it at the end of the loop and then check that:
int input;
do {
//... stuff
input = stdIn.nextInt();
} while(input < 0 || input > 36);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.