I'm making a vending machine program and I don't know how to use the users choice in my for loop because it gives me an error when i put choice in the loop.
public class PopGenerator {
double price[] = {2.49, 1.25, 3.49, 3.25, 2,25, 1.30, 3.40, 3.49, 2.50, 3.00};
public void beveragechoice()
{
Scanner c = new Scanner(System.in);
int choice = c.nextInt();
double price[] = {2.49, 1.25, 3.49, 3.25, 2.25, 1.30, 3.40, 3.49, 2.50, 3.00};
switch(choice)
{
case 1:
System.out.println("This beverage costs $" + price[0]);
break;
case 2:
System.out.println("This beverage costs $" + price[1]);
break;
case 3:
System.out.println("This beverage costs $" + price[2]);
break;
case 4:
System.out.println("This beverage costs $" + price[3]);
break;
case 5:
System.out.println("This beverage costs $" + price[4]);
break;
case 6:
System.out.println("This beverage costs $" + price[5]);
break;
case 7:
System.out.println("This beverage costs $" + price[6]);
break;
case 8:
System.out.println("This beverage costs $" + price[7]);
break;
case 9:
System.out.println("This beverage costs $" + price[8]);
break;
case 10:
System.out.println("This beverage costs $" + price[9]);
}
}
public void change()
{
System.out.println("Enter money put into the machine: ");
Scanner m = new Scanner(System.in);
int money = m.nextInt();
for(int x = choice; x >= 0 ; x--)
if (money == price[x])
{
System.out.println("No change.");
}
else
{
System.out.print("Your change is: ");
System.out.print(money - price[x]);
}
}
}
You can simplify both of your methods to avoid having multiple Scanners or pass variables through them, like this:
public void beveragechoice() {
double prices[] = {2.49, 1.25, 3.49, 3.25, 2,25, 1.30, 3.40, 3.49, 2.50, 3.00};
System.out.println("Select one beverage");
Scanner c = new Scanner(System.in);
int choice = c.nextInt();
if (choice > 0 && choice <= prices.length) {
double p = prices[choice - 1];
System.out.println("This beverage costs $" + p);
System.out.println("Enter money put into the machine: ");
double money = c.nextDouble();
if (money == p) {
System.out.println("No change.");
} else {
System.out.println("Your change is: $" + (money - p));
}
}
}
Related
I am trying to program a menu for my calculator. I do not know how to get out of my inner loop and get back to my outer loop once I have executed the inner loop. I have to finish the whole case of the inner loop before I can get back to the outer loop. Is there a way that I can go back and forth of the nested switch whenever I want to? and when I execute my outer switch case 3, it executes infinitely. Maybe because it's inside a loop.
import java.util.Scanner;
public class MainInterface {
static Scanner input = new Scanner(System.in);
static boolean hasRun = false;
public static void main(String args[])
{
Calculator Mycal = new SimpleCalculator();
ScientificCalculator Mycal2 = new ScientificCalculator();
mainMenu();
int choice = input.nextInt();
do {
System.out.println("\n");
switch(choice) // outer switch
{
case 1: simpleCalculatorMenu();
int choice2 = input.nextInt();
switch(choice2) // inner switch
{
case 1: //ADDITION
System.out.println("ADDITION");
System.out.println("\n");
System.out.println("Enter first number: ");
float x = input.nextFloat();
System.out.println("Enter second number: ");
float y = input.nextFloat();
System.out.println("\n");
System.out.println("The sum of " + x + " " + y + " is " + Mycal.add(x, y));
System.out.println("\n");
System.out.println("Would you like to return to the main menu?");
System.out.println("\n");
System.out.println("Enter 1. to return 2. to exit");
choice = input.nextInt();
break;
}
break;
case 2: System.out.println("SCIENTIFIC CALCULATOR");
System.out.println("\n");
System.out.println("1. power(x, pow) 2. sin(xDeg) 3. cos(xDeg) 4. tan(xDeg)");
System.out.println("\n");
System.out.println("5. pi() 6. fact(x) ");
int choice3 = input.nextInt();
switch(choice3) {
case 1:
System.out.println("POWER");
System.out.println("\n");
System.out.println("Enter first number: ");
double x = input.nextDouble();
System.out.println("Enter second number: ");
double y = input.nextDouble();
System.out.println("\n");
System.out.println("The power of " + x + " to the power of " + y + " is " + Mycal2.power(x, y));
System.out.println("\n");
System.out.println("Would you like to return to the main menu?");
System.out.println("\n");
System.out.println("Enter 1. to return 2. to exit");
choice = input.nextInt();
break;
case 2:
}
break;
case 3: mainMenu();
break;
case 4: System.exit(choice);
break;
}
} while(choice != 0);
}
public static void simpleCalculatorMenu () {
System.out.println("SIMPLE CALCULATOR");
System.out.println("\n");
System.out.println("1. Addition 2. Subtraction 3. Multiplication 4. Division");
System.out.println("\n");
System.out.println("5. Squared Root 6. Squared 7. Cube 8. Discount ");
}
public static void mainMenu () {
System.out.println("What calculator do you want to use?");
System.out.println("\n");
System.out.println("1. Simple Calculator \t 2. Scientific Calculator");
}
}
by adding label you can try like this
OUTER:
switch(condition1) {
case x:
switch(condition2) {
case y:
System.out.println("hello");
break OUTER;
}
}
Here's A Java Calculator Program I Just Made Recently, But It Doesn't Meet My Expectations! I Want It In A More Convenient Way Like It Has 6 Classes And Some Exclamation Marks, I Wanna Get A+ So Please Help Me!
1) Can I loop the codes so after displaying the answer, It runs the code again?
2) Can I somehow decrease the number of classes and the length of codes?
3) Can I clear screen in the console like in C++, So it should display a separate view for the Intro and the answer?
Here's The Code:
import java.util.Scanner;
public class javaCalc {
public static void welcome() {
System.out.println("Welcome to Calculator.java v0.1");
System.out.println("(Developed By RAZ0229)");
}
public static void main(String[] args) {
welcome();
System.out.flush();
System.out.println("\n1) Addition");
System.out.println("2) Substraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("\nChoose A Basic Operator:");
Scanner operandOne = new Scanner(System.in);
int inpOperation = operandOne.nextInt();
switch(inpOperation) {
case 1: additionMethod();
break;
case 2: substractionMethod();
break;
case 3: multiplicationMethod();
break;
case 4: divisionMethod();
break;
default: System.out.println("\n(Invalid Argument)");
return;
}
}
public static void additionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne + numTwo;
System.out.println(numOne + " + " + numTwo + " = " + answer);
}
public static void substractionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne - numTwo;
System.out.println(numOne + " - " + numTwo + " = " + answer);
}
public static void multiplicationMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne * numTwo;
System.out.println(numOne + " x " + numTwo + " = " + answer);
}
public static void divisionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne / numTwo;
System.out.println(numOne + " / " + numTwo + " = " + answer);
}
}
You are asking for two floats in every method and using the same prints many times, so you can just create some method such as this and call it inside your operation method to stop repeating code (constantly repeated blocks of code is a strong indicator that the block can probably be abstracted into its own method):
public static float[] getValues(){
float[] values;
/*Implement your logic here asking user for floats, then put into above array
and do calculations in your methods using float array*/
return values;
}
You can also loop your main by wrapping it in a while loop and adding an extra case to your switch statement like so (if you would like to exit program, enter 5):
public static void main(String[] args) {
welcome();
while (true){
System.out.flush();
System.out.println("\n1) Addition");
System.out.println("2) Substraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Quit");
System.out.println("\nChoose A Basic Operator:");
Scanner operandOne = new Scanner(System.in);
int inpOperation = operandOne.nextInt();
switch(inpOperation) {
case 1: additionMethod();
break;
case 2: substractionMethod();
break;
case 3: multiplicationMethod();
break;
case 4: divisionMethod();
break;
case 5: System.exit(0);
default: System.out.println("\n(Invalid Argument)");
return;
}
}
}
I'm struggling to understand why intAt will not work in this program. The goal of program is to simply convert weights for a specific planet.
package weightonotherplanets;
import java.util.Scanner;
public class WeightonOtherPlanets
{
public static void main(String args[]){
System.out.println("What is your weight on the Earth?");
Scanner weightInput = new Scanner(System.in); // Enter your weight
int weight = weightInput.nextInt();
System.out.println("1. Voltar\n2. Krypton\n3. Fertos\n4. Servontos\n"); // Choice of planets
System.out.println(" Selection?");
Scanner selectionChoice = new Scanner(System.in);
int selection = selectionChoice.nextInt();
int select = selection.intAt(0); // This is the problem in the code
switch (select)
{
case '1':
System.out.println("Your weight on Voltor would be " + weight * 0.091);
break;
case '2':
System.out.println("Your weight on Krypton would be " + weight * 0.720);
break;
case '3':
System.out.println("Your weight on Fertos would be " + weight * 0.865);
break;
case '4':
System.out.println("Your weight on Servontos would be " + weight * 4.612);
break;
default:
System.out.println("Please make a selection.");
}
}
}
I want to use switch statement after an if statement but I can't and I don't know what is the problem.
public class main {
public static void main (String[] args) {
String input; //To hold user's input.
char selectPackage; //To hold Internet Package
double hourUsage, totalCharges, addCharges; //other variables
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Prompt the user to select a Internet Package.
System.out.print("Which package did you purchase? ( Enter the Package's letter)");
input = keyboard.nextLine();
selectPackage = input.charAt(0);
System.out.print("Please select the amount of hours used.");
input = keyboard.nextLine();
hourUsage = Double.parseDouble(input);
//Display pricing for selected package...
switch (selectPackage)
{
case 'a':
case 'A':
if (hourUsage > 10)
{
addCharges = hourUsage - 10;
totalCharges = (addCharges * 2.0) + 9.95;
System.out.println("You have used " + hourUsage + " hours and your total is $" +
totalCharges + " per month. ");
}
else
System.out.println("Your total is $9.95 per month.");
break;
case 'b':
case 'B':
if (hourUsage > 20 )
{
addCharges = hourUsage - 20;
totalCharges = (addCharges * 1.0) + 13.95;
System.out.println("You have used " + hourUsage + " and your total is $" + totalCharges + " per month.");
}
else
System.out.println("Your total is $13.95 per month.");
break;
case 'c':
case 'C':
System.out.println("Your total is $19.95 per month.");
break;
default:
System.out.println("Invalid Choice. Choice A,B,C");
}
}
}
System.out.println("Your total is $19.95 per month.");
}
else
System.out.println("Your total is $19.95 per month.");
}
}
Now I want to use the switch statement for telling user that if he/she chose package "B", he would save 20 dollars.
I have had a look through your code and have made ALOT of edits and improvements, the main issue I found was your use of } in the wrong places. I believe this was because you haven't organised your code very well; in future consider organising your code to make it easier to find errors, below I have corrected your code and have put the last few lines into a comment as I'm not sure why you have them there, if there are any questions about it, just ask me:
public class Test {
public static void main(String[] args) {
char selectPackage; //To hold Internet Package
double hourUsage, totalCharges, addCharges; //other variables
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Prompt the user to select a Internet Package.
System.out.print("Which package did you purchase? ( Enter the Package's letter)");
char input = keyboard.next().charAt(0);
selectPackage = Character.toUpperCase(input);
System.out.print("Please select the amount of hours used.");
hourUsage = keyboard.nextDouble();
//Display pricing for selected package...
switch (selectPackage) {
case 'A':
if (hourUsage > 10) {
addCharges = hourUsage - 10;
totalCharges = (addCharges * 2.0) + 9.95;
System.out.println("You have used " + hourUsage + " hours and your total is $" + totalCharges + " per month. ");
}
else {
System.out.println("Your total is $9.95 per month.");
}
break;
case 'B':
if (hourUsage > 20 ) {
addCharges = hourUsage - 20;
totalCharges = (addCharges * 1.0) + 13.95;
System.out.println("You have used " + hourUsage + " and your total is $" + totalCharges + " per month.");
}
else{
System.out.println("Your total is $13.95 per month.");
}
break;
case 'C':
System.out.println("Your total is $19.95 per month.");
break;
default:
System.out.println("Invalid Choice. Choice A,B,C");
}
/**System.out.println("Your total is $19.95 per month.");
System.out.println("Your total is $19.95 per month.");
**/
}
}
Basically I want to be able to input a number that isn't an option and then be given the option to choice again and get the statement to repeat.
Scanner keyboard = new Scanner(System.in);
double weight;
int Choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune");
Choice =keyboard.nextInt();
if (Choice == 1){
System.out.println("Your weight on Venus would be " + (weight * 0.78));
}
else if (Choice == 2){
System.out.println("Your weight on Mars would be " + (weight * .39));
}
else if (Choice == 3){
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
}
else if (Choice == 4){
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
}
else if (Choice == 5){
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
}
else if (Choice == 6) {
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
}
else
System.out.println("This was not a choice, try again!");
Choice = keyboard.nextInt();
}
This is an easier way to go, using a do-while loop and a switch.
Also fixed choice
Scanner keyboard = new Scanner(System.in);
double weight;
int choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
do {
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune\n 7. Exit");
choice = keyboard.nextInt();
switch(choice) {
case 1:
System.out.println("Your weight on Venus would be " + (weight * 0.78));
break;
case 2:
System.out.println("Your weight on Mars would be " + (weight * .39));
break;
case 3:
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
break;
case 4:
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
break;
case 5:
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
break;
case 6:
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
break;
case 7:
System.out.println("Bye");
break;
default:
System.out.println("This was not a choice, try again!");
break;
}
} while (choice != 7);
You have to learn the basic in programming language... do..while and switch statements.
Here is the sample program.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double weight;
int choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
do {
System.out
.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune \n7.EXIT");
System.out.println();
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("Your weight on Venus would be "
+ (weight * 0.78));
break;
case 2:
System.out.println("Your weight on Mars would be "
+ (weight * .39));
break;
case 3:
System.out.println("Your weight on Jupiter would be "
+ (weight * 2.65));
break;
case 4:
System.out.println("Your weight on Saturn would be "
+ (weight * 1.17));
break;
case 5:
System.out.println("Your weight on Uranus would be"
+ (weight * 1.05));
break;
case 6:
System.out.println("Your weight on Neptune would be "
+ (weight * 1.23));
case 7:
System.out.println("Leaving...");
break;
default:
System.out.println("This was not a choice, try again!");
break;
}
} while (choice != 7);
}
Use the Java Naming conventions to give names to variables. Start with lowercase letter.
This can be done with a while loop. Also consider using arrays.
String[] planets = {"Mars", "Jupiter", .... };
double[] factors = {0.45, 1.5, ....};
double mass = keyboard.nextDouble();
int choice = keyboard.nextInt();
while (choice < 0 || choice >= planets.length)
{
System.out.println("Not a valid option!");
choice = keyboard.nextInt();
}
double weight = mass * factors[choice];
System.out.println("Your weight on " + planets[choice] + " would be " + weight);
So, basically, you ask it once politely. Afterwards, start shouting that it is wrong, until the user gave a proper input.