2016-11-17 75 views
-2

只要你教我,我就可以學習。我只是在談論Java,因爲它是我學習的最佳方式。如果你發現任何你認爲我可以在代碼中改進的東西,我很樂意聽到它。不知道發生了什麼 - 沒有錯誤(Java新手)

我爲自己準備的項目的總體目標是用Java創建一個存儲有關人員信息的程序。然後將該信息傳輸到Tomcat服務器,我將設置運行SQL。

現在你明白我的總體目標(我是從它很遠),我的問題是:

我可以去詢問我一個條目的命令和名稱等代碼的一部分沒問題。但是,當我輸入「read」並按下回車鍵時,它似乎跳過else語句,我已經設置並直接進入else部分。

編輯:我更新了帖子,以顯示什麼解決了問題。

工作代碼:

import javax.print.DocFlavor.READER; 

import java.util.ArrayList; 
import java.util.Scanner; 


public class Main { 

public static void main(String[] args) { 
    Scanner reader = new Scanner(System.in); 
    int total = 0; 
    int years = 0; 
    int invested =0; 
    ArrayList<String> names = new ArrayList<String>(0); 
    ArrayList<String> address = new ArrayList<String>(0); 
    ArrayList<String> ids = new ArrayList<String>(0); 
    Scanner sReader = new Scanner(System.in); 

    while(reader.nextLine().toLowerCase().split(" ")[0] != "exit") 
    { 
    System.out.print("Enter Instruction: "); 
    String n = reader.nextLine(); 
    String[] uin = n.toLowerCase().split(" "); 
    if(uin[0].equals("add")) 
    { 
      try { 
       System.out.println(add(Integer.parseInt(uin[1]),Integer.parseInt(uin[2]))); 
       } catch (NumberFormatException e) { 
       System.out.println("Numbers Only"); 
       System.out.print(e.getMessage()); 
       } catch (Exception e){ 
        System.out.println("ERROR"); 
       } 
    } 


    if(uin[0].equals("sub") || uin[0].equals("subtract")) 
    { 
      try { 
       System.out.print(Integer.parseInt(uin[1])-Integer.parseInt(uin[2])); 
       } catch (NumberFormatException e){ 
       System.out.println("Numbers Only"); 
       System.out.print(e.getMessage()); 
       } catch (Exception e){ 
        System.out.println("ERROR"); 
       } 

    } 

    if(uin[0].equals("string")) 
    { 
     String string = "this is a string."; 
     System.out.println(string.indexOf("cat")); 
    } 
    if(uin[0].equals("a")) 
    { 
     total+=Integer.parseInt(uin[1]); 
     invested+=Integer.parseInt(uin[1]); 
    } 
    if(uin[0].equals("aa")) 
    { 
     total+=10000; 
     invested+=10000; 
    } 
    if(uin[0].equals("bb")) 
    { 
     total-=55000; 
     invested-=55000; 
    } 
    if(uin[0].equals("aq")) 
    { 
     for(int i = 0;i < Integer.parseInt(uin[1]);i++){ 
      total+=10000; 
      invested+=10000; 
      total*=1.06; 
      years+=1; 
     } 
     years-=1; 
    } 
    if(uin[0].equals("exit")) 
    { 
     break; 
    } 
    if(uin[0].equals("entry")){ 
     for(int i=0;i==0;i+=0){ 
      System.out.print("Entry Command: "); 
      String input = sReader.nextLine(); 
      if(input.equals("exit")){ 
       i = 1; 
      }else if(input.equals("read")){ 
       for(int y=0;y<names.size();y++){ 
        System.out.println("User #"+y); 
        System.out.println(names.get(y).toString()); 
        System.out.println(address.get(y).toString()); 
        System.out.println(ids.get(y).toString()); 
       } 
      }else{ 
       System.out.print("Name: "); 
       input = sReader.nextLine(); 
       names.add(input); 
       System.out.print("Address: "); 
       input = sReader.nextLine(); 
       address.add(input); 
       System.out.print("ID: "); 
       input = sReader.nextLine(); 
       ids.add(input); 
      } 
     } 
    } 
    total*=1.06; 
    years+=1; 
    double earn = Math.floor(total) * 0.06; 
    System.out.println("Bank: " + total); 
    System.out.println("Invested: " + invested); 
    System.out.println("Yearly Earnings: " + earn); 
    System.out.println("Years: " + years); 
    } 
} 
public static int add(int n1, int n2){ 
int out; 
try{ 
    out = (n1+n2); 
    return(out); 
}catch (Exception e){ 
    System.out.print("ERROR"); 
    return(0); 
} 
} 
} 

舊代碼:

import javax.print.DocFlavor.READER; 

import java.util.ArrayList; 
import java.util.Scanner; 


public class Main { 

public static void main(String[] args) { 
    Scanner reader = new Scanner(System.in); 
    int total = 0; 
    int years = 0; 
    int invested =0; 
    ArrayList<String> names = new ArrayList<String>(0); 
    ArrayList<String> address = new ArrayList<String>(0); 
    ArrayList<String> ids = new ArrayList<String>(0); 
    Scanner sReader = new Scanner(System.in); 

    while(reader.nextLine().toLowerCase().split(" ")[0] != "exit") 
    { 
    System.out.print("Enter Instruction: "); 
    String n = reader.nextLine(); 
    String[] uin = n.toLowerCase().split(" "); 
    if(uin[0].equals("add")) 
    { 
      try { 
       System.out.println(add(Integer.parseInt(uin[1]),Integer.parseInt(uin[2]))); 
       } catch (NumberFormatException e) { 
       System.out.println("Numbers Only"); 
       System.out.print(e.getMessage()); 
       } catch (Exception e){ 
        System.out.println("ERROR"); 
       } 
    } 


    if(uin[0].equals("sub") || uin[0].equals("subtract")) 
    { 
      try { 
       System.out.print(Integer.parseInt(uin[1])-Integer.parseInt(uin[2])); 
       } catch (NumberFormatException e){ 
       System.out.println("Numbers Only"); 
       System.out.print(e.getMessage()); 
       } catch (Exception e){ 
        System.out.println("ERROR"); 
       } 

    } 

    if(uin[0].equals("string")) 
    { 
     String string = "this is a string."; 
     System.out.println(string.indexOf("cat")); 
    } 
    if(uin[0].equals("a")) 
    { 
     total+=Integer.parseInt(uin[1]); 
     invested+=Integer.parseInt(uin[1]); 
    } 
    if(uin[0].equals("aa")) 
    { 
     total+=10000; 
     invested+=10000; 
    } 
    if(uin[0].equals("bb")) 
    { 
     total-=55000; 
     invested-=55000; 
    } 
    if(uin[0].equals("aq")) 
    { 
     for(int i = 0;i < Integer.parseInt(uin[1]);i++){ 
      total+=10000; 
      invested+=10000; 
      total*=1.06; 
      years+=1; 
     } 
     years-=1; 
    } 
    if(uin[0].equals("exit")) 
    { 
     break; 
    } 
    if(uin[0].equals("entry")){ 
     for(int i=0;i==0;i+=0){ 
      System.out.print("Entry Command: "); 
      sReader = new Scanner(System.in); 
      System.out.println(sReader.nextLine()); 
      if(sReader.nextLine().equals("exit")){ 
       i = 1; 
      }else if(sReader.nextLine().equals("read")){ 
       System.out.println(names.size()); 
       System.out.println("yup"); 
       for(int y=0;y==names.size()-1;y++){ 
        System.out.println(names); 
        System.out.println(names.get(y)); 
        System.out.println(address.get(y)); 
        System.out.println(ids.get(y)); 
       } 
      }else{ 
       System.out.print("Name: "); 
       sReader = new Scanner(System.in); 
       names.add(sReader.nextLine()); 
       System.out.print("Address: "); 
       sReader = new Scanner(System.in); 
       address.add(sReader.nextLine()); 
       System.out.print("ID: "); 
       sReader = new Scanner(System.in); 
       ids.add(sReader.nextLine()); 
      } 
     } 
    } 
    total*=1.06; 
    years+=1; 
    double earn = Math.floor(total) * 0.06; 
    System.out.println("Bank: " + total); 
    System.out.println("Invested: " + invested); 
    System.out.println("Yearly Earnings: " + earn); 
    System.out.println("Years: " + years); 
    } 
} 
public static int add(int n1, int n2){ 
int out; 
try{ 
    out = (n1+n2); 
    return(out); 
}catch (Exception e){ 
    System.out.print("ERROR"); 
    return(0); 
} 
} 
} 
+3

你繼續閱讀文本的每次調用'sReader.nextLine時間()新行'。將結果存儲在變量中,並在您的條件中使用該變量。 –

+1

歡迎來到堆棧溢出!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –

+1

你for for循環看起來很奇怪...'for(int y = 0; y == names.size() - 1; y ++){'可能不會輸入 –

回答

4

這是怎麼回事錯了,因爲你正在做System.out.println(sReader.nextLine()) - 這自動獲得最後的輸入(所以"exit"在你的例子) 。但是當你試圖做if (sReader.nextLine().equals("exit"))它正在尋找下一行。

要解決,你可以輸入存儲在一個變量:

String input = sReader.nextLine(); 
System.out.println(input); 
if (input.equals("exit")) { 
    // etc 
} 
+0

謝謝!這絕對推動我朝着正確的方向解決問題。 – SWB