Hi im so close to finishing my code and i just need help tweaking the last bits - beginner level

Basically im using if and else statements... etc.. and when i run my code it works how i want it to... it says what flavour of ice do you want then you input vanilla, chocolate or strawberry.... now heres where my code is slightly off on what i want it to do.... when i put a different flavour it should finish with we dont have that flavour but instead it goes onto the next line of code which is how many scoops do you want and when i put the no. of scoops then it says we dont have that flavour but i want it to say we dont have that flavour before it runs to the next line of code.
No description
No description
57 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @StremesJ! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ponchisao326
ponchisao3263w ago
you are missing a "}" btw, you're making some strange things if scoops < 0 would be the "We don't sell just a cone"
ponchisao326
ponchisao3263w ago
I made it on Java, it should give you some clues of what's happening
No description
ponchisao326
ponchisao3263w ago
btw I've changed some things for performance upgrades like changing double to Float and removing unnecessary println lines also changed the ifs & else if with a switch for better performance you can change this to the beginning:
if (scoops < 0) {
System.out.println("We don't sell just a cone");
} else if (scoops > 3) {
System.out.println("That's too many scoops to fit in a cone");
System.exit(0);
}
if (scoops < 0) {
System.out.println("We don't sell just a cone");
} else if (scoops > 3) {
System.out.println("That's too many scoops to fit in a cone");
System.exit(0);
}
Hope It helps ^^!! Anything else, just say it @StremesJ
StremesJ
StremesJOP3w ago
thanks so much
JavaBot
JavaBot3w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
ponchisao326
ponchisao3263w ago
np, anything else?
StremesJ
StremesJOP3w ago
thats all for now
ponchisao326
ponchisao3263w ago
perfect ^^
JavaBot
JavaBot3w ago
JavaBot#9523
Recent Warns
<@743072402702860358> has 0 active warns with a total of 0 severity.
StremesJ
StremesJOP3w ago
im so sorry. If you are busy then no worries but actually... im wondering you said you used switch case instead but if i wanted to do it with the if and else way would you be able to show me how it would be done if thats not too much trouble?
ponchisao326
ponchisao3263w ago
Yeah, I'm actually not on the Pc but it's simple
StremesJ
StremesJOP3w ago
ok thanks
ponchisao326
ponchisao3263w ago
You know what? On my way to the Pc 🙂
StremesJ
StremesJOP3w ago
😄
ponchisao326
ponchisao3263w ago
I hate to write on my phone
StremesJ
StremesJOP3w ago
thats so fair its tedious im new to java btw so im trying to learn stuff
ponchisao326
ponchisao3263w ago
yeah, for the way you typed your code I gessed
StremesJ
StremesJOP3w ago
lol
StremesJ
StremesJOP3w ago
but yh basically just need this to work
No description
ponchisao326
ponchisao3263w ago
Should be smth like this
No description
StremesJ
StremesJOP3w ago
so you define all the variables within a print statement? why is that wait nvm also if can you do it without using floats?
ponchisao326
ponchisao3263w ago
some recommendations. 1. You are using few decimals so change double to float, this is because double take much more space than float. 2. remove the Println on every if statement, just change the variable, u're gonna make the program run less code lines
StremesJ
StremesJOP3w ago
ok
ponchisao326
ponchisao3263w ago
you can, just replace the float with double and on the vars
private static float v = 0.10F;
private static float c = 0.53F;
private static float s = 0.00F;
private static float cone = 1.00F;
private static float v = 0.10F;
private static float c = 0.53F;
private static float s = 0.00F;
private static float cone = 1.00F;
replace the "F" with a "D"
StremesJ
StremesJOP3w ago
ok have you used jshell ?
ponchisao326
ponchisao3263w ago
nope, I used JAVA itself, but it shouldn't be that hard, it's very similar
StremesJ
StremesJOP3w ago
yh ok
ponchisao326
ponchisao3263w ago
can you pass me your jsh code pls? not an screenshot the code itself
StremesJ
StremesJOP3w ago
ok what the file
ponchisao326
ponchisao3263w ago
just the file
StremesJ
StremesJOP3w ago
btw since im doing beginner level task i dont get the why you put X: stuff and such and such or anoObjects?
ponchisao326
ponchisao3263w ago
do you mean in the println?
StremesJ
StremesJOP3w ago
yh well ik why you use println but theres the X:
ponchisao326
ponchisao3263w ago
thats because I have the Java extension pack on Visual Studio Code, that's what the system put but you don't have to put it
StremesJ
StremesJOP3w ago
ok thanks
ponchisao326
ponchisao3263w ago
import java.util.Scanner;

public class Flavours {

private static float v = 0.10F;
private static float c = 0.53F;
private static float s = 0.00F;
private static float cone = 1.00F;
private static String flavour;
private static int scoops;
private static float total;

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.println("v = vanilla, c = chocolate, s = strawberry");
flavour = keyboard.nextLine();

System.out.println("How many scoops?");
scoops = keyboard.nextInt();

if (flavour.toLowerCase().equals("v")) {
total = cone + (v * scoops);
} else if (flavour.toLowerCase().equals("c")) {
total = cone + (c * scoops);
} else if (flavour.toLowerCase().equals("s")) {
total = cone + (s * scoops);
} else {
System.out.println("We don't have that flavour");
System.exit(0);
}


if (scoops < 0) {
System.out.println("We don't sell just a cone");
} else if (scoops > 3) {
System.out.println("That's too many scoops to fit in a cone");
System.exit(0);
}

System.out.println("That will be " + total + " please");

keyboard.close();
}

}
import java.util.Scanner;

public class Flavours {

private static float v = 0.10F;
private static float c = 0.53F;
private static float s = 0.00F;
private static float cone = 1.00F;
private static String flavour;
private static int scoops;
private static float total;

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

System.out.println("v = vanilla, c = chocolate, s = strawberry");
flavour = keyboard.nextLine();

System.out.println("How many scoops?");
scoops = keyboard.nextInt();

if (flavour.toLowerCase().equals("v")) {
total = cone + (v * scoops);
} else if (flavour.toLowerCase().equals("c")) {
total = cone + (c * scoops);
} else if (flavour.toLowerCase().equals("s")) {
total = cone + (s * scoops);
} else {
System.out.println("We don't have that flavour");
System.exit(0);
}


if (scoops < 0) {
System.out.println("We don't sell just a cone");
} else if (scoops > 3) {
System.out.println("That's too many scoops to fit in a cone");
System.exit(0);
}

System.out.println("That will be " + total + " please");

keyboard.close();
}

}
look this is the raw code
StremesJ
StremesJOP3w ago
kk the other thing is jshell will shutdown with System.exit(0) is there a way to do it without jshell shutting down so it prints thats too many scoops to fit in a cone without it shutting as well
ponchisao326
ponchisao3263w ago
double v = 0.10;
double c = 0.53;
double s = 0.00;
double cone = 1.00;

Scanner scanner = new Scanner(System.in);

System.out.println("Would you like (v)anilla, (c)hocolate or (s)trawberry?");
String flavour = scanner.nextLine();

System.out.println("How many scoops would you like?");
int scoops = scanner.nextInt();

if (scoops < 0) {
System.out.println("We don't sell just a cone.");
} else if (scoops > 3) {
System.out.println("That's too many scoops!");
} else {

double totalcost = 0;
if (flavour.equals("v")) {
totalcost = cone + (v * scoops);
} else if (flavour.equals("c")) {
totalcost = cone + (c * scoops);
} else if (flavour.equals("s")) {
totalcost = cone + (s * scoops);
} else {
System.out.println("We don't have that flavour.");
}

if (totalcost > 0) {
System.out.println("That will be " + totalcost + " please.");
}
}

scanner.close();
double v = 0.10;
double c = 0.53;
double s = 0.00;
double cone = 1.00;

Scanner scanner = new Scanner(System.in);

System.out.println("Would you like (v)anilla, (c)hocolate or (s)trawberry?");
String flavour = scanner.nextLine();

System.out.println("How many scoops would you like?");
int scoops = scanner.nextInt();

if (scoops < 0) {
System.out.println("We don't sell just a cone.");
} else if (scoops > 3) {
System.out.println("That's too many scoops!");
} else {

double totalcost = 0;
if (flavour.equals("v")) {
totalcost = cone + (v * scoops);
} else if (flavour.equals("c")) {
totalcost = cone + (c * scoops);
} else if (flavour.equals("s")) {
totalcost = cone + (s * scoops);
} else {
System.out.println("We don't have that flavour.");
}

if (totalcost > 0) {
System.out.println("That will be " + totalcost + " please.");
}
}

scanner.close();
I think this should work
StremesJ
StremesJOP3w ago
ill test it in jshell gimme a min
StremesJ
StremesJOP3w ago
No description
StremesJ
StremesJOP3w ago
it didnt print the thats not a flavour mssg ?
ponchisao326
ponchisao3263w ago
yeah, but it would say it after you enter the scoops amount
StremesJ
StremesJOP3w ago
i hate to be a pain is there any way to do it before and is it quick fix or pain in the ass fix ?
ponchisao326
ponchisao3263w ago
I mean, you can, it's easy but its gross haha, let me change it
StremesJ
StremesJOP3w ago
oki ty
ponchisao326
ponchisao3263w ago
double v = 0.10;
double c = 0.53;
double s = 0.00;
double cone = 1.00;

Scanner scanner = new Scanner(System.in);

System.out.println("Would you like (v)anilla, (c)hocolate or (s)trawberry?");
String flavour = scanner.nextLine();

if (!flavour.equals("v") && !flavour.equals("c") && !flavour.equals("s")) {
System.out.println("We don't have that flavour.");
System.exit(0);
}

System.out.println("How many scoops would you like?");
int scoops = scanner.nextInt();

if (scoops < 0) {
System.out.println("We don't sell just a cone.");
} else if (scoops > 3) {
System.out.println("That's too many scoops!");
} else {

double totalcost = 0;
if (flavour.equals("v")) {
totalcost = cone + (v * scoops);
} else if (flavour.equals("c")) {
totalcost = cone + (c * scoops);
} else if (flavour.equals("s")) {
totalcost = cone + (s * scoops);
}

if (totalcost > 0) {
System.out.println("That will be " + totalcost + " please.");
}
}
}

scanner.close();
double v = 0.10;
double c = 0.53;
double s = 0.00;
double cone = 1.00;

Scanner scanner = new Scanner(System.in);

System.out.println("Would you like (v)anilla, (c)hocolate or (s)trawberry?");
String flavour = scanner.nextLine();

if (!flavour.equals("v") && !flavour.equals("c") && !flavour.equals("s")) {
System.out.println("We don't have that flavour.");
System.exit(0);
}

System.out.println("How many scoops would you like?");
int scoops = scanner.nextInt();

if (scoops < 0) {
System.out.println("We don't sell just a cone.");
} else if (scoops > 3) {
System.out.println("That's too many scoops!");
} else {

double totalcost = 0;
if (flavour.equals("v")) {
totalcost = cone + (v * scoops);
} else if (flavour.equals("c")) {
totalcost = cone + (c * scoops);
} else if (flavour.equals("s")) {
totalcost = cone + (s * scoops);
}

if (totalcost > 0) {
System.out.println("That will be " + totalcost + " please.");
}
}
}

scanner.close();
I think it should work I haven't tryed myself cause I don't have JShell 🙃 let me know if it works
StremesJ
StremesJOP3w ago
will do give me justa min ok 2 things when i put how many scoops as 0 it printed 1.0 instead of you cant just have to cone - second thing is more just out of interest: when using System.exit(0); that causes jshell to crash it does print the mssg - does return: print the mssg and keep it open or will it not work but i appreciate this so much
ponchisao326
ponchisao3263w ago
1. If you're typing "0" on the scoops is gonna give you an error because you just put < 0 instead of <= 0, so you're not including the 0. 2. It should close the program so, I think it's doing it's job
StremesJ
StremesJOP3w ago
ok cool Look ponchisao thanks so much for the help
JavaBot
JavaBot3w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
StremesJ
StremesJOP3w ago
😄
ponchisao326
ponchisao3263w ago
np, hope you've resolved your doubts ^^
StremesJ
StremesJOP3w ago
indeed 👍
ponchisao326
ponchisao3263w ago
😃
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server