Мне нужна помощь в этом, я изучаю Jave, создав простой калькулятор продаж. У меня есть раздел в пределах этого кода следующим образом:Java: Сохранение выбранного значения для последующего использования
System.out.println ("Please make a selection:");
System.out.println ("1. Octopus Tentacles, 2. Rat Droppings");
System.out.println ("3. Sharks teeth, 4. Mountain Oysters");
System.out.println (" ");
// Since we don't have a primary database to pull from we use a dummy DB
while (foodCount <= 1)
{
System.out.print("Enter selection (1 , 2, 3, 4): ");
result = input.nextInt();
if (result == 1)
System.out.println("You selected Octopus tentacles, price $4.99");
else if (result == 2)
System.out.println("You selected Rat droppings, price $3.49");
else if (result == 3)
System.out.println("You selected Sharks teeth, price $6.73");
else if (result == 4)
System.out.println("You selected Mountain Oysters, price $9.95");
break;
}
System.out.println (" ");
позволяет выбор мне сделало, что я хочу сделать, это иметь, что значение, сохраненное для использования в разделе Alittle ввода Цены ниже этого. Может кто-то мне помочь, пожалуйста? Я новичок в Java и изучаю свой путь через собственные прогулки по книгам, которые ищут в Интернете, но не нашел ничего, что поможет мне в этом случае. Любая помощь с этим будет оценена по достоинству.
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.Scanner;
import java.math.BigDecimal;
import java.text.NumberFormat;
class priceCalc {
public static void main (String[] args) throws IOException {
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
Scanner input = new Scanner(System.in);
int Quantity;
int result = 0;
int foodCount = 1;
double Price, Subtotal, SalesTax, TotalSale;
NumberFormat curForm = NumberFormat.getCurrencyInstance();
// Here is a simple display of available products
System.out.println (" ");
System.out.println ("Welcome to the IP2 part 2!");
System.out.println (" ");
System.out.println ("Please make a selection:");
System.out.println ("1. Octopus Tentacles, 2. Rat Droppings");
System.out.println ("3. Sharks teeth, 4. Mountain Oysters");
System.out.println (" ");
// Since we don't have a primary database to pull from we use a dummy DB
while (foodCount <= 1)
{
System.out.print("Enter selection (1 , 2, 3, 4): ");
result = input.nextInt();
if (result == 1)
System.out.println("You selected Octopus tentacles, price $4.99");
else if (result == 2)
System.out.println("You selected Rat droppings, price $3.49");
else if (result == 3)
System.out.println("You selected Sharks teeth, price $6.73");
else if (result == 4)
System.out.println("You selected Mountain Oysters, price $9.95");
break;
}
System.out.println (" ");
// Create Scanner for input
Scanner key = new Scanner(System.in);
//Quantity
System.out.print ("Please enter quantity: ");
Quantity = key.nextInt();
System.out.println (" ");
//Price
System.out.print ("Please input price (price amount w/no $): ");
Price = key.nextDouble();
System.out.println (" ");
//Subtotal
Subtotal = Price * Quantity;
System.out.println ("Your subtotal is: " +curForm.format(Subtotal));
//Sales Tax
SalesTax = Subtotal * .065;
System.out.println ("Your sales tax is: " +curForm.format(SalesTax));
//Total Sale
TotalSale = Subtotal+SalesTax;
System.out.println ("Your total sale price is: " +curForm.format(TotalSale));
System.out.println (" ");
}
}
Я до сих пор не понимаю, где проблема –
Theres не проблема с кодом, как он работает, он прекрасно работает. Проблема в том, что я хочу, чтобы цена была автоматизированной, поэтому, когда я делаю выбор выше 1, 2, 3, 4, цена будет сохранена и вызывается как вход для цены ниже после того, как добавится количество добавленных количества. – Powercat