基本java读取列表字符串和其他功能
我正在努力学习java,所以这里完全是新手。 比如说,我们有一个字符串基本java读取列表字符串和其他功能,java,Java,我正在努力学习java,所以这里完全是新手。 比如说,我们有一个字符串 String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20"; 我需要做的是创建一个基于控制台的应用程序来列出所有这些项目,然后添加多个项目并显示价格 这是我尝试过的代码。我可以添加数字,但我不知道如何将用户的输入转换为变量 例如,当我运行控制台应用程序时,我希望用户输入他想要的汉堡数量。然后他选择了数字3 请温柔一点 import java.util.
String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";
我需要做的是创建一个基于控制台的应用程序来列出所有这些项目,然后添加多个项目并显示价格
这是我尝试过的代码。我可以添加数字,但我不知道如何将用户的输入转换为变量
例如,当我运行控制台应用程序时,我希望用户输入他想要的汉堡数量。然后他选择了数字3
请温柔一点
import java.util.Scanner;
import java.util.*;
public class Test {
public static void main(String[] args) {
String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";
//split
String [] elements = file.split(";");
//convert string to list of string
List<String> fixedLengthList = Arrays.asList(elements);
//copy fixed to arraylist
ArrayList<String> listOfString = new ArrayList<String>(fixedLengthList);
int p = 0;
while (p != 5) {
Scanner sc = new Scanner(System.in);
for (String line : listOfString
) {
System.out.println("1. Cheese burger 50");
System.out.println("2. Pizza 80");
System.out.println("3. View your bill");
sc.nextInt();
String[] items = line.split(",");
String itemPriceString = items[2];
double itemPrice = Double.parseDouble(itemPriceString);
double burger = itemPrice;
System.out.println(burger+burger);
}
}
}
}
也许这有助于:
Scanner sc = new Scanner(System.in);
while (true) {
// print the menu
for (String menuItem : listOfString) {
String[] item = menuItem.split(",");
System.out.println(item[0] + ". " + item[1] + " " + item[2]);
}
// read the selection
int selection = sc.nextInt();
if (selection == 3) {
// print bill
break;
}
// get information of selected item
String[] selectedItem = listOfString.get(selection);
String itemName = selectedItem[1];
int itemPrice = Integer.parseInt(selectedItem[2]);
}
很抱歉,我不能完全理解这个问题,但我想推荐一些技巧来管理捆绑包中的类似变量
与C++中的数据结构(如对和TUPE)一样,java有一种声明“类”< /P>的方法
class Menu {
int order; //for food order number
String name; // for food name,
int price; //for food price
Menu(String name, int price){
//following the 'Constructor' We can put data
this.name = name;
this.price = price;
}
}
那我们怎么用呢?
以这种方式使用列表
String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";
//split
String [] elements = file.split(";");
List<Menu> foodMenu = new ArrayList<Menu>(); //init
for(int i = 0; i< 4; i++) { //4 foods
String [] line = elements[i].split(",");
foodMenu.add(new Menu(Integer.parseInt(line[0]), //food order number
line[1], //food name
Integer.parseInt(line[2])) //food price
);
}
/*foodMenu[0] = 1, Cheese Burger, 50
foodMenu[1] = 2, Pizza, 70
foodMenu[2] = 3, Coke, 20
foodMenu[3] = 4, Beer, 20
if you want to access a data you can use like this
foodMenu.get(index).order / name / price ; */
现在,让我们尝试一下这个新的存储系统,让客户选择购买和支付什么
import java.util.Scanner;
import java.util.*;
class Menu {
int order; //for food order number
String name; // for food name
int price; //for food price
Menu(int order, String name, int price){
//following the 'Constructor' We can put data
this.order = order;
this.name = name;
this.price = price;
}
}
public class StackOver {
public static void main(String[] args) {
String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";
//split
String [] elements = file.split(";");
List<Menu> foodMenu = new ArrayList<Menu>(); //init
for(int i = 0; i< 4; i++) { //4 foods
String [] line = elements[i].split(",");
foodMenu.add(new Menu(Integer.parseInt(line[0]), //food order number
line[1], //food name
Integer.parseInt(line[2])) //food price
);
}
//foodMenu[0] = 1, Cheese Burger, 50
//foodMenu[1] = 2, Pizza, 70
//foodMenu[2] = 3, Coke, 20
//foodMenu[3] = 4, Beer, 20
//if you want to access a data you can use like this
// foodMenu.get(index).order / name / price ;
Scanner sc = new Scanner(System.in);
int price = 0; //for purchase price
int p = 0; //Great Init for avoiding NullPointerException
while (p != 5) {
System.out.println("Please Input number 1 to 5 to Order food or purchase");
System.out.println("1. Cheese burger 50$");
System.out.println("2. Pizza 80$");
System.out.println("3. Coke 20$");
System.out.println("4. Beer 20$");
System.out.println("5. Beer 20$");
p = sc.nextInt(); //customer will choose number
if(p!=5) {
price += foodMenu.get(p-1).price;
//add price of selected food!
}
}
System.out.println("Ok, The price is "+price+"$ in total");
System.out.println("Have a nice day!");
}
}
^示例结果
我希望这个答案能对你有所帮助,如果你不理解,请留下评论!
祝你有一个平静的一天 查看Scanner提供的其他方法,如next或nextLine。您可以将这些方法的返回值分配给字符串变量,如String line=sc.nextLine;我希望这对我很有帮助。非常感谢你的清楚解释。我会调整我的代码,我很高兴这个答案是有用的!!谢谢你的选择,希望你有一个和平的一天!