2017-02-19 44 views
0

我正在編寫一個程序,它將數據輸入到程序中並創建一個檢查報告,但是格式化不可行,並且無法準確找到它的內容。下面是我的結果截圖,下面是以下代碼,以及格式應該是什麼樣子。 https://gyazo.com/4f4f4d6c43b4e0c3545a48f9e06565b5Java格式是關閉的

/* 
* file : 
* name : 
* course : 
* description: Process input file to print formatted data and summary data 
*/ 

package program04; 

import java.util.Scanner; 

public class Program04 
{ 

// TODO Declare and initialise class variables here 

/** 
* @param args 
*/ 
public static void main(String[] args) 
{ 

    // TODO: Declare and initialise local variables here: 
    // isValid 

    boolean isValid; 

    String patientName; 
    int patientID; 
    String gender; 
    float exam1, exam2, exam3; 
    double avg; 
    String pos_neg; // will always be a color 


    // System.out.println top line of report 
    System.out.println("*~~< Patient Exam Report >~~*"); 
    // System.out.println blank line 
    System.out.println(""); 
    // Name  Pat. M/F Exam1  Exam2  Exam3  AVG Risk Level 
    // ----  ---- --- ----- -----  ----- ----- ---------- 
    // print words 
    // print column headers; 
    System.out.printf("%-12s", "Name"); 
    System.out.printf("%-13s", "Pat."); 
    System.out.printf("%-8s", "M/F"); 
    System.out.printf("%-8s", "Exam1"); 
    System.out.printf("%-8s", "Exam2"); 
    System.out.printf("%-8s", "Exam3"); 
    System.out.printf("%-6s", "AVG"); 
    System.out.printf("%-13s", "Risk Level \n"); 
    // print dashes 
    System.out.printf("%-11s", "----"); 
    System.out.printf("%-12s", "----"); 
    System.out.printf("%-7s", "---"); 
    System.out.printf("%-7s", "----"); 
    System.out.printf("%-7s", "----"); 
    System.out.printf("%-7s", "----"); 
    System.out.printf("%-5s", "----"); 
    System.out.printf("%-12s", "----------\n"); 

    Scanner input = new Scanner(System.in); 

      // Start for loop1 
      for (int i = 0; i < 10; i++) {     

    // input(patientName)/print; 
    patientName = input.next(); 
    System.out.printf("%-12s", patientName); 

    // input(patientID) 
    patientID = input.nextInt(); 
    System.out.printf("%-13s", patientID); 

    // input(gender); 
    gender = input.next(); 
    System.out.printf("%-8s", gender); 

    // input(exam1); 
    exam1 = input.nextFloat(); 
    System.out.printf("%-8.2f", exam1); 

    // input(exam2); 
    exam2 = input.nextFloat(); 
    System.out.printf("%-8.2f", exam2); 

    // input(exam3); 
    exam3 = input.nextFloat(); 
    System.out.printf("%-8.2f", exam3); 


      // Create isValid variable to equal true unless if/else statement fails it changes to false 
      // If true, do nothing... If false, Print "Invalid data" 
    isValid = true;     
    if (1111 <= patientID && patientID <= 9999) { 
    } 
      else { 
     isValid = false; 
    } 
    if (0.00 <= exam1 && exam1 <= 100.00) { 
    } 
      else { 
     isValid = false; 
    } 
    if (0.00 <= exam2 && exam2 <= 100.00) { 
    } 
      else { 
     isValid = false; 
    } 
    if (0.00 <= exam3 && exam3 <= 100.00) { 
    } 
      else { 
     isValid = false; 
    } 

    if (isValid == true) {     
      // Find Average 
      avg = (exam1 + exam2 + exam3)/3; 
      System.out.printf("%-6.2f", avg); 
      // If else statement to decide if average is above X number Print Y 
      if (avg >= 97) { 
       System.out.printf("%-13s", "RED\n"); 
      } else if (avg > 88) { 
       System.out.printf("%-13s", "ORANGE\n"); 
      } 
      else if (avg > 78) { 
       System.out.printf("%-13s", "YELLOW\n"); 
      } 
      else if (avg > 70) { 
       System.out.printf("%-13s", "BLUE\n"); 
      } 
      else { 
       System.out.printf("%-13s", "GREEN\n"); } 
    } 
      else { 
     System.out.println("~~ Invalid data~~\n"); 
    } 
      } // end loop1 

      System.out.println(" "); 
      System.out.println("Summary\n"); 


      System.out.println(" "); 
      System.out.println("*< end of report >*"); 
} 
} 
+0

是否有可能,一些病人的名字已前導空白字符? –

+0

我不這麼認爲,我使用的輸入文件名爲input.data和https://gyazo.com/33a83332e1cc5686d4afc2bf27757719的完整副本粘貼 – Devin

回答

0

當你寫

System.out.printf("%-13s", "RED\n"); 

你問的Java長度13的列左對齊的字符串"RED\n",用空格填充其餘部分。結果是"RED\n ",它爲下一行的開頭增加了空格。

System.out.printf("%-13s\n", "RED"); 

或單獨結束行:

System.out.printf("%-13s", "RED"); 
System.out.println(); 

既然你只需添加一個字符串

您可以通過將新行格式的字符串,其中屬於解決這個問題到行尾,您並不需要格式字符串,而只需寫入:

System.out.println("RED"); 

說到println,不要忘了lnprintln意味着它添加一個換行符。有print如果你不想添加一個換行符。因此,而不是

System.out.println("~~ Invalid data~~\n"); 

你大概的意思是寫

System.out.println("~~ Invalid data~~"); 
+0

謝謝,這使得更有意義並解決了我的問題! – Devin