2017-06-24 76 views
-2
public class Exam 
{ 
    public static void main (String args []) 
    { 
     int exam [] = new int [] {45, 58, 21, 92, 47, 33}; 
     System.out.println("Exam Results: "); 
     for(int i=0; i<exam.length; i++) 
     { 
      if(exam[i]>=40) 
       System.out.println(" - Pass "); 
      else if(exam[i]<=40) 
       System.out.println(" - Fail "); 
      System.out.println(exam[i]); 
     } 
    } 
} 

上面的代碼完成了它應該做的事情,但我無法得到格式化權限的輸出。需要幫助格式化這個輸出java

這就是我想要打印出上面的代碼。

Exam Results: 

45 - Pass 

58 - Pass 

21 - Fail 

92 - Pass 

47 - Pass 

33 - Fail 
+0

預期結果表明每行應以點數開始。在那種情況下,爲什麼您決定在每次迭代結束時將代碼負責?你也熟悉'print'和'println'之間的區別嗎? – Pshemo

回答

1

爲什麼你不使用:

String s = "";//create a variable 
for (int i = 0; i < exam.length; i++) { 
    if (exam[i] >= 40) { 
     s = " - Pass ";//set the value 
    } else if (exam[i] <= 40) { 
     s = " - Fail "; 
    } 
    System.out.println(exam[i] + s);//print it in the end 
} 

輸出

Exam Results: 
45 - Pass 
58 - Pass 
21 - Fail 
92 - Pass 
47 - Pass 
33 - Fail 

否則容易,您可以使用:

int exam[] = new int[]{45, 58, 21, 92, 47, 33}; 
for(int i : exam){ 
    System.out.println(i + (i >= 40 ? " - Pass " : " - Fail ")); 
} 
0

打印結果的PA前SS /失敗和使用print代替println

for (int i = 0; i < exam.length; i++) { 
    System.out.print(exam[i]); 
    if (exam[i] >= 40) 
     System.out.println(" - Pass "); 
    else if (exam[i] <= 40) 
     System.out.println(" - Fail "); 
} 

打印:

Exam Results: 
45 - Pass 
58 - Pass 
21 - Fail 
92 - Pass 
47 - Pass 
33 - Fail 

甚至用這一個班輪:

for (int i = 0; i < exam.length; i++) 
    System.out.println(exam[i] + (exam[i] >= 40 ? " - Pass " : " - Fail ")); 
} 
+0

感謝@Sevastyan的幫助。我知道修復是一些小問題,但我無法弄清格式。非常感謝您的快速響應。 –

+0

@MarkDoherty我認爲你提到了錯誤的名字,但我很高興我的回答幫助你:) – mumpitz

0

這應該工作。我移動了一些代碼。您需要移動一些打印線和實際打印的等級。

public class Exam 
{ 
public static void main (String args []) 
    { 
    int exam [] = new int [] {45, 58, 21, 92, 47, 33}; 
    System.out.println("Exam Results: "); 
    for(int i=0; i<exam.length; i++) 
    { 
     System.out.print(exam[i]); 

    if(exam[i]>=40) 
     System.out.println(" - Pass "); 
    else if(exam[i]<=40) 
     System.out.println(" - Fail "); 
    } 
    } 
} 
0

試試這個:

public class Exam 
{ 
public static void main (String args []) 
    { 
    int exam [] = new int [] {45, 58, 21, 92, 47, 33}; 
    System.out.println("Exam Results: "); 
    for(int i=0; i<exam.length; i++) { 
     if(exam[i]>=40) { 
      System.out.println(exam[i] + " - Pass "); 
      continue; 
     } 
     System.out.println(exam[i] + " - Fail "); 
    } 
    } 
} 
0

這將產生正是輸出:

public class Exam 
{ 
public static void main (String args []) 
    { 
    int exam [] = new int [] {45, 58, 21, 92, 47, 33}; 
    System.out.println("Exam Results: "); 
    for(int i=0; i<exam.length; i++) 
    { 
     System.out.println(""); 
     System.out.print(exam[i]); 

    if(exam[i]>=40) 
     System.out.println(" - Pass "); 
    else if(exam[i]<=40) 
     System.out.println(" - Fail "); 
    } 
    } 
} 

注意System.out.print新行不追加到輸出

輸出:

Exam Results: 

45 - Pass 

58 - Pass 

21 - Fail 

92 - Pass 

47 - Pass 

33 - Fail 
0

你需要做的是在OOP方式

public final class Result { 
    private final int result; 

    public Result(int result) { 
     this.result = result; 
    } 

    public String formatted() { 
     if (result >= 40) { 
      return result + " - Pass"; 
     } else { 
      return result + " - Fail"; 
     } 
    } 

類ExamResults:

public final class ExamResults { 
    private final int[] rawResults; 

    public ExamResults(int[] rawResults) { 
     this.rawResults = rawResults; 
    } 

    public List<Result> asList() { 
     List<Result> examResults = new ArrayList<>(); 
     for (int result : rawResults) { 
      examResults.add(new Result(result)); 
     } 
     return examResults; 
    } 

    public void printTo(PrintWriter output) { 
     output.append("Exam Results:"); 
     output.println(); 
     Iterator<Result> results = asList().iterator(); 
     while (results.hasNext()) { 
      output.append(results.next().formatted()); 
      if (results.hasNext()) { 
       output.println(); 
      } 
     } 
     output.flush(); 
     output.close(); 
    } 
} 

類主營:

public class Main { 

    public static void main(String[] args) { 
     int[] rawResults = {20, 30, 40, 60, 95}; 
     ExamResults examResults = new ExamResults(rawResults); 
     examResults.printTo(new PrintWriter(System.out)); 
    } 
} 

輸出將是:

Exam Results: 
20 - Fail 
30 - Fail 
40 - Pass 
60 - Pass 
95 - Pass