2015-11-17 22 views
0

嗨,大家好,請幫助我!對不起,如果標題中不夠清楚。我的英語非常有限。我無法在此列表中添加其他信息給聯繫人

所以我有這樣的Java代碼,它有這樣的工作:

MENU:

1 - 註冊用戶信息

2 - 註冊客戶航空公司里程

3 - 列出特定客戶的英里數

4 - 列出客戶最多和最少里程

5 - 列出每個人的名字和他們的英里

我可以註冊自己的信息就好了,但我似乎無法登記他們英里出於某種原因。

我輸入1鍵入信息,輸入完所有內容後,它回到菜單,我輸入2,它應該做的是:請求一個我之前已經註冊過的人的名字,然後我註冊他們的航空公司里程。但是當我輸入2來做這件事時,它甚至不讓我輸入一個名字。它只是給

這將是有問題的代碼塊中的「其他」條件「找不到名稱」:

 case 2: 
      System.out.println("customer name: "); 
      name = scanner.nextLine(); 
      d=0; 

      while (d < c-1 && name!=data1[d]) 
      {d++;} 

      if (name==data1[d]) 
      { 
       System.out.println("enter airline miles of " +data1[d]); 
       airMiles = scanner.nextDouble(); 

       airMile[d] = airMile[d]+airMiles; 
      } 

      else 

      {System.out.println("name not found");} 
      break; 

而且這是在情況下,整個代碼任何你想取看。

import java.util.Scanner; 
public class Vetor49eng { 
public static void main(String[] args) { 
    Scanner scanner = new Scanner (System.in); 

    int op, c, d, posLast, posFirst; 
    String name, filler; 
    double airMiles; 
    String [] data1 = new String [50]; 
    String [] data2 = new String [50]; 
    String [] data3 = new String [50]; 
    double [] airMile = new double [50]; 

    for (c=0; c<5; c++) 
    {airMile[c]=0.;} 
    c=0; 

    do 
    { 
     System.out.println("1. register customer info"); 
     System.out.println("2. register customer airline miles"); 
     System.out.println("3. show miles of a customer"); 
     System.out.println("4. show the names of the customer with the most miles and least miles"); 
     System.out.println("5. show all their names and their miles"); 
     System.out.println("6. exit"); 
     op = scanner.nextInt(); 

     switch (op) 
     { 
     case 1: 
      if(c<50) 
      { 
       System.out.println("customer " +(c+1) +": "); 
       filler = scanner.nextLine(); //IT ALWAYS SKIP THIS FIRST LINE SO I CREATED THIS FILLER 

       System.out.println("name: "); 
       data1[c] = scanner.nextLine(); 

       System.out.println("address: "); 
       data2[c] = scanner.nextLine(); 

       System.out.println("telephone number: "); 
       data3[c] = scanner.nextLine(); 

       c++; 
      } 

      else 

      {System.out.println("archive complete");} 
      break; 

     case 2: 
      System.out.println("customer name: "); 
      name = scanner.nextLine(); 
      d=0; 

      while (d < c-1 && name!=data1[d]) 
      {d++;} 

      if (name==data1[d]) 
      { 
       System.out.println("enter airline miles of " +data1[d]); 
       airMiles = scanner.nextDouble(); 

       airMile[d] = airMile[d]+airMiles; 
      } 

      else 

      {System.out.println("name not found");} 
      break; 

     case 3: 
      System.out.println("customer name: "); 
      name = scanner.nextLine(); 
      d=0; 

      while (d<c-1 && name!=data1[d]) 
      {d++;} 

      if (name==data1[d]) 

      {System.out.println("miles of " +data1[d] +": " +airMile[d]);} 

      else 

      {System.out.println("name not found");} 
      break; 

     case 4: 
      d=1; 
      posLast=0; 
      posFirst=0; 

      while (d<=c) 
      { if (airMile[d] > airMile[posFirst]) 
      { posFirst=d; } 
      else 
      { if (airMile[d] < airMile[posLast]) 
      { posLast = d; } 
      } 
      d++; 
      } 
      System.out.println("customer with the most mileage:"); 
      System.out.println("name: " +data1[posFirst]); 
      System.out.println("address: " +data2[posFirst]); 
      System.out.println("phone number: " +data3[posFirst]); 
      System.out.println("miles: " +airMile[posFirst]); 
      System.out.println(); 
      System.out.println("customer with the least mileage:"); 
      System.out.println("name: " +data1[posLast]); 
      System.out.println("address: " +data2[posLast]); 
      System.out.println("phone number: " +data3[posLast]); 
      System.out.println("miles: " +airMile[posLast]); 
      break; 

     case 5: 
      System.out.println("list"); 
      for (d=0; d<c; d++) 
      {System.out.println(d +" - " +data1[d] +": " +airMile[d]);} 
      break; 

     case 6: 
      System.out.println("have a nice flight"); 
      break; 
     default: 
      System.out.println("option not available"); 
     } 
    } 
    while (op!=6); 
    System.out.println(); 
} 
} 

對不起,我的英文和佔用你的時間。我非常感謝這裏的幫助。我(非常)新來這個。

+0

也http://stackoverflow.com/q/13102045/4125191 – RealSkeptic

+0

見我不知道這是問題。否則我會用搜索。對不起。 –

回答

0

那麼這個代碼塊有一些東西。在比較STRINGS時,您不希望使用==。兩個字符串不是同一個對象,雖然它們可能具有相同的值,但它們是完全不同的對象,因此當您嘗試驗證輸入時會出現錯誤。

對於要使用.equals()如果情況很重要的字符串,或者如果您不關心套管,則使用.equalsIgnoreCase()。

現在爲您的問題跳過,您需要在這裏使用= scanner.next()。使用nextLine會放棄所有條目,並自動使您的條目爲空。

  case 2:      
       System.out.print("customer name: "); 
       name = scanner.next(); 
       d = 0; 

       while (d < c - 1 && !name.equalsIgnoreCase(data1[d])) { 
        d++; 
       } 

       if (name.equalsIgnoreCase(data1[d])) { 
        System.out.println("enter airline miles of " + data1[d]); 
        airMiles = scanner.nextDouble(); 

        airMile[d] = airMile[d] + airMiles; 
       } else { 
        System.out.println("name not found"); 
       } 
       break; 
+0

你是一個該死的救世主!謝謝你,兄弟!! –

+0

不用擔心的人。也絕對要看看你被標記的那個笨蛋。這將極其有益。 – basic

相關問題