Java 有没有办法用文字代替数字作为输出?
我想用文字代替打字。这是为那些想在菜单上点食物的人准备的。 这是我的输出:Java 有没有办法用文字代替数字作为输出?,java,Java,我想用文字代替打字。这是为那些想在菜单上点食物的人准备的。 这是我的输出: 2 Good Salad $7.00 3 Soda $2.00 5 我只想输入好的沙拉,然后转到下一行,以此类推。我该如何解决这个问题? 以下是代码的其余部分: import java.util.Scanner; import java.text.DecimalFormat; public class Project1 { public static void main(String[] args) {
2
Good Salad $7.00
3
Soda $2.00
5
我只想输入好的沙拉,然后转到下一行,以此类推。我该如何解决这个问题?
以下是代码的其余部分:
import java.util.Scanner;
import java.text.DecimalFormat;
public class Project1 {
public static void main(String[] args) {
int choice = 0;
boolean doneOrdering = false;
boolean yes = false;
String order, A, B;
Scanner Keyboard = new Scanner(System.in);
DecimalFormat moneyFormat = new DecimalFormat("$#,###.00");
double num, GoodBurger = 0, GoodSalad = 0, Soda = 0, KidsMeal = 0;
double GoodBurgers = 7.75;
double GoodSalads = 7.00;
double Sodas = 2.00;
double KidsMeals = 3.00;
double tax;
double subtotal = 0, total;
int C = 0;
double tip = 0.010;
final double salestax = 0.081;
System.out.println("Welcome to Good Burger!");
System.out.println("=======================");
System.out
.println("Place your orders and type 'Finish' when you are done");
System.out
.println("--------------------------------------------------------");
System.out.println("1. GoodBurger $8.00");
System.out.println("2. GoodSalad $ 7.00");
System.out.println("3. Soda $2.00");
System.out.println("4. KidsMeal $3.00");
System.out.println("Type '5' if you want to tip. \n");
System.out.println("What would you like?");
while (!doneOrdering) {
choice = Keyboard.nextInt();
if (choice == 1) {
System.out.println("GoodBurger $8.00");
subtotal = subtotal + 8.00;
} else if (choice == 2) {
System.out.println("Good Salad $7.00");
subtotal = subtotal += 7.00;
} else if (choice == 3) {
System.out.println("Soda $2.00");
subtotal = subtotal + 2.00;
} else if (choice == 4) {
System.out.println("KidsMeal $3.00");
subtotal = subtotal + 3.00;
} else if (choice == 5) {
doneOrdering = true;
System.out.println("Do you want to tip?");
A = Keyboard.next();
if (A.equalsIgnoreCase("yes")) {
System.out.print("What percentage would you like to tip? ");
C = Keyboard.nextInt();
double tiptotal = C * tip * subtotal;
double taxtotal = subtotal * salestax;
System.out.println("SubTotal $" + subtotal);
System.out.println("Tax " + salestax);
System.out.println("Tip $" + tiptotal);
System.out.println("-------------------------");
System.out.println(subtotal + tiptotal + taxtotal);
}
if (A.equalsIgnoreCase("no")) {
double tiptotal = C * tip * subtotal;
double taxtotal = subtotal * salestax;
System.out.println("SubTotal $" + subtotal);
System.out.println("Tax " + salestax);
System.out.println(subtotal + tiptotal + taxtotal);
}
} else
System.out.println("Invalid");
}
}
}
本例使用HashMap记录价格:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Project1 {
private static String userMessage() {
StringBuffer buffer = new StringBuffer();
String lineSeparator = System.getProperty("line.separator");
buffer.append("Welcome to Good Burger!");
buffer.append(lineSeparator);
buffer.append("=======================");
buffer.append(lineSeparator);
buffer.append("Place your orders and type 'Finish' when you are done");
buffer.append(lineSeparator);
buffer.append("--------------------------------------------------------");
buffer.append(lineSeparator);
buffer.append("1. GoodBurger $8.00");
buffer.append(lineSeparator);
buffer.append("2. GoodSalad $ 7.00");
buffer.append(lineSeparator);
buffer.append("3. Soda $2.00");
buffer.append(lineSeparator);
buffer.append("4. KidsMeal $3.00");
buffer.append(lineSeparator);
buffer.append("Type '5' if you want to tip. \n");
buffer.append(lineSeparator);
buffer.append("What would you like?");
buffer.append(lineSeparator);
return buffer.toString();
}
private static Map<String, Double> getPrices() {
Map<String, Double> prices = new HashMap<String, Double>();
prices.put("GoodBurgers", 7.75);
prices.put("GoodSalads", 7.00);
prices.put("Sodas", 2.00);
prices.put("KidsMeals", 3.00);
return prices;
}
public static void main(String[] args) {
String choice;
boolean doneOrdering = false;
String tipConfirmation;
Scanner Keyboard = new Scanner(System.in);
double subtotal = 0;
int C = 0;
double tip = 0.010;
final double salestax = 0.081;
String userMessage = userMessage();
System.out.println(userMessage);
Map<String, Double> prices = getPrices();
while (!doneOrdering) {
choice = Keyboard.next();
if (prices.containsKey(choice)) {
double price = prices.get(choice);
System.out.println(choice + " " + price);
subtotal = subtotal + price;
} else if (choice == "Tip") {
doneOrdering = true;
System.out.println("Do you want to tip?");
tipConfirmation = Keyboard.next();
if (tipConfirmation.equalsIgnoreCase("yes")) {
System.out.print("What percentage would you like to tip? ");
C = Keyboard.nextInt();
double tiptotal = C * tip * subtotal;
double taxtotal = subtotal * salestax;
System.out.println("SubTotal $" + subtotal);
System.out.println("Tax " + salestax);
System.out.println("Tip $" + tiptotal);
System.out.println("-------------------------");
System.out.println(subtotal + tiptotal + taxtotal);
}
if (tipConfirmation.equalsIgnoreCase("no")) {
double tiptotal = C * tip * subtotal;
double taxtotal = subtotal * salestax;
System.out.println("SubTotal $" + subtotal);
System.out.println("Tax " + salestax);
System.out.println(subtotal + tiptotal + taxtotal);
}
} else
System.out.println("Invalid");
}
}
}
因此,一种技术是将要使用的字符串存储在HashMap中,然后查找它们以获得所需的其余信息。例如:
static String[] foods = { "Good Burger", "Good Salad", "Kid's Meal", "Drink" };
static double[] costs = { 8.0, 7.0, 3.0, 0.75 };
HashMap<String, FoodItem> itemLookup = new HashMap<>();
{
for( int i = 0; i < foods.length; i++ ) {
itemLookup.put( foods[i], new FoodItem( foods[i], costs[i] ) );
}
}
在这里,我添加了一个trim来消除任何空白,这将导致找不到字符串,因为HashMap只返回精确的匹配
这是主要的想法。下面有点粗糙,但我试过一次,给你一个如何组合的想法
class FoodMenu {
class FoodItem {
public FoodItem( String name, double cost )
{
this.name = name;
this.cost = cost;
}
String name;
double cost;
@Override
public String toString()
{
return "FoodItem{" + "name=" + name + ", cost=" + cost + '}';
}
}
static String[] foods = { "Good Burger", "Good Salad", "Kid's Meal", "Drink" };
static double[] costs = { 8.0, 7.0, 3.0, 0.75 };
HashMap<String, FoodItem> itemLookup = new HashMap<>();
{
for( int i = 0; i < foods.length; i++ ) {
itemLookup.put( foods[i], new FoodItem( foods[i], costs[i] ) );
}
}
List<FoodItem> process( Reader input ) throws IOException {
BufferedReader bin = new BufferedReader( input );
ArrayList<FoodItem> order = new ArrayList<>();
for( String s; (s = bin.readLine()) != null && s.length() > 0; ) {
FoodItem item = itemLookup.get( s.trim() );
if( item == null ) System.out.println("I couldn't find a "+ s );
else order.add(item);
}
return order;
}
public static void main(String[] args) throws IOException {
System.out.println("Enter a food item name to order it.");
System.out.println("Items available: "+ Arrays.toString( foods ) );
System.out.println("Press enter on a blank line to finish");
List<FoodItem> order = new FoodMenu().process( new InputStreamReader( System.in ));
System.out.println("You ordered: "+order );
}
}
你可以,但这需要更多的工作。更多的工作,取决于你想要它有多健壮。哦,好吧。这个怎么样:我怎么能把1和goodburger放在同一行上,等等?好吧,在顶端,你会得到一个叫做自然语言解析的东西。它经常在研究生级别的课程中学习。我会试着想出一些简单一点的办法。
class FoodMenu {
class FoodItem {
public FoodItem( String name, double cost )
{
this.name = name;
this.cost = cost;
}
String name;
double cost;
@Override
public String toString()
{
return "FoodItem{" + "name=" + name + ", cost=" + cost + '}';
}
}
static String[] foods = { "Good Burger", "Good Salad", "Kid's Meal", "Drink" };
static double[] costs = { 8.0, 7.0, 3.0, 0.75 };
HashMap<String, FoodItem> itemLookup = new HashMap<>();
{
for( int i = 0; i < foods.length; i++ ) {
itemLookup.put( foods[i], new FoodItem( foods[i], costs[i] ) );
}
}
List<FoodItem> process( Reader input ) throws IOException {
BufferedReader bin = new BufferedReader( input );
ArrayList<FoodItem> order = new ArrayList<>();
for( String s; (s = bin.readLine()) != null && s.length() > 0; ) {
FoodItem item = itemLookup.get( s.trim() );
if( item == null ) System.out.println("I couldn't find a "+ s );
else order.add(item);
}
return order;
}
public static void main(String[] args) throws IOException {
System.out.println("Enter a food item name to order it.");
System.out.println("Items available: "+ Arrays.toString( foods ) );
System.out.println("Press enter on a blank line to finish");
List<FoodItem> order = new FoodMenu().process( new InputStreamReader( System.in ));
System.out.println("You ordered: "+order );
}
}