出于某种原因,我很难将这部分代码编译完成。我们在本节中应该做的是创建一个买卖股票的选项。用户将输入" B" " B"购买或" S" " S"卖。我遇到的问题是我的代码在接受用户选择购买或出售之前就停止了。它一直打印到"输入选项:"然后停下来。
股票类
import java.util.Scanner;
public class TradeStock
{
public static void main(String[] args)
{
//declarations
Scanner in = new Scanner(System.in);
String name; //Name of stock
String symbol, decision; //Symbol of stock
double price; //price per share of stock
int shares; //number of shares of stock
Stock myStock = new Stock();
//Get name and symbol
System.out.print("Enter name of stock: ");
name = in.nextLine( );
if(!myStock.setName(name))
{
System.out.println("Invalid Name - Must have at least one character");
System.exit(0);
}
System.out.print("Get symbol of stock: ");
symbol = in.nextLine( );
if(myStock.setSymbol(symbol))
{
myStock.setSymbol(symbol);
}
else
{
System.out.println("Invalide Symbol of stock - Must be between 3 and 6 characters, inclusively");
System.exit(0);
}
//create Stock object with price and symbol
Stock myNames = new Stock(name, symbol);
//prompt user for price and set it
System.out.print("Enter price of stock: ");
price = in.nextDouble( );
myStock.setPrice(price);
if(myStock.setPrice(price))
{
myStock.setPrice(price);
}
else
{
System.out.println("Invalid Price - Must be greater than 0");
System.exit(0);
}
//prompt user for number of shares and set it
System.out.print("Enter number of shares: ");
shares = in.nextInt( );
myStock.setShares(shares);
if(myStock.setShares(shares))
{
myStock.setShares(shares);
}
else
{
System.out.println("Invalid number of Shares Must be between 10 and 1000, inclusively and a multiple of 10");
System.exit(0);
}
//print all stock information
//uses space needed for each output value
System.out.printf("%nName: %s%n" ,myStock.getName());
System.out.printf("Symbol: %s%n" , myStock.getSymbol());
System.out.printf("Price: %.2f%n", myStock.getPrice());
System.out.printf("Shares: %d%n" , myStock.getShares());
System.out.println("");
System.out.println("Options as single upper or lower case character:");
System.out.println(" B to buy the stock");
System.out.println(" S to sell the stock");
System.out.println(" Any other to exit");
System.out.println("Enter Option: ");
decision = in.nextLine();
if(decision.equals("B") || decision.equals("b"))
{
System.out.println("Cost of shares: " + myStock.calcValue());
System.out.println("Commission: " + myStock.calcCommission());
System.out.println("Total Cost: " + (myStock.calcValue() + myStock.calcCommission()));
}
else
if(decision.equals("S") || decision.equals("s"))
{
System.out.println("Receipts: " + myStock.calcValue());
System.out.println("Commission: " + myStock.calcCommission());
System.out.println("Net Receipts: " + (myStock.calcValue() + myStock.calcCommission()));
}
}
}
测试课 import java.util.Scanner;
public class TradeStock
{
public static void main(String[] args)
{
//declarations
Scanner in = new Scanner(System.in);
String name; //Name of stock
String symbol, decision; //Symbol of stock
double price; //price per share of stock
int shares; //number of shares of stock
Stock myStock = new Stock();
//Get name and symbol
System.out.print("Enter name of stock: ");
name = in.nextLine( );
if(!myStock.setName(name))
{
System.out.println("Invalid Name - Must have at least one character");
System.exit(0);
}
System.out.print("Get symbol of stock: ");
symbol = in.nextLine( );
if(myStock.setSymbol(symbol))
{
myStock.setSymbol(symbol);
}
else
{
System.out.println("Invalide Symbol of stock - Must be between 3 and 6 characters, inclusively");
System.exit(0);
}
//create Stock object with price and symbol
Stock myNames = new Stock(name, symbol);
//prompt user for price and set it
System.out.print("Enter price of stock: ");
price = in.nextDouble( );
myStock.setPrice(price);
if(myStock.setPrice(price))
{
myStock.setPrice(price);
}
else
{
System.out.println("Invalid Price - Must be greater than 0");
System.exit(0);
}
//prompt user for number of shares and set it
System.out.print("Enter number of shares: ");
shares = in.nextInt( );
myStock.setShares(shares);
if(myStock.setShares(shares))
{
myStock.setShares(shares);
}
else
{
System.out.println("Invalid number of Shares Must be between 10 and 1000, inclusively and a multiple of 10");
System.exit(0);
}
//print all stock information
//uses space needed for each output value
System.out.printf("%nName: %s%n" ,myStock.getName());
System.out.printf("Symbol: %s%n" , myStock.getSymbol());
System.out.printf("Price: %.2f%n", myStock.getPrice());
System.out.printf("Shares: %d%n" , myStock.getShares());
System.out.println("");
System.out.println("Options as single upper or lower case character:");
System.out.println(" B to buy the stock");
System.out.println(" S to sell the stock");
System.out.println(" Any other to exit");
System.out.println("Enter Option: ");
decision = in.nextLine();
if(decision.equals("B") || decision.equals("b"))
{
System.out.println("Cost of shares: " + myStock.calcValue());
System.out.println("Commission: " + myStock.calcCommission());
System.out.println("Total Cost: " + (myStock.calcValue() + myStock.calcCommission()));
}
else
if(decision.equals("S") || decision.equals("s"))
{
System.out.println("Receipts: " + myStock.calcValue());
System.out.println("Commission: " + myStock.calcCommission());
System.out.println("Net Receipts: " + (myStock.calcValue() + myStock.calcCommission()));
}
}
}
提前致谢
答案 0 :(得分:1)
如果用户输入“s”或“b”以外的任何内容(包括换行符),您的程序将立即退出。输入流很可能包含此时前一个提示的换行符。在执行最后一个if。
之前,请确保decision
有效
答案 1 :(得分:1)
您需要对代码进行一些更改
System.out.print("Enter number of shares: ");
shares = Integer.parseInt(in.nextLine()); // insted of in.nextInt()
或in.skip("\n");
之后in.nextInt();
。
因为你是在这些行之后这样做的: -
System.out.println("Enter Option: ");
decision = in.nextLine();
接受\n
来终止输入流中in.nextInt()
留下的阅读。
查看代码后,我认为您应该使用in.skip("\n")
,因为您在nextDouble()
之前正在nextInt()
。两者都会在输入流中留下\n
。
执行nextInt()
后,您输入的数据如下:123
输入。
nextInt()
仅读取123
,但输入(\n
)仍保留在您的信息流中。
nextLine()
需要\n
来读取由enter。
因此,当您使用nextLine()
向您阅读下一个输入时,\n
(输入)已存在于流中。这就是为什么你的下一个输入没有被阅读。
因此,为了读取下一个数据,您必须从输入流中跳过\n
。
答案 2 :(得分:0)
shares = in.nextInt( );
decision = in.nextLine();
scanner.nextInt()只取整数值(不读取下一个令牌),如果输入整数(24)和“回车”,则共享变量分配给24并且决策被分配到“\ n”,因为你在整数24之后点击了“Enter”。这就是我们建议使用in.skip(“\ n”)的原因,这样它就会忽略整数之后的下一个换行符。
请在决策块中添加一个else语句,以观察您的代码是如何工作的:
else
if(decision.equals("S") || decision.equals("s"))
{
System.out.println("Receipts: " + myStock.calcValue());
System.out.println("Commission: " + myStock.calcCommission());
System.out.println("Net Receipts: " + (myStock.calcValue() + myStock.calcCommission()));
}else {
System.out.println("User Input "+decision+" is invalid");
}
或以调试模式运行以观察决策变量的值。