我只想遵循這個條件,但問題是,當我輸入空時,它會繼續循環無限。我如何使用空字符串來終止?
請輸入S(供應)或R(補充)後跟ID和數量。
R p122 10
新庫存級別爲P125(踏板)18
S p905 20
否帶ID P905
空字符串(終止)
顯示最終發現一部分。
我也嘗試下面的代碼,但它不能正常工作
if (n.equals("")){
code
}
這裏是我的代碼:
import java.util.*;
public class TestPart {
public static void main(String[] args)
{
Part[] part = new Part[5];
part[0] = new Part("p122", "Chain", 48, 12.5);
part[1] = new Part("p123", "Chain Guard", 73, 22.0);
part[2] = new Part("p124", "Crank", 400, 11.5);
part[3] = new Part("p125", "Pedal", 38, 6.5);
part[4] = new Part("p126", "Handlebar", 123, 9.50);
Scanner src = new Scanner(System.in);
String n;
String id;
int qty;
System.out.print("Please Enter either S(supply) or R(replenish) followed by ID and quantity:\n");
int a = 0;
do
{
n = src.next();
id= src.next();
qty= src.nextInt();
boolean found = false;
for(int i = 0; i< part.length; i++)
{
if(part[i].getID().equals(id))
{
found = true;
String name = part[i].getName();
int stockLevel = part[i].getStockLevel();
if(id.equals(part[i].getID()) && n.charAt(0) == 'S')
{
double amount = part[i].supply(qty);
if(amount>0){
System.out.println("New Stock-level for " + part[i].getID() + "(" + part[i].getName() + ") is " + part[i].getStockLevel());
}
else{
System.out.println("New Stock-level for " + part[i].getID() + "(" + part[i].getName() + ") is not available");
}
}
else if (id.equals(part[i].getID()) && n.charAt(0) == 'R')
{
part[i].replenish(qty);
System.out.println("New Stock-level for " + part[i].getID() + "(" + part[i].getName() + ") is " + part[i].getStockLevel());
}
}
}
if (found == false)
{
System.out.println("No part found with ID " + id);
}
System.out.println("");
for(int i=0; i<part.length; i++)
{
System.out.println(part[i].getID()+ "\t"+part[i].getName()+"\t"+part[i].getStockLevel()+"\t"+part[i].getUnitPrice());
}
}while(a ==0);
}
當你到底要終止程序?如果沒有發現身份證? –
你在尋找'break'語句嗎?你究竟做了什麼? (你顯示了一個'if'語句,但它只是在代碼中表示'code';你爲'code'嘗試了什麼?) – ajb
那麼當我不輸入任何東西(例如.id,n和qty)和它將直接顯示我的最終名單。 – CKL