2016-03-29 38 views
0

enter image description herekullanma Anahtarlar içeride geçer: Java

Kullanıcıların tanı sorular sorar ve sonra başka bir soru sorar bir program oluşturma, yeni bir soru daha önceki bir yanıta bağlı seçilir. Bir ders kitabındaki Bölüm ödevinden alınan görüntüye dayanmaktadır. Çalışacak programı aldım, ama benim için çözümüm verimsiz görünüyor, görevimi tamamlamanın daha verimli bir yolu olup olmadığını merak ediyorum. Programın amacı, kullanıcı belirtileri almak ve daha sonra bu belirtilere dayalı bir tanı sağlamaktır. Benim içimde o kadar çok anahtarın olması dağınık görünüyor.

import java.util.*; 

    public class Diagnostic2 { 

//main(): application entry point 
public static void main(String[] args) { 
    System.out.println("\nFever Diagnostic Tool"); 
    System.out.println("====================="); 
    System.out.println("\nDisclaimer: This tool is meant primarily to act as an indicator "); 
    System.out.println("of possible causes of fever symptoms. It is not meant to replace"); 
    System.out.println("professional medical advise. If you believe you are sick "); 
    System.out.println("consult with your doctor about your symptoms."); 

    Scanner stdin = new Scanner(System.in); 
    System.out.print("\nDo you have a fever? (y/n): "); 
    char fever = stdin.next().trim().charAt(0); 

    String diagnosis = "Insufficient information to make diagnosis\n"; 
    char cough = 'n'; 
    char wheeze = 'n'; 
    char headache = 't'; 
    char achyJoints = 'n'; 
    char rash = 'n'; 
    char soreThroat = 'n'; 
    char backPain = 'n'; 
    char urinaryPain = 'n'; 
    char tooHot = 'n'; 
    char diarrhea = 'n'; 
    char vomit = 'n'; 

    switch (fever) { 
    case 'y': 
     System.out.print("\nAre you coughing? (y/n): "); 
     cough = stdin.next().trim().charAt(0); 
     switch (cough) { 
      case 'y': 
      System.out.print("\nAre you short of breath, wheezing, or coughing up phlegm? (y/n): "); 
      wheeze = stdin.next().trim().charAt(0); 
      switch (wheeze) { 
       case 'y': 
        diagnosis = "Possibilites include pneumonia or infection of airways.\n"; 
        break; 
       case 'n': 
        System.out.print("\nDo you have a headache? (y/n): "); 
        headache = stdin.next().trim().charAt(0); 
        switch(headache) { 
         case 'y': 
         diagnosis = "Possibilites include viral infection.\n"; 
         break; 
         case 'n': 
         System.out.print("\nDo you have a aching bones or joints? (y/n): "); 
         achyJoints = stdin.next().trim().charAt(0); 
         switch(achyJoints) { 
          case 'y': 
           diagnosis = "Possibilites include viral infection.\n"; 
           break; 
          case 'n': 
           System.out.print("\nDo you have a rash? (y/n): "); 
           rash = stdin.next().trim().charAt(0); 
           switch(rash) { 
            case 'y': 
            diagnosis = "Insufficient information to list possibilities, consult with doctor.\n"; 
            break; 
            case 'n': 
            System.out.print("\nDo you have a sore throat? (y/n): "); 
            soreThroat = stdin.next().trim().charAt(0); 
            switch(soreThroat) { 
             case 'y': 
              diagnosis = "Possibilites include throat infection.\n"; 
              break; 
             case 'n': 
              System.out.print("\nDo you have back pain just above the waist with chills and fever? (y/n): "); 
              backPain = stdin.next().trim().charAt(0); 
              switch(backPain) { 
               case 'y': 
               diagnosis = "Possibilites include kidney infection.\n"; 
               break; 
               case 'n': 
               System.out.print("\nDo you have pain urinating or are urinating more often? (y/n): "); 
               urinaryPain = stdin.next().trim().charAt(0); 
               switch(urinaryPain) { 
                case 'y': 
                 diagnosis = "Possibilites include urinary tract infection.\n"; 
                 break; 
                case 'n': 
                 System.out.print("\nHave you spent the day in the sun or hot conditions? (y/n): "); 
                 tooHot = stdin.next().trim().charAt(0); 
                 switch(tooHot) { 
                  case 'y': 
                  diagnosis = "Possibilites include sun stroke or heat exhaustion.\n"; 
                  break; 
                  case 'n': 
                  break; 
                 } 
                } 
               } 
              } 
             } 
            } 
           } 
          } 
          break;      
      case 'n': 
      System.out.print("\nDo you have a headache? (y/n): "); 
      headache = stdin.next().trim().charAt(0); 
      switch(headache) { 
       case 'y': 
        System.out.println("\nAre you experiencing any of the following: pain when"); 
        System.out.println("bending your head forward, nausea or vomiting, bright"); 
        System.out.print("light hurting your eyes, drowsiness, or confusion? (y/n): "); 
        vomit = stdin.next().trim().charAt(0); 
        switch(vomit){ 
        case 'y': 
         diagnosis = "Possibilites include meningitis.\n"; 
         break; 
        case 'n': 
         System.out.print("\nAre you vomiting, or have had diarrhea? (y/n): "); 
         diarrhea = stdin.next().trim().charAt(0); 
         switch(diarrhea) { 
          case 'y': 
           diagnosis = "Possibilites include digestive tract infection.\n"; 
           break; 
          case 'n': 
           System.out.print("\nDo you have a aching bones or joints? (y/n): "); 
           achyJoints = stdin.next().trim().charAt(0); 
           switch(achyJoints) { 
           case 'y': 
            diagnosis = "Possibilites include viral infection.\n"; 
            break; 
           case 'n': 
            System.out.print("\nDo you have a rash? (y/n): "); 
            rash = stdin.next().trim().charAt(0); 
            switch(rash) { 
             case 'y': 
              diagnosis = "Insufficient information to list possibilities, consult with doctor.\n"; 
              break; 
             case 'n': 
              System.out.print("\nDo you have a sore throat? (y/n): "); 
              soreThroat = stdin.next().trim().charAt(0); 
              switch(soreThroat) { 
              case 'y': 
               diagnosis = "Possibilites include throat infection.\n"; 
               break; 
              case 'n': 
               System.out.print("Do you have back pain just above the waist with chills and fever? (y/n): "); 
               backPain = stdin.next().trim().charAt(0); 
               switch(backPain) { 
                case 'y': 
                 diagnosis = "Possibilites include kidney infection.\n"; 
                 break; 
                case 'n': 
                 System.out.print("\nDo you have pain urinating or are urinating more often? (y/n): "); 
                 urinaryPain = stdin.next().trim().charAt(0); 
                 switch(urinaryPain) { 
                 case 'y': 
                  diagnosis = "Possibilites include urinary tract infection.\n"; 
                  break; 
                 case 'n': 
                  System.out.print("\nHave you spent the day in the sun or hot conditions? (y/n): "); 
                  tooHot = stdin.next().trim().charAt(0); 
                  switch(tooHot) { 
                   case 'y': 
                    diagnosis = "Possibilites include sun stroke or heat exhaustion.\n"; 
                    break; 
                   case 'n': 
                    break; 
                  } 
                 } 
                } 
               } 
              } 
             } 
            } 
           } 
          break; 
       case 'n': 
        System.out.print("\nDo you have a aching bones or joints? (y/n): "); 
           achyJoints = stdin.next().trim().charAt(0); 
           switch(achyJoints) { 
           case 'y': 
            diagnosis = "Possibilites include viral infection.\n"; 
            break; 
           case 'n': 
            System.out.print("\nDo you have a rash? (y/n): "); 
            rash = stdin.next().trim().charAt(0); 
            switch(rash) { 
             case 'y': 
              diagnosis = "Insufficient information to list possibilities, consult with doctor.\n"; 
              break; 
             case 'n': 
              System.out.print("\nDo you have a sore throat? (y/n): "); 
              soreThroat = stdin.next().trim().charAt(0); 
              switch(soreThroat) { 
              case 'y': 
               diagnosis = "Possibilites include throat infection.\n"; 
               break; 
              case 'n': 
               System.out.print("\nDo you have back pain just above the waist with chills and fever? (y/n): "); 
               backPain = stdin.next().trim().charAt(0); 
               switch(backPain) { 
                case 'y': 
                 diagnosis = "Possibilites include kidney infection.\n"; 
                 break; 
                case 'n': 
                 System.out.print("\nDo you have pain urinating or are urinating more often? (y/n): "); 
                 urinaryPain = stdin.next().trim().charAt(0); 
                 switch(urinaryPain) { 
                 case 'y': 
                  diagnosis = "Possibilites include urinary tract infection.\n"; 
                  break; 
                 case 'n': 
                  System.out.print("\nHave you spent the day in the sun or hot conditions? (y/n): "); 
                  tooHot = stdin.next().trim().charAt(0); 
                  switch(tooHot) { 
                   case 'y': 
                    diagnosis = "Possibilites include sun stroke or heat exhaustion.\n"; 
                    break; 
                   case 'n': 
                    break; 
                  } 
                 } 
                } 
               } 
              } 
             } 

      } 
     } 

    case 'n': 
     break; 
    } 


    System.out.println("--------------------------------------------------------------"); 
    System.out.println("\nSymptoms"); 
    if(fever == 'y') { 
     System.out.println("*\tFever"); 
    } 
    if(cough == 'y') { 
     System.out.println("*\tCough"); 
    } 
    if(wheeze == 'y') { 
     System.out.println("*\tWheezing"); 
    } 
    if(headache == 'y') { 
     System.out.println("*\tHeadache"); 
    } 
    if(achyJoints == 'y') { 
     System.out.println("*\tAchy joints or bones"); 
    } 
    if(rash == 'y') { 
     System.out.println("*\tRash"); 
    } 
    if(soreThroat == 'y') { 
     System.out.println("*\tSore throat"); 
    } 
    if(backPain == 'y') { 
     System.out.println("*\tBack pain"); 
    } 
    if(urinaryPain == 'y') { 
     System.out.println("*\tFrequent urination or pain urinating"); 
    } 
    if(tooHot == 'y') { 
     System.out.println("*\tDay spent in hot conditions"); 
    } 
    if(diarrhea == 'y') { 
     System.out.println("*\tDiarrhea"); 
    } 
    if(vomit == 'y') { 
     System.out.println("*\tPain when bending head forward, nausea or vomiting,"); 
     System.out.println("\tbright light hurting eyes, drowsiness or confusion."); 
    } 
    System.out.println("\nDiagnosis"); 
    System.out.println("\t" + diagnosis); 

} 
} 
+1

"Anahtar" (lar) ı yuvalamayın. İşleri yollara ayırmanı öneririm. –

+0

Woah! Etkileyici anahtar piramitleri! Ama seni reddetmeliyim ve modele bir kaynak dosyadan yüklenerek model odaklı bir durum otomasyonu kullanmayı öneriyorum. –

+2

Bu soru codereview.stackexchange.com adresine taşınmalıdır – forgivenson

cevap

1

Bunu yapmak için pek çok iyi yol var, yuva anahtarlarının bir olmadığını düşünüyorum. Bu tür bir uygulama için, daha sonra daha kolay bir şekilde dönüştürebileceğiniz gibi, veriye dayalı bir model kullanmak daha iyidir. Sadece mevcut kodu göz önünde bulundurun, ders kitabındaki grafiğe geri dönüştürmek kolay bir şekilde otomatikleştirilemez. Daha iyi bir yol göstermeye çalışıyorum. Bu modelde, bellekteki model ile, yukarıdaki yuvalanmış Java kodunu oluşturmak için bir program bile yazabilirsiniz.

package hu.hege; 

import java.util.Scanner; 

public class State { 

    public final boolean terminal; 
    public final String message; 
    public final State positive; 
    public final State negative; 

    public State(boolean terminal, String message, State positive, State negative) { 
     this.terminal = terminal; 
     this.message = message; 
     this.positive = positive; 
     this.negative = negative; 
    } 

    public State(String message) { 
     this(true, message, null, null); 
    } 

    public State(String message, State positive, State negative) { 
     this(false, message, positive, negative); 
    } 

    public static State buildGraph() { 
     // It's recommended to engineer some file format, and read the graph 
     // structure from a resource file here 

     State insufficient = new State("Insuficcient information to list possibilities"); 
     State further = new State("Continue to ask questions here..."); 
     State qCough = new State("Are you coughing?", further, insufficient); 
     State qFever = new State("Do you have a fever?", qCough, insufficient); 

     // return entry question 
     return qFever; 
    } 

    public static void main(String[] args) { 
     State current = buildGraph(); 
     Scanner stdin = new Scanner(System.in); 
     for (;;) { 
      System.out.println(current.message); 
      if (current.terminal) 
       break; 
      String choice = stdin.next().trim(); 
      if (choice.startsWith("y")) { 
       current = current.positive; 
      } else if (choice.startsWith("n")) { 
       current = current.negative; 
      } else { 
       System.out.println("Unrecognized answer"); 
      } 
     } 
     stdin.close(); 
    } 

} 

Bu kod temelde ders kitabı olarak bellekte aynı grafiği kurar ve bir bilgisayar programı olsaydı olarak yorumlar: Ben böyle bir şey önermek. Amacımız açık ve sürdürülebilir (ama biraz daha yavaş) bir kod yazmak olduğu zaman, bu tür bir yorumu yaparız.

+0

Güzel bir gösteri. OP'nin sağladığı diyagramın küçük bir sorunu, “hayır” ın iki farklı soruyla sonuçlandığı bir örnek içermesidir, bu yüzden grafik böyle bir durumu ele almalıdır. Örneği genişletmek için büyük bir anlaşma değil. – KevinO

+0

Henüz kendimi öğrenmeye çalışıyorum, kendimi Java öğretmeye çalışıyorum. Eyaletler hakkında biraz daha fazla şey öğrenmek için iyi kaynaklar önerebilir misiniz? –

İlgili konular