Java:其他if语句错误(二进制搜索帮助)

时间:2016-06-12 06:17:59

标签: java if-statement binary-search

所以我花了一整天的时间研究一个小小的java项目,并且在我花了三个小时用谷歌搜索之前我应该​​先尝试一下。

所以我要做的是在if-else语句中使用二进制搜索来访问连接到代码中其他位置的类的数组。

所以你可以看到代码:

public class BankProject {

public static void main(String[] args) {

Account[] accountArray = new Account[10];
accountArray[0] = new Account("Dillen Newton", 5555, 9000000.0, 10.0);
accountArray[1] = new Account("Meteor", 5556, 10000.0, 5.5);
accountArray[2] = new Account("Meteorite", 5557, 20000.5, 20.0);
accountArray[3] = new Account("Super Mario", 5558, 100.0, 7.0);
accountArray[4] = new Account("A Person", 5559, 234567.0, 6.0);
accountArray[5] = new Account("Noone", 5560, 97498237.99, 50.0);
accountArray[6] = new Account("Yes", 5561, 5.5, 100.0);
accountArray[7] = new Account("Anonymous", 5562, 222.2, 13.0);
accountArray[8] = new Account("Five Hours", 5563, 7600.0, 40.0);
accountArray[9] = new Account("Who Knows", 5564, 9533.8, 99.0);

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("Please Enter A Specified Name");
System.out.println("Dillen Newton");
System.out.println("Meteor");
System.out.println("Meteorite");
System.out.println("Super Mario");
System.out.println("A Person");
System.out.println("Noone");
System.out.println("Yes");
System.out.println("Anonymous");
System.out.println("Five Hours");
System.out.println("Who Knows");
System.out.println("");
String n = reader.next(); // Scans the next token of the input as an int.

    if (n.equals("Dillen Newton")){
        Arrays.binarySearch(accountArray, "Dillen Newton");
        System.out.println("Welcome, Dillen");
} else if (n.equals("Meteor")){
        Arrays.binarySearch(accountArray, "Meteor");
        System.out.println("Welcome, Meteor!");     
} else if (n.equals("Meteorite")){
    Arrays.binarySearch(accountArray, "Meteorite");
    System.out.println("Hello, Meteorite!");        
} else if (n.equals("Super Mario")){
    Arrays.binarySearch(accountArray, "Super Mario");
    System.out.println("Welcome, Mario");       
} else if (n.equals ("A Person")){
    Arrays.binarySearch(accountArray, "A Person");
    System.out.println("Hi Person!");       
} else if (n.equals("Noone")){
    Arrays.binarySearch(accountArray, "Noone");
    System.out.println("Welcome, Nobody!");     
} else if (n.equals("Yes")){
    Arrays.binarySearch(accountArray, "Yes");
    System.out.println("Yes");  
} else if (n.equals("Anonymous")){
    Arrays.binarySearch(accountArray, "Anonymous");
    System.out.println("Hello There!");     
} else if (n.equals("Five Hours")){
    Arrays.binarySearch(accountArray, "Five Houres");
    System.out.println("Has it been that long already?");       
} else if (n.equals("Who Knows")){
    Arrays.binarySearch(accountArray, "Who Knows");
    System.out.println("I don't");      
} else{
    System.out.println("Incorrect Account. Please restart and try again");
    reader.close();
}


    /*  Account testAccount = new Account("Dillen Newton", 1122, 20000.0, 4.5);
    testAccount.withdraw(2500);
    testAccount.deposit(3000);
    java.util.Date dateCreated = new java.util.Date();
    System.out.println("Date Created:" + dateCreated );
    System.out.println("Name:" + testAccount.getName());
    System.out.println("Account ID:" + testAccount.getId());
    System.out.println("Balance:" + testAccount.getBalance());
    System.out.println("Monthly Interest:" + testAccount.getMonthlyInterest());

    System.out.println("Process completed.");
    }
    */
} 

}

班级帐户{

private String name;
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

public Account(){
    name = "";
    id = 0;
    balance = 0.0;
    annualInterestRate = 0.0;
}

public Account(int newId, double newBalance){
    id = newId;
    balance = newBalance;
}

public Account(String newName, int newId, double newBalance, double newAnnualInterestRate) {
    name = newName;
    id = newId;
    balance = newBalance;
    annualInterestRate = newAnnualInterestRate;
}

public String getName(){
    return name;
}

public int getId(){
    return id;
}
public double getBalance(){
    return balance;
}
public double getAnnualInterestRate(){
    return annualInterestRate;
}

public void setName(String newName){
    name = newName;
}

public void setId(int newId){
    id = newId;
}
public void setBalance(double newBalance){
    balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate){
    annualInterestRate = newAnnualInterestRate;
}
public Date getDateCreated(){
    return dateCreated;
}
public double getMonthlyInterestRate(){
    return annualInterestRate / 12;
}
public double getMonthlyInterest(){
return (balance*getMonthlyInterestRate()/100);
}

public void withdraw(double amount)
{
    balance = balance - amount;
}

public void deposit(double amount)
{
    balance = balance + amount;
}
}

所以我相信到目前为止所做的一切都是正确的。但if-else语句只考虑一切不正确。

我要做的是从数组中选择一个帐户,以便在下一部分我可以开始执行提款,存款和余额检查等操作。这个项目需要线性搜索或二进制搜索。

我不知道我是否需要尝试别的东西,或者我只是缺少一些非常简单的东西。无论如何,任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

使用public class MultiPortRequest<T> extends Request<T> { private static final String FILE_PART_NAME = "file"; private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create(); private final Response.Listener<String> mListener; private final File mImageFile; protected Map<String, String> headers; public MultiPortRequest(String url,Response.Listener<String> listener, ErrorListener errorListener, File imageFile) { super(Method.POST, url, errorListener); mListener = listener; mImageFile = imageFile; buildMultipartEntity(); } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = super.getHeaders(); if (headers == null || headers.equals(Collections.emptyMap())) { headers = new HashMap<String, String>(); } headers.put("Identity", "rHTYsg8vZ/KWTaGOsy0eMNhngJMiZiK60pd9jAUQ+fI="); return headers; } private void buildMultipartEntity() { mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("multipart/form-data"), mImageFile.getName()); mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8")); } @Override public String getBodyContentType() { String contentTypeHeader = mBuilder.build().getContentType().getValue(); return contentTypeHeader; } @Override public byte[] getBody() throws AuthFailureError{ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try{ mBuilder.build().writeTo(bos); } catch (IOException e) { VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request."); } return bos.toByteArray(); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response){ try { String result = null; result = new String( response.data, HttpHeaderParser.parseCharset( response.headers ) ); return ( Response<T> ) Response.success( new JSONObject( result ), HttpHeaderParser.parseCacheHeaders(response) ); } catch ( UnsupportedEncodingException e ) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } @Override protected void deliverResponse(T response){ mListener.onResponse( response.toString()); Log.v("yes", String.valueOf(mListener)); } }

比较字符串

答案 1 :(得分:0)

您的问题标题具有误导性。您在上一段中表达的问题是关于字符串比较,与二进制搜索无关。实际上,通过大量的i​​f-else语句,您正在执行线性搜索。

我不知道您的任务是自己实施二进制搜索还是只是整理数据。在第二种情况下,我建议使用TreeMap

编辑:

class Account implements Comparable<Account> {
    [...]

    @Override
    public int compareTo(Account another) {
        return name.compareTo(another.getName());
    }
}

现在在您的代码中,您可以:

Arrays.sort(accountArray);
int i = Arrays.binarySearch(accountArray, new Account(name, 0, 0, 0));