Java格式已经过时了

时间:2017-02-19 21:32:06

标签: java

我正在编写一个程序,将数据输入到程序中并创建一个检查报告,但是格式化很远,无法找到它的确切内容。这是我的结果的屏幕截图,下面的代码如下,以及格式应该是什么样子。 https://gyazo.com/4f4f4d6c43b4e0c3545a48f9e06565b5

/* 
*   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 >*");
}
}

1 个答案:

答案 0 :(得分: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,请不要忘记ln中的println表示它会添加换行符。如果您不想添加换行符,请print。而不是

System.out.println("~~ Invalid data~~\n");
你可能想写

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