Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
For some reason, my program stops when it reaches the part that asks the user if it know it's exam 1 score. I need the user to be able to enter yes or no. Why does the program stop? I need it to work properly. I have all the if-else statements. I am able to enter the percentage weights, but that is all that the program will do. More must be done. My code extends far beyond entering the percentage weights. Please help me.
import java.util.Scanner;
public class GradeCalculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner grade = new Scanner (System.in);
// A new scanner must be created. The scanner is essential to this program performing properly.
double A = 90-100;
double B = 80-89;
double C = 70-79;
double D = 60-69;
double F = 0-59;
String LetterGrade;
String yes;
String no;
double Exam1, Exam2, finalExam, Labs, Projects, Attendance, Quizzes;
double Exam1Grade, Exam2Grade, finalExamGrade, LabAverage, ProjectsAverage, AttendanceAverage, QuizzesAverage;
double knownWeight;
double PercentageWeights;
// As always, the variables must be declared at the beginning of the program.
System.out.print(
"Grading Scale:\n"+
"A = 90-100 \n"+
"B = 80-89 \n"+
"C = 70-79 \n"+
"D = 60-69 \n"+
"F = 00-59 \n");
System.out.println("What letter grade do you want to achieve in this course?");
LetterGrade = grade.next();
// The user will type the letter grade that it wants in this part.
System.out.println("\nEnter Percentage Weights: \t");
String input = grade.nextLine();
// The string above is needed when the user enters the exam grades and averages.
System.out.print("\n\nExam 1: \t");
Exam1 = grade.nextShort();
System.out.print("\nExam 2: \t");
Exam2 = grade.nextShort();
System.out.print("\nFinal Exam: \t");
finalExam = grade.nextShort();
System.out.print("\nLabs: \t");
Labs = grade.nextShort();
System.out.print("\nProjects: \t");
Projects = grade.nextShort();
System.out.print("\nAttendance: \t");
Attendance = grade.nextShort();
System.out.print("\nQuizzes: \t");
Quizzes = grade.nextShort();
PercentageWeights = (int)(Exam1 + Exam2 + finalExam + Labs + Projects + Attendance + Quizzes);
// The equation above will provide the sum of the percentage weights. Since the variables in the equation were
// originally declared as doubles, I had to put "int" before the actual equation.
if (PercentageWeights > 100 || PercentageWeights < 100) {
System.out.println("\nWeights do not add up to 100. Program exiting. Have a nice day!");
System.exit(0);
}
else {
System.out.println("\nEnter your scores out of a 100: \t");
}
// The part above is very important to continue the rest of the program. If the sum of the percentage weights equals 100,
// the program will continue to run. If the sum is greater than or less than 100, the program will terminate.
System.out.print("\nDo you know your Exam 1 score?");
if (input.equalsIgnoreCase("yes")) {
System.out.print("\nScore received on Exam 1: ");
Exam1Grade = grade.nextDouble();
}
else{
Exam1Grade = 0;
}
System.out.print("\nDo you know your Exam 2 score?");
if (input.equalsIgnoreCase("yes")) {
System.out.print("\nScore received on Exam 2: ");
Exam2Grade = grade.nextDouble();
}
else{
Exam2Grade = 0;
}
System.out.print("\nDo you know your final exam score?");
if (input.equalsIgnoreCase("yes")){
System.out.print("\nScore received on final exam: ");
finalExamGrade = grade.nextDouble();
}
else{
finalExamGrade = 0;
}
System.out.print("\nDo you know your lab average?");
if (input.equalsIgnoreCase("yes")){
System.out.print("\nAverage lab grade: ");
LabAverage = grade.nextDouble();
}
else{
LabAverage = 0;
}
System.out.print("\nDo you know your project average?");
if (input.equalsIgnoreCase("yes")){
System.out.print("\nAverage project grade: ");
ProjectsAverage = grade.nextDouble();
}
else{
ProjectsAverage = 0;
}
System.out.print("\nDo you know your quiz average?");
if (input.equalsIgnoreCase("yes")) {
System.out.print("\nAverage quiz grade: ");
QuizzesAverage = grade.nextDouble();
}
else{
QuizzesAverage = 0;
}
System.out.print("\nDo you know your attendance average?");
if (input.equalsIgnoreCase("yes")){
System.out.print("\nAverage Attendance Grade: ");
AttendanceAverage = grade.nextDouble();
}
else{
AttendanceAverage = 0;
}
// The user has finished answering the questions. Now the program will automatically calculate the data based on
// what the user typed into the program.
double CurrentGrade, avgToFinalLetterGrade, WeightandGrade, finalOverallGrade;
// The doubles above need to be declared in order for the equations below to work properly.
WeightandGrade = (int)((Exam1 * Exam1Grade) + (Exam2 * Exam2Grade) + (finalExam * finalExamGrade) + (Labs * LabAverage) + (Projects * ProjectsAverage) + (Quizzes * QuizzesAverage) + (Attendance * AttendanceAverage));
CurrentGrade = (int)((WeightandGrade) / (Exam1 + Exam2 + finalExam + Labs + Projects + Quizzes + Attendance ));
knownWeight = (Exam1 + Exam2 + finalExam + Labs + Projects + Quizzes + Attendance);
if (grade.equals(A)){
finalOverallGrade = 90;
}
else if (grade.equals(B)){
finalOverallGrade = 80;
}
else if (grade.equals(C)){
finalOverallGrade = 70;
}
else if (grade.equals(D)){
finalOverallGrade = 60;
}
else
finalOverallGrade = F;
avgToFinalLetterGrade = (((100-finalOverallGrade) * (WeightandGrade)) / (100 - knownWeight));
// The equations above are one of the last parts of the program. These equations are critical to determine whether or not the user received its desired letter grade.
// If the desired grade was not reached, the program will give a score that the user must consistently receive in order to possibly reach the desired letter grade.
if (finalOverallGrade >= 90){
System.out.print("Congratulations! You got an A in the class! Hooray!");
}
else if (finalOverallGrade >=80 && finalOverallGrade < 90){
System.out.print("Good job. You got a B in the class!");
}
else if (finalOverallGrade >=70 && finalOverallGrade < 80){
System.out.print("You got a C in the class.");
}
else if (finalOverallGrade >=60 && finalOverallGrade < 70){
System.out.print("You got a D in the class.");
}
else
System.out.print("I'm sorry, but you have a failing grade in the class. May your GPA have mercy on your soul.");
}
}
There are quite a lot of things wrong with this code.
Doing double A=90-100; will set A equal to -10;
However, for your current question:
You do String input = grade.nextLine();
You never change input, and so if input isn't "yes", it will just skip getting the grades for each piece.
(You might want to also consult Using scanner.nextLine() for other pitfalls with using scanner.nextLine() intermixed with scanner.nextInt or similar [in summary: if you do scanner.nextInt, this doesn't consume the newline, so scanner.nextLine() will just get that newline and not the next line after that you might be expecting to get])
input = grade.nextLine() reads the remainder of the line with the user's "percentage weights" input on it. So unless the user had a priori knowledge to enter "yes", input will be empty.
I.e., you need to update input with user input before if (input.equalsIgnoreCase("yes")) {....
Related
I am a beginner coder using Netbeans Java. I have created a code that initially asks how many gallons are in your gas tank. Then, it will have a while loop asking how many miles you will be traveling for this first run and how fast are you traveling. This will repeat with a while loop until you input '0' to stop adding trips. I am stumped on how to convert this while loop into only using For loops. I would greatly appreciate the assistance. Here is my code that has while loops.
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int tank;
double miles;
double speed;
double totalMiles = 0.0;
int choice;
double time;
double totalTime = 0.0;
double fuelConsumption;
System.out.print("How many gallons of gas is in your tank (Integer 1-15)? ");
tank = input.nextInt();
System.out.printf("%s%d%s\n\n" , "You have ", tank , " gallons of gas in your tank.");
System.out.print("Are you going on a trip (1 = Yes or 0 = No)? ");
choice = input.nextInt();
while (choice == 1)
{
System.out.print("How many miles are you traveling? "); // miles
miles = input.nextFloat();
System.out.print("What is your speed for this run (MPH)? "); // speed
speed = input.nextInt();
System.out.print("\n");
totalMiles = totalMiles + miles;
time = (miles/speed);
totalTime += (time*60);
fuelConsumption = (20*(tank/totalMiles));
System.out.print("Is there another leg in your trip (1 = Yes or 0 = No)? "); // asking another leg
choice = input.nextInt();
if (choice == 0)
{
System.out.printf("%s%5.2f%s\n","Your data for this trip is: \n"
+ "You traveled a total of about ", totalMiles , " miles.");
System.out.printf("%s%.2f%s\n" , "You traveled about " , totalTime , " minutes.");
if (fuelConsumption >= 2)
{
System.out.println("Your car has enough gas to return.");
break;
}
else
{
System.out.println("Your car will need more gas to return.");
break;
}
}
}
}
}
That is not a use case for a for loop, where we iterate over a known number of elements for do a known number of iterations. Like, repeat 10 times or such.
Technically it can be solved with a for loop, but that is abusing the concept a bit. The while loop is a perfect fit for that task.
This is not a place to use a for-loop, you use a for loop for something like this:
printAmount = 10;
for (int i = 0; i < printAmount; i++) {
System.out.println("Hello World");
}
Here you are using the for loop to print "Hi" for the amount in printAmount.
Your case is different: You want the while-loop to repeat while the input is "1" so you use a WHILE-loop.
I am new to Java and I am trying to code a "Grading" system. Basically, when the code is ran, a window opens, it asks a question, you enter a number value, press enter, it loads another question, same process as before, hit enter, and repeat for the final time. At the end it takes those 3 numbers, does math and then displays a message. I have the basic code, I am just having issues with the popup window.
public class PopUp {
public static void main(String[] args) {
// Constant grade worth percentage
final int examWeight = 55;
final int hwWeight = 20;
final int activityWeight = 25;
// Current grades
double examGrade = 65.8;
double hwGrade = 55.3;
double activityGrade = 29.5;
double finalGrade;
// Math to figuring out final grade.
examGrade = examGrade * (examWeight / 100.0);
hwGrade = hwGrade * (hwWeight / 100.0);
activityGrade = activityGrade * (activityWeight / 100.0);
finalGrade = examGrade + activityGrade + hwGrade;
//round thingy
double rounded = Math.round(finalGrade *10)/10.0;
// What you made
if (rounded>=90.0 && rounded<100.0) {
System.out.println("You made an A");
}
if (rounded>=85.0 && rounded<92.9) {
System.out.println("You made a B");
}
if (rounded>=75.0 && rounded<84.9) {
System.out.println("You made a C");
}
if (rounded>=67.0 && rounded<74.9) {
System.out.println("You made a D");
}
if (rounded<=59.9) {
System.out.println("You are a fucking failure");
}
System.out.println("Your final grade is: " + rounded);
}
}
I need THAT code to display the information like this in a NEW WINDOW:
So basically:
Question one: 98.8 ENTER
Question two: 76.9 ENTER
Question three: 87.6 ENTER
Result: 93.3 END
In BlueJ the result I am trying to replicate in Eclipse is as follows:
// Ask student to input scores for exam, lab and nomework
IO.output("Enter your exam grade: ");
examScore = IO.inputDouble( );
IO.output("Enter your lab grade: ");
labScore = IO.inputDouble( );
IO.output("Enter your homework grade: ");
hwScore = IO.inputDouble( );
Then the very end would be
// Output the final grade
IO.outputln("Your final grade is " + finalGrade);
http://prntscr.com/9hicp6 -- Proof that it is a new window.
If you want a popup windows maybe this will help you:
String grade = JOptionsPane.showInputDialog("Enter your grade:");
Good luck.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working on an assignment and it is working well so far. But several aspects aren't working. For starters, my counters for int total and int counter won't work. Also my if statements don't seem to be working. I have been scratching my head for several days now.
The assignment calls for a program to input the order number and will loop based on how many orders the customer has. It also calls for customer name, sign type(wood or plastic), the number of characters,and color of characters.
Some more information:
The base price for all signs is $20.
If sign is wood, add $10. If it is plastic add $5.
The first 5 letters/numbers are included in base price, and $2 for each additional character.
Black or white characters are included in base price, there is an additional $8 for colored letters.
If the total charge is more than $100 give 25% discount on total price.
Here is my code right now:
import java.util.Scanner;
public class Carpenter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int orderNumber;
String custName;
String signType;
int numOfCharacters;
String color;
int i = 20;
double total;
int counter;
System.out.println("Enter your order number");
orderNumber = sc.nextInt();
counter=orderNumber;
counter--;
sc.nextLine();
System.out.println("Enter customer name");
custName = sc.next();
do{
System.out.println("Enter the sign type (wood or plastic)");
signType = sc.next();
if(signType == "wood") {
i+=10;
}
if(signType == "plastic") {
i+=5;
}
System.out.println("Enter the number of characters");
numOfCharacters = sc.nextInt();
if(numOfCharacters > 5) {
i += 2*(numOfCharacters-5);
}
System.out.println("Enter the color of characters");
color = sc.next();
if(color != "white" || color != "black") {
i += 8;
}
total= i;
System.out.println("Total is: $" + total);
if( total > 100 ) {
total = (total * 0.25);
System.out.println("The total is " + total );
}
}
while(counter <= orderNumber);
}
}
I added comments to guide you through the changes I made. Also, remember to call the sc.NextLine() function after you get user input so that they can input something different next time (this is called 'flushing' the buffer).
import java.util.Scanner;
public class Carpenter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int orderNumber;
String custName;
String signType;
int numOfCharacters;
String color;
int i = 20;
double total;
int counter;
//I changed the phrasing just because it is a little confusing
System.out.println("Enter your number of orders");
orderNumber = sc.nextInt();
counter = orderNumber;
sc.nextLine();
System.out.println("Enter customer name");
custName = sc.next();
sc.nextLine();
//When you know how many times you want to repeat something (like when a user tells you how many) I prefer using a for-loop, a do while loop works as well though
for(int x=0; x<counter;x++)
{
System.out.println("Enter the sign type (wood or plastic)");
signType = sc.next();
//When comparing Strings, there is a function that you can use to compare them rather than using '=='
// It is also good to use the 'equalsIgnoreCase()' function to be more user friendly and robust
if(signType.equalsIgnoreCase("wood")) {
i+=10;
}
if(signType.equalsIgnoreCase("plastic")) {
i+=5;
}
//Flush the buffer (I haven't tested if this is necessary or not, it is good practice though)
sc.nextLine();
System.out.println("Enter the number of characters");
numOfCharacters = sc.nextInt();
if(numOfCharacters > 5) {
i += 2*(numOfCharacters-5);
}
System.out.println("Enter the color of characters");
color = sc.next();
//Same concept as above, the differene is the ! before the function to test if it is false or not
if(!color.equalsIgnoreCase("white") || !color.equalsIgnoreCase("black")) {
i += 8;
}
}
total = i;
//You will not want to print this out until the end due to the possibility of it being over $100
// System.out.println("Total is: $" + total);
if( total > 100 ) {
//Mathematically speaking, you are making your total a quarter of what the original is, rather than taking a quarter off. You want 75% rather than 25%
// total = (total * 0.25);
total = (total * 0.75);
}
System.out.println("Total is: $" + total);
}
}
You should set counter to the correct starting value (which is presumably 1 in your case):
orderNumber = sc.nextInt();
counter=1;
//counter=orderNumber;
//counter--;
Then at the end of the loop, you should increment your counter:
do{
//code
counter++;
}
while(counter <= orderNumber);
When I run this code, which is a menu with many different options. it consists of many loops. Some of which I have yet to make. I am having trouble with my coin toss simulator. Although I have a for loop, the loop only cycles through once instead of until the user selects "0" to exit the program and return to the main menu but also my other problem is that the simulator wont return to the main menu if 0 is selected. What is going on??? here is an example of my troubles.
Enter seed value: 3
===== CS302 TOOL BOX =====
T > COIN TOSS SIMULATOR
G > GRADE ESTIMATOR
C > COLOR CHALLENGE
Q > QUIT
Type code letter for your choice: t
COIN TOSS SIMULATOR
Enter 0 to quit. How many tosses?
4
3.0 heads and 1.0 tails means 75.0% were heads
Then the simulator doesn't ask "enter 0 to quit. How many tosses?" again. in fact if I press another number, say 3 I'd get this. Also zero doesn't end the simulator and prompt the main menu again.
3 is not a valid choice.
===== CS302 TOOL BOX =====
T > COIN TOSS SIMULATOR
G > GRADE ESTIMATOR
C > COLOR CHALLENGE
Q > QUIT
System.out.println("");
System.out.println( "===== CS302 TOOL BOX =====\nT > COIN TOSS SIMULATOR\nG > GRADE ESTIMATOR\nC > COLOR CHALLENGE\nQ > QUIT");
{
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
String c;
while (!usersSelection)
{
{
System.out.print(""+ "Type code letter for your choice: ");
}
if (anotherScanner.hasNext("q|Q"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.println(""
+ ""
+ "Good-Bye");
break;
}
if (anotherScanner.hasNext("t|T")){
c = anotherScanner.next();
usersSelection = true;
System.out.println("");
System.out.println("COIN TOSS SIMULATOR");
System.out.println("");
System.out.println("Enter 0 to quit. How many tosses?");
Random rand = new Random();
Scanner insideScanner = new Scanner(System.in);
int feeble = insideScanner.nextInt();
double heads = 0;
double tails = 0;
boolean headsvsTails;
headsvsTails = rand.nextBoolean();
for (int i =0; i < feeble; i++)
{
headsvsTails = rand.nextBoolean();
if (headsvsTails == true){ heads++;}
else {tails++;}
}
System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));
}
if (anotherScanner.hasNext("g|G"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.println("");
System.out.println("GRADE ESTIMATOR");
Scanner gradeE = new Scanner(System.in);
double exam1;
double possiblePts;
double programPts;
double programPossible;
double avg;
double avge;
double avrg;
System.out.print("Enter exam points earned (int): ");
exam1=gradeE.nextDouble();
if (exam1<0)
{System.out.print("Enter exam points earned (int): ");
exam1=gradeE.nextDouble();}
System.out.print("Enter exam points possible (int): ");
possiblePts=gradeE.nextDouble();
if (possiblePts<0) {System.out.print("Enter exam points possible (int): ");
possiblePts=gradeE.nextDouble();}
System.out.print("Enter program points earned (int): ");
programPts=gradeE.nextDouble();
if (programPts<0) {System.out.print("Enter program points earned (int): ");
programPts=gradeE.nextDouble();}
System.out.print("Enter program points possible (int): ");
programPossible=gradeE.nextDouble();
if (programPossible<0) {System.out.print("Enter program points possible (int): ");
programPossible=gradeE.nextDouble();}
avge = exam1+programPts;
avrg = possiblePts+programPossible;
avg = avge/avrg*100;
if (avg<60)
System.out.println("Grade estimate for " +avg+ "% is a F");
else if (avg<70)
System.out.println("Grade estimate for " +avg+ "% is a D");
else if (avg<80)
System.out.println("Grade estimate for " +avg+ "% is a C");
else if (avg<90)
System.out.println("Grade estimate for " +avg+ "% is a B");
else if (avg>=90)
System.out.println("Grade estimate for " +avg+ "% is a A");
}
Alright buddy there is a lot wrong with your code. You constantly declare new Scanners. Spacing is inconsistent and portions of your code are separated from each other by a sea of white space the span of the Pacific Ocean.
Here is a fix:
if (anotherScanner.hasNext("t|T")){
c = anotherScanner.next();
usersSelection = true;
System.out.println("");
System.out.println("COIN TOSS SIMULATOR");
System.out.println("");
System.out.println("Enter 0 to quit. How many tosses?");
Random rand = new Random();
Scanner insideScanner = new Scanner(System.in);
int feeble = insideScanner.nextInt();
double heads = 0;
double tails = 0;
boolean headsvsTails;
headsvsTails = rand.nextBoolean(); // This is Worthless
while ( feeble != 0 ) { //Pay attention to this while loop
for (int i =0; i < feeble; i++) {
headsvsTails = rand.nextBoolean();
if (headsvsTails == true){ heads++;}
else {tails++;}
}
System.out.println(heads + " heads and " + tails + " tails means " + (heads/(heads+tails)*100 + "% were heads"));
System.out.println("Enter 0 to quit. How many tosses?"); //I ask the question again
heads = 0;
tails = 0;
feeble = insideScanner.nextInt();//I get new input
}
}
The key here is the while loop. I use your feeble variable as its condition. It will run so long as the user does not provide a 0.
Please take some time to walk a friend or a professor through your code. Explain it to them, encourage them to ask questions.
Also I consider it mandatory to read this. Don't be discouraged. When I look at the code I wrote 3 years ago it looked a lot like this. You will get better.
*fixed code so heads/tails is not additive based on input from comments.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing code that should create an infinite loop, but is currently terminating unexpectedly.
When I either type in 'Y' for yes or 'N' for no, this code should enter a non-terminating loop.
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class Math_Island_XTENDED_Final
{
static Scanner sc = new Scanner(System.in);
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Declarations
int Bignumber;
int Mednumber;
int Smallnumber;
double addition;
double subtraction;
double multiplcation;
double division;
double sphere_radius1;
double sphere_radius2;
double sphere_radius3;
double rectangle_measurements;
char repeat;
String input;
System.out.println("Welcome to Math Island :D ! ");
System.out.println("We will use some numbers for our Formula ! ");
System.out.println("1 rule, NO DECIMALS !! ");
System.out.println("Please Pick a # from 1 to 100 NOW!! ");
Bignumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 20 NOW!! ");
Mednumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 5 NOW!! ");
Smallnumber = sc.nextInt();
//Results
addition = Bignumber + Mednumber + Smallnumber;
subtraction = Bignumber - Mednumber - Smallnumber;
multiplcation = Bignumber * Mednumber * Smallnumber;
division = Bignumber / Mednumber / Smallnumber;
sphere_radius1 = Bignumber * 3.14 * 3.14;
sphere_radius2 = Mednumber * 3.14 * 3.14;
sphere_radius3 = Smallnumber * 3.14 * 3.14;
rectangle_measurements = Bignumber * Mednumber * Smallnumber;
System.out.println();
System.out.println();
//results 2
System.out.println(" Your addition answer would be " + addition);
System.out.println(" Your subtraction answer would be " + subtraction);
System.out.println(" Your multiplcation answer would be " + multiplcation);
System.out.println(" Your division answer would be " + division);
System.out.println(" Your first sphere answer would be " + sphere_radius1);
System.out.println(" Your second sphere answer would be " + sphere_radius2);
System.out.println(" Your third sphere answer would be " + sphere_radius3);
System.out.println(" Your recangle size would be " + rectangle_measurements+ " in cubic Feet");
System.out.println();
System.out.println();
System.out.println("Would you like to Play again ? ");
System.out.println("Y for yes, & N for no " );
input = keyboard.nextLine();
repeat = input.charAt(0);
while (repeat == 'Y');
System.out.println();
while (repeat == 'N');
System.out.println();
System.out.println("Goodbye!");
}
}
You have semicolons after your while loops:
while (repeat == 'Y'); // <-- Right here
System.out.println();
If you remove it, you should get an infinite loop if repeat == 'Y' is true.
The same goes for the other loop. Just make sure you use braces around the code that you want to loop over:
while (repeat == 'Y')
System.out.println();
while (repeat == 'N') {
System.out.println();
System.out.println("Goodbye!");
}
If you want to use the loop to play the game again, I would recommend using a do/while loop, since you want to play at least once:
do {
System.out.println("Welcome to Math Island :D ! ");
System.out.println("We will use some numbers for our Formula ! ");
System.out.println("1 rule, NO DECIMALS !! ");
System.out.println("Please Pick a # from 1 to 100 NOW!! ");
Bignumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 20 NOW!! ");
Mednumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 5 NOW!! ");
Smallnumber = sc.nextInt();
//Results
addition = Bignumber + Mednumber + Smallnumber;
subtraction = Bignumber - Mednumber - Smallnumber;
multiplcation = Bignumber * Mednumber * Smallnumber;
division = Bignumber / Mednumber / Smallnumber;
sphere_radius1 = Bignumber * 3.14 * 3.14;
sphere_radius2 = Mednumber * 3.14 * 3.14;
sphere_radius3 = Smallnumber * 3.14 * 3.14;
rectangle_measurements = Bignumber * Mednumber * Smallnumber;
System.out.println();
System.out.println();
//results 2
System.out.println(" Your addition answer would be " + addition);
System.out.println(" Your subtraction answer would be " + subtraction);
System.out.println(" Your multiplcation answer would be " + multiplcation);
System.out.println(" Your division answer would be " + division);
System.out.println(" Your first sphere answer would be " + sphere_radius1);
System.out.println(" Your second sphere answer would be " + sphere_radius2);
System.out.println(" Your third sphere answer would be " + sphere_radius3);
System.out.println(" Your recangle size would be " + rectangle_measurements+ " in cubic Feet");
System.out.println();
System.out.println();
System.out.println("Would you like to Play again ? ");
System.out.println("Y for yes, & N for no " );
input = keyboard.nextLine();
repeat = input.charAt(0);
} while (repeat == 'y');
System.out.println("Goodbye!");*
encase the entire code in a while loop
define repeat as a boolean before the while loop- set it as True
then replace
while (repeat == 'Y') {
System.out.println();
}
while (repeat == 'N') {
System.out.println();
System.out.println("Goodbye!");
}
with an if statement that checks if the user input is "n", in which case change repeat to False. Otherwise keep repeat as True.
Also- lots of syntax errors in the code- clean those up and you should be fine
Take all the code you want to repeat and put it inside of its own function:
void myMathGame() {
//code for one round: setup, get user input, calculate values, print them
}
Then simply construct an unconditional infinite loop inside of your main function which you break if the user doesn't want another round:
public static void main(String[] args) {
while (true) {
myMathGame();
System.out.println("Would you like to Play again? (Y/N) ");
if (keyboard.nextLine().charAt(0) != "Y") {
break;
}
}
}