Here I am once again. I have listened to all the previous suggestions, but because my course hasn't taught me about methods I'm wary of using one. The program should prompt the user to enter a number and a maximum number of guesses, generate a random number, have the user exhaust all the guesses or guess the correct number, then ask whether the user wants to play again. I noticed three errors and if you could help identify their solutions I will be extremely grateful:
If I enter a negative number twice in the guesses does not produce anything (I’m assuming a runtime error), same goes for if I enter 0 twice in the guesses.
After the second round of the game it doesn’t ask the user whether they would like to play again.
It is inconsistent when recognizing when the maximum number of guesses is greater than the total possible amount of numbers. i.e. If I enter 1 for guess and 1 for upper limit, this shouldn’t display the message that I should “challenge” myself, because 0 and 1 are two possible numbers being generated, greater than 1, because 2 yet it still displays that.
import java.util.Scanner;
import java.util.Random;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Hello! Here are the rules for this game:");
System.out.println("1. You choose the limit of the secret number!");
System.out.println("2. I generate the secret number!");
System.out.println("3. You choose the maximum number of guesses!");
System.out
.println("4. You try to guess the secret number I generated within the number of guesses you entered!");
System.out.println("5. You may win a prize!");
System.out.println(
"With that being said, would you like to play? If yes please enter true, if not please enter false.");
boolean wantToPlay = input.nextBoolean();
while (wantToPlay != true) {
System.out.println("Thank you and remember to wash your hands! You may close me.");
System.out.println(
"Did you accidentally press false? If yes please enter true, if not you may leave the program. ");
wantToPlay = input.nextBoolean();
}
System.out.println(
"Please enter the limit of the secret number. Please note it must be a greater than 0 positive integer.");
System.out.println("Please note that your secret number will be between zero and the number you just entered.");
int upperBound = input.nextInt();
System.out.println("The upper bound you entered is" + " " + upperBound + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false.");
boolean yesOrNo = input.nextBoolean();
if (upperBound <= 0) {
yesOrNo = false;
}
while (yesOrNo != true) {
System.out.println(
"Please enter the new upper bound of the secret number. Remember it must be a greater than 0 positive integer.");
upperBound = input.nextInt();
System.out.println("The upper bound you entered is" + " " + upperBound + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false. ");
yesOrNo = input.nextBoolean();
if (upperBound <= 0) {
yesOrNo = false;
}
}
int secretNumber = (int) (Math.random() * upperBound);
System.out.println(
"Please enter the maximum number of guesses. Please note it must be a greater than 0 positive integer.");
int numberOfGuesses = input.nextInt();
if (numberOfGuesses >= ++secretNumber) {
System.out.println(
"Please note that the number of guesses you entered is equal or greater than the possible number of values for the secret number.");
System.out.println("If you would like more of a challenge please re-enter the number of guesses below.");
}
System.out.println("The number of guesses you entered is" + " " + numberOfGuesses + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false.");
boolean yesOrNo2 = input.nextBoolean();
if (numberOfGuesses <= 0) {
yesOrNo2 = false;
}
while (yesOrNo2 != true) {
System.out.println(
"Please enter the new number of guesses. Remember it must be a greater than 0 positive integer.");
numberOfGuesses = input.nextInt();
if (numberOfGuesses >= ++secretNumber) {
System.out.println(
"Please note that the number of guesses you entered is equal or greater than the possible number of values for the secret number.");
System.out
.println("If you would like more of a challenge please re-enter the number of guesses below.");
System.out.println(
"If not you can still continue with your previous number as long as you re-enter it below.");
}
numberOfGuesses = input.nextInt();
System.out.println("The number of guesses you entered is" + " " + numberOfGuesses + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false. ");
yesOrNo2 = input.nextBoolean();
if (numberOfGuesses <= 0) {
yesOrNo2 = false;
}
}
boolean loop = false;
for (int i = 0; i < numberOfGuesses; ++i) {
System.out.println("Please enter your guess.");
int guesses = input.nextInt();
if (guesses > secretNumber) {
System.out.println("Your guess is too high.");
}
if (guesses < secretNumber) {
System.out.println("Your guess is too low.");
}
if (guesses == secretNumber) {
System.out.println("Your guess is correct.");
System.out.println("Congragulations you won the game.");
loop = true;
break;
}
}
if (loop == false) {
System.out.println("Sorry you lost the game.");
System.out.println(
"Would you like to see the secret number? If so please enter true, if not please enter false");
boolean seeSecretNumber = input.nextBoolean();
if (seeSecretNumber == true) {
System.out.println("The secret number was " + secretNumber);
}
}
System.out.println("Would you like to play again? If so, please enter true, if not you may enter no.");
boolean playAgain = input.nextBoolean();
while (playAgain == true) {
System.out.println(
"Please enter the limit of the secret number. Please note it must be a greater than 0 positive integer.");
System.out.println(
"Please note that your secret number will be between zero and the number you just entered.");
upperBound = input.nextInt();
System.out.println("The upper bound you entered is" + " " + upperBound + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false.");
yesOrNo = input.nextBoolean();
if (upperBound <= 0) {
yesOrNo = false;
}
while (yesOrNo != true) {
System.out.println(
"Please enter the new upper bound of the secret number. Remember it must be a greater than 0 positive integer.");
upperBound = input.nextInt();
System.out.println("The upper bound you entered is" + " " + upperBound + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false. ");
yesOrNo = input.nextBoolean();
if (upperBound <= 0) {
yesOrNo = false;
}
}
secretNumber = (int) (Math.random() * upperBound);
System.out.println(
"Please enter the maximum number of guesses. Please note it must be a greater than 0 positive integer.");
numberOfGuesses = input.nextInt();
if (numberOfGuesses >= ++secretNumber) {
System.out.println(
"Please note that the number of guesses you entered is equal or greater than the possible number of values for the secret number.");
System.out
.println("If you would like more of a challenge please re-enter the number of guesses below.");
}
System.out.println("The number of guesses you entered is" + " " + numberOfGuesses + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false.");
yesOrNo2 = input.nextBoolean();
if (numberOfGuesses <= 0) {
yesOrNo2 = false;
}
while (yesOrNo2 != true) {
System.out.println(
"Please enter the new number of guesses. Remember it must be a greater than 0 positive integer.");
numberOfGuesses = input.nextInt();
if (numberOfGuesses >= ++secretNumber) {
System.out.println(
"Please note that the number of guesses you entered is equal or greater than the possible number of values for the secret number.");
System.out.println(
"If you would like more of a challenge please re-enter the number of guesses below.");
System.out.println(
"If not you can still continue with your previous number as long as you re-enter it below.");
}
numberOfGuesses = input.nextInt();
System.out.println("The number of guesses you entered is" + " " + numberOfGuesses + "." + " "
+ "Is that correct? If yes please enter true, if not please enter false. ");
yesOrNo2 = input.nextBoolean();
if (numberOfGuesses <= 0) {
yesOrNo2 = false;
}
}
loop = false;
for (int i = 0; i < numberOfGuesses; ++i) {
System.out.println("Please enter your guess.");
int guesses = input.nextInt();
if (guesses > secretNumber) {
System.out.println("Your guess is too high.");
}
if (guesses < secretNumber) {
System.out.println("Your guess is too low.");
}
if (guesses == secretNumber) {
System.out.println("Your guess is correct.");
System.out.println("Congragulations you won the game.");
loop = true;
break;
}
}
if (loop == false) {
System.out.println("Sorry you lost the game.");
System.out.println(
"Would you like to see the secret number? If so please enter true, if not please enter false");
boolean seeSecretNumber = input.nextBoolean();
if (seeSecretNumber == true) {
System.out.println("The secret number was " + secretNumber);
break;
}
}
}
System.out.println("Would you like to play again? If so please enter true, if not please enter false.");
playAgain = input.nextBoolean();
if (playAgain == false) {
System.out.println("Thank you for playing, have a good day and wash your hands!");
}
}
}
Methods and functions are your friend. I highly recomend you start learning about them. They will reduce the amount of code you write and make it easier to read. A quick duckduckgo.com search of "java methods" will take you to Tutorialspoint and W3Schools pages.
I recommend using (or learning) the debugger on your IDE (e.g. Netbeans, Eclipse). You will find it makes fault finding hundreds of times easier.
First rule of dealing with user input: don't allow user input.
Other general rules for dealing with user input: Users dont read instructions, users will do the opposite of your instructions, users will try and do things to your program you've never thought of, limit as much as possible all user input, validate every user input everytime.
These are the possible solutions to your errors for you.
Error 1. User input validation will solve this - there is not a requirement for negetive numbers.
Error 2. Putting the game code into a game while loop while(continueGame) will solve this. You start off with boolean continueGame = true. At the end of the game you ask the question "do you want to play again?" yes = true, no = false. The loop will or will not run again dependant on the answer.
Error 3. Change if (numberOfGuesses >= ++secretNumber) to if(numberOfGuessess > secretNumber)
Related
I have been trying to perfect this program on my own, and I just can't see what I'm doing wrong. The aim of this program is to do these things.
1) ask user for number
2) if the number is positive, print out the number
3) if the number is also a prime number, print that it's also a prime number
4) keep doing the above things until a negative number is inputed by the user.
The problem is, the program only works and determines if a number entered is a prime number or not, at the beginning. After that, when the user is asked for another number (if it's greater than 0), the program just doesn't loop back to the beginning to determine if the number is prime. Instead, it just sticks to what the value at the beginning was determined to be (prime or not prime) and prints out the same statement as what you would get for the first value, for the second. I want it to reevaluate the value every time to see if the number is prime or not, until the user inputs a negative number.
P.S. This is my first year going for a C.S degree. I find programing really fun and challenging (the concept). But I embrace it that challenge and find a sense of accomplishment every time I work through these problems.
import java.util.Scanner;
public class Prime3 {
public static void main(String[] args) {
int userNum;
int i = 2;
boolean isPrime = true;
Scanner input = new Scanner(System.in);
// Ask user for initial number
System.out.println("Please enter a number.");
userNum = input.nextInt();
// Determining whether or not number entered is prime
while (i <= userNum/2) {
System.out.println("Checking if " + i + " is a multiple of n");
if (userNum%i == 0) {
System.out.println(i + " is a multiple of " + userNum);
isPrime = false;
break;
}
i++;
}
// Print out user number if the number is positive.
while (userNum > 0) {
System.out.println("You entered the number, " + userNum);
if (isPrime) { // If it's a prime, state that it's a prime
System.out.println("No even multiples found. " + userNum + " is a prime number");
}
userNum = input.nextInt();
}
System.out.println("Invalid input. Program now ending.");
System.exit(0);
}
}
Well, I see the problem. You should move while (userNum > 0) to the top. So your final code should look something like this:
public class Prime3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Ask user for initial number
System.out.println("Please enter a number.");
int userNum = input.nextInt();
while (userNum > 0) {
int i = 2;
boolean isPrime = true;
// Determining whether or not number entered is prime
while (i <= userNum / 2) {
System.out.println("Checking if " + i + " is a multiple of n");
if (userNum % i == 0) {
System.out.println(i + " is a multiple of " + userNum);
isPrime = false;
break;
}
i++;
}
// Print out user number if the number is positive.
System.out.println("You entered the number, " + userNum);
if (isPrime) { // If it's a prime, state that it's a prime
System.out.println("No even multiples found. " + userNum + " is a prime number");
}
userNum = input.nextInt();
}
System.out.println("Invalid input. Program now ending.");
System.exit(0);
}
In general construction of this code is error prone. You should do something like:
set userNum = 0
enter while (userNum >=0)
Call method which check if userNum is positive, if yes print
Call method which check if userNum is prime, if yes print
get new value from input console into userNum
end body of loop.
If you follow there should be no problem.
Create a program that randomly generates a number from 1-100 and asks the user to guess it. If the number the user inputs is to low or to high display a message to tell them so. When the user guesses the random number tell the user how much tries it took him to get that number. After that ask the user if they want to do it again if the user does repeat the process with a new random number generated.
The problem is that I can't seem to figure out how to let the user do it again, it seems to display an error in code when I run the program. If anyone can help me with this issue that would be great. Thank you!
import java.util.Scanner;
import java.util.Random;
public class RandomGuess
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
Random randy = new Random();
//#declaring variables
int num, count = 0;
final int random = randy.nextInt(100);
String input;
char yn;
//#random number
System.out.println("Num = " + random);
//#title or header
System.out.println("Random Number Guessing Game");
System.out.println("===========================");
//#asking user for input
do
{
System.out.print("Guess the random number " +
"from 1 to 100===> ");
num = keyboard.nextInt();
//#if the number the user entered
//#was less than the random number
if(num < random)
{
//#display this message
System.out.println("Your guess is too low try again...");
System.out.println();
}
//#if the number the user entered
//#was less than the random number
if(num > random)
{
//#display this message
System.out.println("Your guess is too high try again...");
System.out.println();
}
count++;
if (num == random)
{
System.out.println("You guessed the random number in " +
count + " guesses!");
break;
}
do
{
System.out.print("Continue? (Y or N)==> ");
input = keyboard.nextLine();
yn = input.charAt(0);
}
while(yn == 'Y' || yn == 'y');
}
while (num > 1 || num > 100);
}
}
There are a couple of problems with your code without even seeing the error that is displayed (I've put comments in those areas):
count++;
if (num == random)
{
System.out.println("You guessed the random number in " +
count + " guesses!");
break;
} // You should put an else here
do
{
System.out.print("Continue? (Y or N)==> ");
input = keyboard.nextLine();
yn = input.charAt(0);
}
while(yn == 'Y' || yn == 'y'); // This will keep asking if you want to try again so long as you enter a "y"
// But it won't actually let you try.
// Why? Because if you enter a y" it will loop back to the question.
}
while (num > 1 || num > 100); // This should probably be (random != num)
}
}
Here is a revised version
count++;
if (num == random) {
System.out.println("You guessed the random number in " +
count + " guesses!");
} else {
yn = 'x'; // can be anything other than y or n
while(yn != 'y' && yn != 'n') {
System.out.print("Continue? (Y or N)==> ");
input = keyboard.nextLine();
yn = input.toLowerCase().charAt(0);
}
}
}
while (num != random && yn == 'y');
}
}
Hopefully this is enough to move you forward.
Also, please post the error message and/or a description of what it is doing wrong along with a description as to what you actually wnt it to do.
As for the exception, the problem is that scanner.nextInt does not consume the newline at the end of the numbe you entered. So, your "continue Y/N" question gets what's left over from the previous line (i.e. a new line => an empty string).
You could try this:
num = -1; // Initialise the number to enable the loop
while (num <= 1 || num >= 100) {
System.out.print("Guess the random number from 1 to 100===> ");
String ans = keyboard.nextline();
try {
num = Integer.parseInt(); // Convert the string to an integer - if possible
} catch (NumberFormatException e) {
// If the user's input can not be converted to an integer, we will end up here and display an error message.
System.out.println ("Please enter an integer");
}
}
Hi im currently creating a guessing letter game! So far i have made it so you guess between the numbers 1 and 26 using the java.util.random class. However this random number should be converted into the corresponding number within the alphabet! ie a=1 etc..This is where my problem is i dont know how to convert this randomly generated number into a letter! This needs to be related to the integer as i have to able to tell the user if they are above the letter in the alphabet or to low based on the guess they entered! One more thing is that the users guess will be a letter also! Below is my code! Any help will be greatly appreciated!
{
Random rand = new Random(); //This is were the computer selects the Target
int guess;
int numGuesses = 0;
int Target;
String userName;
String playagain;
boolean play = true;
int session = 0;
int sessions = 0;
int bestScore = 0;
Scanner consoleIn = new Scanner(System.in);
Scanner name = new Scanner(System.in);
System.out.println("Hello! Please enter your name:\n"); //This is were the user enters his/her name
userName= name.nextLine();
System.out.println("Hello "+ userName + " :) Welcome to the game!\n");
while (play = true)
{
session++;
Target = rand.nextInt(26) + 1;
System.out.println("Guess a number between 1 and 26? You will have 5 attempts to guess the correct number"); //This is where the computer asks the user to guess the number and how many guesses they will have
do {
guess = consoleIn.nextInt();
numGuesses++;
if (guess > 26)
System.out.println("Error! Above MAXIMUM range");
else if (guess <= 0)
System.out.println("Error! Below MINIMUM range");
else if (guess > Target)
System.out.println("Sorry! Your guess was too high! :)"); //This is to help the player get to the answer
else if (guess < Target)
System.out.println("Sorry! Your guess was too low! :)"); //This is to help the player get to the answer
}
while(guess != Target && numGuesses <5);
if(guess == Target) {
System.out.println("Congratulations "+ userName + ", it took you "+ numGuesses +" attempts to guess correctly!"); //This tells the player that they got the correct answer and how many attempts it took
sessions++;
}
else
{
System.out.println("Sorry "+ userName + ", You've used up all of your guesses! The correct answer was "+ Target + "!"); //This tells the player that they failed to find the number and then tells them what the correct answer
}
{
Scanner answer = new Scanner(System.in);
System.out.println("Would you like another GO "+ userName +"? [Y/N]");//This asks the player if they would like to play again
playagain = answer.nextLine();
if(playagain.equalsIgnoreCase("Y"))//This is what happens if the player opts to play again
{
play = true;
numGuesses = 0;
} else if(playagain.equalsIgnoreCase("N"))//This is what happens if the player opts to exit the game
{
play = false;
System.out.println("Thanks for playing "+ userName +"! :) Please come back soon!");
System.out.println("You had "+ session +" Goes");
System.out.println("The number of times you guessed correctly: "+ sessions +"");
break;
}
}
}
}
Random random = new Random();
char c = (char) (random.nextInt(26) + 'a');
This maps (0 to 25 ) to ('a' to 'z')
Hello I am a beginner Java programmer. My Program keeps ending when I put the correct number in (with out printing the message ), or it wont go from a lower number to a higher one with out ending?
Can someone tell me what I am doing wrong?
package week3;
import java.util.Scanner;
public class Week3 {
public static void main(String[] args) {
Scanner In = new Scanner(System.in);
int Guess ;
int count = 0;
count++;
int c = 55;
System.out.println("Welcome to the Higher / Lower game! Try to guess the"
+ "number between 1 and 100. ");
System.out.print("Enter your guess: ");
Guess = In.nextInt();
if(Guess == c)
{
System.out.println("The number was 55 you guessed correctly! "
+ " it took you " + count +" tries");
}//end if
while (Guess < 1)
{
System.out.println("Sorry, the guess needs to be a number between 1 and"
+ " 100. Please try again:");
Guess = In.nextInt();
}//End while < 1
while (Guess > 100)
{
System.out.println("Sorry, the guess needs to be a number between 1 and"
+ " 100. Please try again:");
Guess = In.nextInt();
}//end while <100
while (Guess >= 56)
{
System.out.println("The number is lower.");
Guess = In.nextInt();
} //end while over 55
while (Guess <= 54)
{
System.out.println("The number is higher.");
Guess = In.nextInt();
}//end while lower than 55
}//end main
}//end class
I can see the problem, but you would be better off trying to find it yourself. (Or having another go ...)
Hints:
Try hand-executing the code. Pretend you are the computer, and use a pencil and paper to simulate what it would do.
Have you tried using a debugger?
Some of the comments are good hints too.
While I have your ear, you also should pay attention to your programming style. And one universal style rule for Java is that variables should start with a lower-case letter. Your In and Guess variables violate this.
System.out.println("Welcome to the Higher / Lower game! Try to guess the"
+ "number between 1 and 100. ");
while (true) {
System.out.print("Enter your guess: ");
Guess = Integer.parseint(In.nextLine());
if(Guess == c)
{
System.out.println("The number was 55 you guessed correctly! "
+ " it took you " + count +" tries");
break;
}
if (Guess < 1)
{
System.out.println("Sorry, the guess needs to be a number between 1 and"
+ " 100. Please try again:");
}
if (Guess > 100)
{
System.out.println("Sorry, the guess needs to be a number between 1 and"
+ " 100. Please try again:");
}
if (Guess >= 56)
{
System.out.println("The number is lower.");
}
if (Guess <= 54)
{
System.out.println("The number is higher.");
}
}
}
}
Hopefully this gives you the idea of using the while loop.
The structure of your program is incorrect.
You only need one while loop. Begin your program with a while loop, and continue with conditions, such that:
boolean numFound = false;
while (!numFound) {
//conditions
}
I am trying to validate the the user input, but I can't get it to work.
The user has to enter an amount of Revenue between 0-20,000, but not anything more than that.
In addition, the user must enter expenses between 1500-10000, but not anything more than that or less than that.
I also am trying to loop the code as well. I am asking the user if they have additional records they want to enter in or not, and I am counting how many times the record has been done.
Can anyone tell me what I am doing wrong and point me in the right direction?
Here is what I have so far:
import javax.swing.JOptionPane;
import java.io.*; // Access System.out
import java.util.Scanner;
public class RevenueScan
{
public static void main(String[] args)
{
// Declarations
Scanner in = new Scanner(System.in);
int productNumber;
float revenue;
float expenses;
double finalValue;
char repeat;
int counter = 0;
String input;
Scanner keyboard = new Scanner(System.in);
// Do Loop to run
do
{
// Pop up to advise the user the conditions that have to be met for inputs
System.out.println("Please ensure that your revenue is between 0 to 20,000.00 dollars."
+ "\nPlease ensure that your expenses are between 1,500.000 to 10,000.00 dollars.");
// Pop up to ask the user the values of the variables
System.out.println("Enter in a Product Number (or-1 to END)"
+ "\nEnter the Revenue"
+ "\nEnter the Expenses");
// Read in values
productNumber = in.nextInt();
revenue = in.nextFloat();
expenses = in.nextFloat();
//States the values entered by user
while (revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000);
{
System.out.println("You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
{
System.out.println("Here is the product number you entered: " + productNumber + "."
+ "\nHere is the revenue you entered: " + revenue + "."
+ "\nHere are the expenses you entered: " + expenses + ".");
counter++;
//calculates final value
}
}
finalValue = revenue - expenses;
// Calculates final value and displays as net profit, loss or break even.
if (finalValue > 0)
{
System.out.println("You made a profit. Your net income is: " + finalValue);
}
else
if (finalValue == 0)
{
System.out.println("You broke even. Your revenue was " + revenue + " your expenses were " + expenses);
}
else
if (finalValue < 0)
{
System.out.println("You have not made any profit. Your net loss is: " + finalValue);
}
System.out.println("Number of records: " + counter);
//validate user input
System.out.println("Would you like to input more records?");
System.out.println("Enter 'Y' for yes or 'N' for no.");
input = keyboard.nextLine();
repeat = input.charAt(0);
}
while (repeat == 'Y' || repeat == 'y');
{
}
}
}
You have a ; after the while-statement, it shouldn't be there, otherwise the while-loop is empty, as opposed to containing the block following it.
while (revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000)
{
System.out.println("You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
{
System.out.println("Here is the product number you entered: " + productNumber + "."
+ "\nHere is the revenue you entered: " + revenue + "."
+ "\nHere are the expenses you entered: " + expenses + ".");
counter++;
}
}
But once you fix this, the above block will just keep looping, since none of the values can change inside that loop.
Also, those inner brackets {} are somewhat pointless.
I recommend this:
if (revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000)
{
System.out.println("You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
System.out.println("Here is the product number you entered: " + productNumber + "."
+ "\nHere is the revenue you entered: " + revenue + "."
+ "\nHere are the expenses you entered: " + expenses + ".");
counter++;
continue; // go to the start of the do-while loop
}
Then you also have to change:
char repeat;
to:
char repeat = 'Y';
otherwise it doesn't compile since continue still triggers the condition check, and repeat won't be initialized the first time, and Java doesn't allow this.
If you want to stick to a while-loop, put something like these lines in there:
// tell user to input values again
System.out.println("Enter in a Product Number (or-1 to END)"
+ "\nEnter the Revenue"
+ "\nEnter the Expenses");
// read in values
productNumber = in.nextInt();
revenue = in.nextFloat();
expenses = in.nextFloat();
This will allow the user to input new values until the conditions are met.
And the format of a do-while loop is:
do { ... }
while (...);
Not:
do { ... }
while (...) { ... }
So change:
while (repeat == 'Y' || repeat == 'y');
{
}
To:
while (repeat == 'Y' || repeat == 'y');