用java获取两个按空间分隔的数据并进行计算
我的问题是我想输入两个被空格隔开的数字,得到这两个数字,然后计算这两个数字。例如,我输入了“10 20”,它们被一个空格分隔,然后我想得到它们并将其计算为单独的:10*20=200 对于这一部分,我有一个能手和能手 以下是我的代码: 我的能手和二传手班用java获取两个按空间分隔的数据并进行计算,java,get,set,java.util.scanner,whitespace,Java,Get,Set,Java.util.scanner,Whitespace,我的问题是我想输入两个被空格隔开的数字,得到这两个数字,然后计算这两个数字。例如,我输入了“10 20”,它们被一个空格分隔,然后我想得到它们并将其计算为单独的:10*20=200 对于这一部分,我有一个能手和能手 以下是我的代码: 我的能手和二传手班 import java.util.*; public class LabExer2 { private String itemName; private double itemPrice; private int ite
import java.util.*;
public class LabExer2
{
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setItemName(String newItemName)
{
itemName = newItemName;
}
public String getItemName()
{
return itemName;
}
public void setTotalCost(int quantity, double price)
{
itemQuantity = quantity;
itemPrice = price;
amountDue = itemQuantity * itemPrice;
}
public double getTotalCost()
{
return amountDue;
}
public void readInput()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of the item you are purchasing.");
itemName = s.nextLine();
System.out.println("Enter the quantity and price separated by a space.");
//itemQuantity = s.nextInt();
//itemPrice = s.nextDouble();
}
public void writeInput()
{
System.out.println("You are purchasing " + itemQuantity + " " + itemName + "(s) at " + itemPrice + " each.");
System.out.println("Amount due is " + getTotalCost());
}
}
我的主课
public class MainLabExer2
{
public static void main(String[]args)
{
LabExer2 le = new LabExer2();
//still not finished because I can't figure out the process in the other class.
}
}
我想要的输出:
输入您要购买的商品的名称。铅笔
输入以空格分隔的数量和价格。
3.15.50
您将以每支15.50美元的价格购买3支铅笔。
应付金额为46.50美元
我想得到3和15.50,然后计算。我不知道该用什么,我只是java的初学者。您可以创建两个提示,一个用于输入
itemQuantity
int,另一个用于输入itemPrice
Double
但是,如果希望用户同时输入这两个值,则需要在readInput()方法中编写一个代码,用于提取itemQuantity
值和itemPrice
值
试试这个:
public void readInput() {
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of the item you are purchasing.");
itemName = s.nextLine();
System.out.println("Enter the quantity and price separated by a space.");
String userInput = s.nextLine(); //String to collect user input
//code to loop through String in search for Whitespace
for (int x=0; x<String.length(); x++) {
if (userInput.charAt(x) == ' ') {
//whitespace found, set itemQuantity to a substring
//from the first char in UserInput to the char before whitespace
itemQuantity = Integer.parseInt(userInput.substring(0, x-1));
//convert String to int
itemPrice = Integer.parseInt(userInput.substring(x+1, userInput.length()));
x = String.length();
} else {
itemQuantity = Integer.parseInt(userInput);
}
}
}
public void readInput(){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入您要购买的商品的名称”);
itemName=s.nextLine();
System.out.println(“输入用空格分隔的数量和价格”);
String userInput=s.nextLine();//收集用户输入的字符串
//在字符串中循环搜索空白的代码
对于(int x=0;x,可以按如下方式执行:
public void readInput() {
Scanner s = new Scanner(System.in);
System.out.print("Enter the name of the item you are purchasing: ");
itemName = s.nextLine();
System.out.print("Enter the quantity and price separated by a space: ");
String input[] = s.nextLine().split(" ");
try {
itemQuantity = Integer.parseInt(input[0]);
itemPrice = Double.parseDouble(input[1]);
setTotalCost(itemQuantity, itemPrice);
} catch (Exception e) {
System.out.println("Invalid input");
}
}
public void writeInput() {
DecimalFormat df = new DecimalFormat("#.00");// Formats decimal value to exactly two decimal places
System.out.println(
"You are purchasing " + itemQuantity + " " + itemName + "(s) at " + df.format(itemPrice) + " each.");
System.out.println("Amount due is: " + df.format(getTotalCost()));
}
下面给出了完整的程序和运行示例:
import java.text.DecimalFormat;
import java.util.Scanner;
public class LabExer2 {
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setItemName(String newItemName) {
itemName = newItemName;
}
public String getItemName() {
return itemName;
}
public void setTotalCost(int quantity, double price) {
itemQuantity = quantity;
itemPrice = price;
amountDue = itemQuantity * itemPrice;
}
public double getTotalCost() {
return amountDue;
}
public void readInput() {
Scanner s = new Scanner(System.in);
System.out.print("Enter the name of the item you are purchasing: ");
itemName = s.nextLine();
System.out.print("Enter the quantity and price separated by a space: ");
String input[] = s.nextLine().split(" ");
try {
itemQuantity = Integer.parseInt(input[0]);
itemPrice = Double.parseDouble(input[1]);
setTotalCost(itemQuantity, itemPrice);
} catch (Exception e) {
System.out.println("Invalid input");
}
}
public void writeInput() {
DecimalFormat df = new DecimalFormat("#.00");// Formats decimal value to exactly two decimal places
System.out.println(
"You are purchasing " + itemQuantity + " " + itemName + "(s) at " + df.format(itemPrice) + " each.");
System.out.println("Amount due is: " + df.format(getTotalCost()));
}
}
使用main
method:
public class MainLabExer2 {
public static void main(String[] args) {
LabExer2 lab = new LabExer2();
lab.readInput();
lab.writeInput();
}
}
Enter the name of the item you are purchasing: Pencil
Enter the quantity and price separated by a space: 3 15.50
You are purchasing 3 Pencil(s) at 15.50 each.
Amount due is: 46.50
运行示例:
public class MainLabExer2 {
public static void main(String[] args) {
LabExer2 lab = new LabExer2();
lab.readInput();
lab.writeInput();
}
}
Enter the name of the item you are purchasing: Pencil
Enter the quantity and price separated by a space: 3 15.50
You are purchasing 3 Pencil(s) at 15.50 each.
Amount due is: 46.50
好的。那么,LabExer2要求用户输入名称、数量和价格的方法是什么?(我知道答案。我希望你推理并找到你需要做的事情)@Groenhout这一定是问题的一部分(虽然他一定已经找到了答案,因为代码在那里,但注释掉了).如果你把问题读到最后,你会看到另一部分,那就是他应该首先弄清楚的部分(因此我的问题)is:仍然没有完成,因为我无法理解另一个类中的进程。@JBNizet我注释了它,因为它不正确,我想要的是输入两个仅由空格分隔的数据。这是您注释的代码允许的。为什么您认为它不允许?您测试过它吗(这是我的第一个问题)?@JoricLibiran-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。已接受的答案可以帮助未来的访问者自信地使用该解决方案。请检查以了解如何使用。您的主函数现在可以包含:le.readInput();le.setTotalCost();le.getTotalCost();
我认为使用类构造函数设置对象字段可以更好地简化代码。