SOGO論壇
  登入   註冊   找回密碼
查看: 978|回覆: 4
列印 上一主題 下一主題

[程式設計] Java 程式碼問題 [複製連結]

Rank: 1

狀態︰ 離線
跳轉到指定樓層
1
發表於 2011-12-4 23:56:05 |只看該作者 |倒序瀏覽
public class resturant
{
        public static void main(String[] args)
        {
        int x = 0;
        int i = 0;
        int num = 0;
        String rain = null;
       
        System.out.println("歡迎使用此餐廳經營系統");
        System.out.println(" ");                
        System.out.println("以下是使用本系統時需要的指令:");
        System.out.println("(" + 1 + ")" + "new 客人種類 餐點價格:新客人入內 ");
        System.out.println("(" + 2 + ")" + "pass 時間:推移模擬時間");
        System.out.println("(" + 3 + ")" + "rain start:讓模擬世界下雨");
        System.out.println("(" + 4 + ")" + "rain end:讓模擬世界停雨");
        System.out.println("(" + 5 + ")" + "status:顯示資訊");
        System.out.println("(" + 6 + ")" + "exit:離開系統");
        System.out.println("註:客人類型分為a、b、c、d共四種");
       
        while (x==0)          
        {
        System.out.println(" ");                              
        System.out.println("請輸入指令..");
        String order = ConsoleIn.readLine();
        String[] sorder = order.split(" ");      

       
        customer[] cus = new customer[100];       
       
        if (sorder[0].equals("new")){
        cus[i] = new customer();
        if (sorder[1].equals("a")){
        cus[i].time = 30;
        }
        if (sorder[1].equals("b")){
        cus[i].time = 40;
        }
        if (sorder[1].equals("c")){
        cus[i].time = 20;
        }
        if (sorder[1].equals("d")){
        cus[i].time = 50;
        }
        cus[i].type = sorder[1];
        cus[i].price = Integer.parseInt(sorder[2]);
        i++;
        num++;
        }
       
        if (sorder[0].equals("pass")){
        cus[i] = new customer();
        int passtime = Integer.parseInt(sorder[1]);
        int k = 0;
        cus[k].passtime = passtime;
        k++;
        for (int j = 0; j < 100; j++){
        cus[j].time = cus[j].time - cus[j].passtime;
        if (cus[j].time <= 0){
        num--;
        }       
        }
        }
       
        if (sorder[0].equals("rain")){
       
        if (sorder[1].equals("start")){
        cus[i] = new customer();
        for (int j = 0; j < 100; j++){
        if (cus[j].type.equals("a")){
        cus[j].time = cus[j].time * 2;
        }
        if (cus[j].type.equals("b")){
        cus[j].time = cus[j].time  / 2;
        }
        if (cus[j].type.equals("c")){
        cus[j].time = cus[j].time + 20;
        }
        if (cus[j].type.equals("d")){
        cus[j].time = cus[j].time - 15;
        }
        }
        rain = "yes";
        }
        if (sorder[1].equals("end"))
        {
        rain = "no";
        }
        }
       
        if (sorder[0].equals("status")){
        cus[i] = new customer();
        System.out.println("◎客人資訊");
        for (int j = 0; j < 100; j++){
        if (cus[j].time > 0){
        System.out.println("客人種類:" + cus[j].type);
        System.out.println("餐點價格:" + cus[j].price + "元");
        System.out.println("用餐時間:" + cus[j].passtime + "分鐘");
        System.out.println("剩餘時間:" + cus[j].time + "分鐘");                       
        }
        }
        System.out.println("◎餐廳資訊");
        System.out.println("目前用餐人數:" + num + "人");
        int total = 0;
        int totaltime = 0;
        for (int j = 0; j < 100; j++){
        if (cus[j].time <= 0){
        total = total + cus[j].price;
        }
        }
        System.out.println("總營業額:" + total + "元");
       
        for (int j = 0; j < 100; j++){
        totaltime = totaltime + cus[j].passtime;
        }
        System.out.println("總營業時間:" + totaltime + "分鐘");       
               
        if (rain.equals("yes")){
        System.out.println("天氣狀況:" + "雨天");
        }
        if (rain.equals("no")){
        System.out.println("天氣狀況:" + "無雨");
        }
        }
       
        if (sorder[0].equals("exit")){
        cus[i] = new customer();
        System.out.println("◎餐廳資訊");
        System.out.println("目前用餐人數:" + num + "人");
        int total = 0;
        int totaltime = 0;       
        for (int j = 0; j < 100; j++){
        if (cus[j].time <= 0){
        total = total + cus[j].price;
        }
        }
        System.out.println("總營業額:" + total + "元");       
        for (int j = 0; j < 100; j++){
        totaltime = totaltime + cus[j].passtime;
        }       
        System.out.println("總營業時間:" + totaltime + "分鐘");       
               
        if (rain.equals("yes")){
        System.out.println("天氣狀況:" + "雨天");
        }
        if (rain.equals("no")){
        System.out.println("天氣狀況:" + "無雨");
        }
        break;
        }
        }
       

}
}


===========

public class customer

{
public String type;
public int price;
public int time;
public int passtime;
}

==========

這是我的程式碼 compile的時候會過
但是在執行程式的時候 只要輸入指令
他就會跳出一串字母 什麼nullpointer的
找了好久 還是毫無頭緒
哪位大大 能幫我修改嗎 非常感謝
喜歡嗎?分享這篇文章給親朋好友︰
               感謝作者     

Rank: 3Rank: 3

數位軟體勳章

狀態︰ 離線
2
發表於 2011-12-5 02:22:46 |只看該作者
嗨嗨嗨 大大您好阿

請您把
  1. System.out.println(" ");                              
  2.         System.out.println("請輸入指令..");
  3.         String order = ConsoleIn.readLine();
  4.         String[] sorder = order.split(" ");      
複製代碼
改成
  1.                 System.out.println(" ");                              
  2.                 System.out.println("請輸入指令..");
  3.                 
  4.                 String order = "";
  5.                 BufferedReader ConsoleIn = new BufferedReader(new InputStreamReader(System.in));      
  6.                 try{
  7.                  order = ConsoleIn.readLine();
  8.                 }catch(IOException e){}
  9.                     String[] sorder = order.split(" ");
複製代碼
試試看唷!!!   
已有 1 人評分威望 收起 理由
紅塵孤鳥 + 2 感謝您熱心幫助會員解決問題,論壇需要您的.

總評分: 威望 + 2   查看全部評分

感同身受,就能放下執著

Rank: 1

狀態︰ 離線
3
發表於 2011-12-5 02:47:45 |只看該作者
helper197 發表於 2011-12-5 02:22  
嗨嗨嗨 大大您好阿

請您把改成試試看唷!!!

沒辦法耶....不過還是謝謝你

Rank: 3Rank: 3

數位軟體勳章

狀態︰ 離線
4
發表於 2011-12-5 09:24:35 |只看該作者

   嗨嗨嗨   大大您好阿

   因為阿  「他就會跳出一串字母 什麼nullpointer的」  這個訊息應該是告訴您 您的ConsoleIn並沒有宣告乘一個物件的樣子

   啊  小弟 沒用過 java 不過昨天編譯時 他告訴我 「new BufferedReader(new InputStreamReader(System.in))」 需要給錯誤處理

   所以需要在加上 try  來處理,   然後 偶就編譯成功了

   還是您要先加上去 後 再把您加上去的Code 貼出來 小弟來試試看哩

   加油阿 :D  
感同身受,就能放下執著

Rank: 3Rank: 3

數位軟體勳章

狀態︰ 離線
5
發表於 2011-12-5 10:15:37 |只看該作者
這個是小弟整理過得 可以編譯過得
不過, sorder 陣列的部份您可能要改一下喔.. 才能正常運作
  1. import java.io.*;
  2. import java.io.Console;
  3. public class resturant
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 int x = 0;
  8.                 int i = 0;
  9.                 int num = 0;
  10.                 String rain = null;
  11.              
  12.                 System.out.println("Welcome");
  13.                 System.out.println(" ");               
  14.                 System.out.println("Command List:");
  15.                 System.out.println("(" + 1 + ")" + "new Customer");
  16.                 System.out.println("(" + 2 + ")" + "pass Time");
  17.                 System.out.println("(" + 3 + ")" + "rain Start");
  18.                 System.out.println("(" + 4 + ")" + "rain Stop");
  19.                 System.out.println("(" + 5 + ")" + "status");
  20.                 System.out.println("(" + 6 + ")" + "exit System");
  21.                 System.out.println("PS: Customer [A,B,C,D]");
  22.                
  23.                 while (x==0)         
  24.           {
  25.                         System.out.println(" ");                              
  26.                         System.out.println("Command. Input");
  27.                 
  28.                         String order = "";
  29.                         BufferedReader ConsoleIn = new BufferedReader(new InputStreamReader(System.in));      
  30.                         try{
  31.                                 order = ConsoleIn.readLine();
  32.                         }catch(IOException e)
  33.                         {}
  34.                        
  35.                         String[] sorder = order.split(" ");
  36.                         customer[] cus = new customer[100];      
  37.                
  38.                         if (sorder[0].equals("new"))
  39.                         {
  40.                                 cus[i] = new customer();
  41.                                 if (sorder[1].equals("a"))
  42.                                 {
  43.                                         cus[i].time = 30;
  44.                                 }
  45.                                 if (sorder[1].equals("b"))
  46.                                 {
  47.                                         cus[i].time = 40;
  48.                                 }
  49.                                 if (sorder[1].equals("c"))
  50.                                 {
  51.                                         cus[i].time = 20;
  52.                                 }
  53.                                 if (sorder[1].equals("d"))
  54.                                 {
  55.                                         cus[i].time = 50;
  56.                                 }
  57.                                 cus[i].type = sorder[1];
  58.                                 cus[i].price = Integer.parseInt(sorder[2]);
  59.                                 i++;
  60.                                 num++;
  61.                         }
  62.                
  63.                         if (sorder[0].equals("pass"))
  64.                         {
  65.                                 cus[i] = new customer();
  66.                                 int passtime = Integer.parseInt(sorder[1]);
  67.                                 int k = 0;
  68.                                 cus[k].passtime = passtime;
  69.                                 k++;
  70.                                 for (int j = 0; j < 100; j++)
  71.                                 {
  72.                                         cus[j].time = cus[j].time - cus[j].passtime;
  73.                                         if (cus[j].time <= 0)
  74.                                         {
  75.                                                 num--;
  76.                                         }      
  77.                                 }
  78.                         }
  79.                        
  80.                         if (sorder[0].equals("rain"))
  81.                         {               
  82.                                 if (sorder[1].equals("start"))
  83.                                 {
  84.                                         cus[i] = new customer();
  85.                                         for (int j = 0; j < 100; j++)
  86.                                         {
  87.                                                 if (cus[j].type.equals("a"))
  88.                                                 {
  89.                                                         cus[j].time = cus[j].time * 2;
  90.                         }
  91.                         if (cus[j].type.equals("b"))
  92.                         {
  93.                                 cus[j].time = cus[j].time  / 2;
  94.                         }
  95.                         if (cus[j].type.equals("c"))
  96.                         {
  97.                                 cus[j].time = cus[j].time + 20;
  98.                         }
  99.                         if (cus[j].type.equals("d"))
  100.                         {
  101.                                 cus[j].time = cus[j].time - 15;
  102.                         }
  103.                                         }
  104.                                         rain = "yes";
  105.                                 }
  106.                                 if (sorder[1].equals("end"))
  107.                                 {
  108.                                         rain = "no";
  109.                                 }
  110.                         }
  111.                        
  112.                         if (sorder[0].equals("status"))
  113.                         {
  114.                                 cus[i] = new customer();
  115.                                 System.out.println("◎客人資訊");
  116.                                 for (int j = 0; j < 100; j++)
  117.                                 {
  118.                                         if (cus[j].time > 0)
  119.                                         {
  120.                                                 System.out.println("客人種類:" + cus[j].type);
  121.                                                 System.out.println("餐點價格:" + cus[j].price + "元");
  122.                                                 System.out.println("用餐時間:" + cus[j].passtime + "分鐘");
  123.                                                 System.out.println("剩餘時間:" + cus[j].time + "分鐘");                       
  124.                                         }
  125.                                 }
  126.                                 System.out.println("◎餐廳資訊");
  127.                                 System.out.println("目前用餐人數:" + num + "人");
  128.                                 int total = 0;
  129.                                 int totaltime = 0;
  130.                                 for (int j = 0; j < 100; j++)
  131.                                 {
  132.                                         if (cus[j].time <= 0)
  133.                                         {
  134.                                                 total = total + cus[j].price;
  135.                                         }
  136.                                 }
  137.                                 System.out.println("總營業額:" + total + "元");
  138.                                
  139.                                 for (int j = 0; j < 100; j++)
  140.                                 {
  141.                                         totaltime = totaltime + cus[j].passtime;
  142.                                 }
  143.                                 System.out.println("總營業時間:" + totaltime + "分鐘");      
  144.                                       
  145.                                 if (rain.equals("yes"))
  146.                                 {
  147.                                         System.out.println("天氣狀況:" + "雨天");
  148.                                 }
  149.                                 if (rain.equals("no"))
  150.                                 {
  151.                                         System.out.println("天氣狀況:" + "無雨");
  152.                                 }
  153.                         }
  154.                                
  155.                         if (sorder[0].equals("exit"))
  156.                         {
  157.                                 cus[i] = new customer();
  158.                                 System.out.println("◎餐廳資訊");
  159.                           System.out.println("目前用餐人數:" + num + "人");
  160.                                 int total = 0;
  161.                                 int totaltime = 0;      
  162.                                 for (int j = 0; j < 100; j++)
  163.                                 {
  164.                                         if (cus[j].time <= 0)
  165.                                         {
  166.                                                 total = total + cus[j].price;
  167.                                         }
  168.                                 }
  169.                                 System.out.println("總營業額:" + total + "元");      
  170.                                 for (int j = 0; j < 100; j++)
  171.                                 {
  172.                                         totaltime = totaltime + cus[j].passtime;
  173.                                 }      
  174.                                 System.out.println("總營業時間:" + totaltime + "分鐘");      
  175.                                       
  176.                                 if (rain.equals("yes"))
  177.                                 {
  178.                                         System.out.println("天氣狀況:" + "雨天");
  179.                                 }
  180.                                 if (rain.equals("no"))
  181.                                 {
  182.                                         System.out.println("天氣狀況:" + "無雨");
  183.                                 }
  184.                                 break;
  185.                         }
  186.                 }
  187.         }
  188. }
  189.        
複製代碼
已有 1 人評分威望 收起 理由
又靜 + 2 感謝您熱心幫助會員解決問題,論壇需要您的.

總評分: 威望 + 2   查看全部評分

感同身受,就能放下執著
請注意︰利用多帳號發表自問自答的業配文置入性行銷廣告者,將直接禁訪或刪除帳號及全部文章!
您需要登錄後才可以回覆 登入 | 註冊


本論壇為非營利自由討論平台,所有個人言論不代表本站立場。文章內容如有涉及侵權,請通知管理人員,將立即刪除相關文章資料。侵權申訴或移除要求:abuse@oursogo.com

GMT+8, 2024-5-18 21:05

© 2004-2024 SOGO論壇 OURSOGO.COM
回頂部