2014-08-31 67 views
-1

循環這些語句的任何幫助。我對此很陌生,而且遇到了一些麻煩。我正在努力做到這一點,以便在給予班級平均分數之前,成績簿將五個孩子的成績。只需要幫助循環。循環輸入和語句的列表

import java.util.Scanner; 
public class gradebook { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     float discussionweight, hwweight, examweight, discussion, hw, exam, finalaverage, again; 
     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter the students name: "); 
     String student = scan.next(); 

     System.out.print ("Enter the weight of the discussions as an integer: "); 
     discussionweight = scan.nextFloat(); 

     // Prompts for the discussions grade 
     System.out.print ("Enter the score of the discussion as an integer: "); 
     discussion = scan.nextFloat(); 

     // Prompts for the weight of the homework grade in an integer 
     System.out.print ("Enter the weight of the homework as an integer: "); 
     hwweight = scan.nextFloat(); 

     // Prompts for hw grade 
     System.out.print ("Enter the hw grade: "); 
     hw = scan.nextFloat(); 

     System.out.print("Enter the weight of the exam as an integer"); 
     examweight = scan.nextFloat(); 

     System.out.print("Enter the exam grade"); 
     exam = scan.nextFloat(); 




     // Calculate and print the final, weighted average. 
     finalaverage = (((discussionweight * discussion) + (hw * hwweight) + (exam * examweight))/100); 

     if (finalaverage >= 90) 
     System.out.println(student +"'s " + "final grade is an A."); 
     else if (finalaverage >= 80) 
     System.out.println(student +"'s " + "final grade is a B."); 
     else if (finalaverage >= 70) 
     System.out.println(student +"'s " + "final grade is a C."); 
     else if (finalaverage >= 60) 
     System.out.println(student +"'s " + "final grade is a D."); 
     else if (finalaverage >= 10) 
     System.out.println(student +"'s " + "final grade is an F."); 


     System.out.println ("The final average is "+ finalaverage); 
     System.out.println ("Would you like to continue? Enter 0 to exit and anything else to continue."); 
     String again = scan.nextFloat(); 

     while (again != 0) { 
      finalaverage += again; 
     } 



    } 
+0

如果你想整個事情循環 - 就像從'浮discussionWeight ...''直到再次字符串= scan.nextFloat()',那麼你應該附上所有在循環塊中的那些語句。因此,底部的'while'循環應該包含所有上面的語句。 – jackarms 2014-08-31 04:19:43

+0

嗯 - 對於你的程序不會編譯 - ''再次'被聲明兩次... – jdphenix 2014-08-31 04:21:19

+0

我建議你把它分解成更易於管理的方法。 – 2014-08-31 05:18:24

回答

1

做這種方式 當你想繼續一次又一次,你需要循環,直到出境成爲真正的條件下使用的邏輯。在這種情況下,循環將繼續執行,直到用戶輸入爲0,然後程序退出。

while(true) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Enter the students name: "); 
    String student = scan.nextLine();//change to nextLine() 
    // rest all same 
    System.out.println("The final average is " + finalaverage); 
    System.out.println("Would you like to continue? Enter 0 to exit 
            and anything else to continue."); 
    again = scan.nextFloat(); 
    if(again == 0)// if input is zero it will continue or else it will exit 
     break; 
} 

改變這個String again = scan.nextFloat();again = scan.nextFloat(); 因爲你已經通過名稱的變量再次float類型此外nextFloat()爲您提供了float類型的輸入,然後你爲什麼需要在這種情況下字符串。

+0

不是給出答案,而是幫助OP瞭解他的問題。他或她甚至不知道字符串和浮點類型之間的區別。 – 2014-08-31 04:26:35

+0

@KickButtowski抱歉,我不知道,我認爲她只是被困在了一邊的東西 – SparkOn 2014-08-31 04:37:34

+0

這是k沒必要對不起。如果我是,我會拿出我的代碼 – 2014-08-31 04:38:47

1

你不能在Java中重新聲明一個與你之前聲明的同名的變量。在你的程序String again = scan.nextFloat();你試圖做到這一點。而且你正在嘗試給一個不允許的字符串變量賦值一個浮點值。關於循環的問題,您可以使用一個簡單的while(..)循環,如下面的代碼所示,以繼續處理另一個學生數據。我不確定你是否需要finalaverage+= again;。請檢查一下。

import java.util.Scanner; 


public class gradebook { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     float discussionweight, hwweight, examweight, discussion, hw, exam, finalaverage, again ; 

     do { 

      Scanner scan = new Scanner(System.in); 
      System.out.print("Enter the students name: "); 
      String student = scan.next(); 

      ....// same as your code. 

      System.out.println ("The final average is "+ finalaverage); 
      System.out.println ("Would you like to continue? Enter 0 to exit and anything else to continue."); 
      again = scan.nextFloat(); 


      finalaverage += again; 
     } while (again != 0); 

    } 
} 
+1

而不是給出答案,幫助OP瞭解他有問題。他或她甚至不知道字符串和浮點類型之間的區別。 – 2014-08-31 04:26:12

+0

@KickButtowski你是對的。但我覺得上面的代碼是不言自明的,難以用言語表達。 – 2014-08-31 04:28:15

+0

我很抱歉告訴你。不是這樣。他或她甚至不知道float和String類型之間的區別。你如何期望他或她知道你的代碼? – 2014-08-31 04:29:03

2

按照Java文檔

while語句而 特定條件爲真連續執行語句塊。其語法可以被表示爲:

而(表達式){

statement(s) 

}

while語句計算表達式,其必須返回一個布爾 值。如果表達式的計算結果爲true,則while語句 將執行while代碼塊中的語句。 while語句 繼續測試表達式並執行其塊,直到 表達式計算結果爲false。

考慮所有計算邏輯,並將用戶輸入作爲statement(s)在上面的示例中。 while循環將執行,只要expression返回true布爾值。因此,您可以添加again!=0作爲expression,因爲當用戶按下0程序應該被終止。

變量也應該初始化爲非零值,以便第一次滿足條件。否則,您必須使用do-while循環。

scan.nextFloat()將用戶輸入視爲浮點數。僅當用戶輸入爲浮動時才使用它。如果你聲明again爲int,那麼你必須使用scan.nextInt();

public class gradebook { 


/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    float discussionweight, hwweight, examweight, discussion, hw, exam, finalaverage; 
    int again=-1; 

    while(again!=0){ 

    Scanner scan = new Scanner(System.in); 
    System.out.print("Enter the students name: "); 
    String student = scan.next(); 

    System.out.print ("Enter the weight of the discussions as an integer: "); 
    discussionweight = scan.nextFloat(); 

    // Prompts for the discussions grade 
    System.out.print ("Enter the score of the discussion as an integer: "); 
    discussion = scan.nextFloat(); 

    // Prompts for the weight of the homework grade in an integer 
    System.out.print ("Enter the weight of the homework as an integer: "); 
    hwweight = scan.nextFloat(); 

    // Prompts for hw grade 
    System.out.print ("Enter the hw grade: "); 
    hw = scan.nextFloat(); 

    System.out.print("Enter the weight of the exam as an integer"); 
    examweight = scan.nextFloat(); 

    System.out.print("Enter the exam grade"); 
    exam = scan.nextFloat(); 




    // Calculate and print the final, weighted average. 
    finalaverage = (((discussionweight * discussion) + (hw * hwweight) + (exam * examweight))/100); 

    if (finalaverage >= 90) 
    System.out.println(student +"'s " + "final grade is an A."); 
    else if (finalaverage >= 80) 
    System.out.println(student +"'s " + "final grade is a B."); 
    else if (finalaverage >= 70) 
    System.out.println(student +"'s " + "final grade is a C."); 
    else if (finalaverage >= 60) 
    System.out.println(student +"'s " + "final grade is a D."); 
    else if (finalaverage >= 10) 
    System.out.println(student +"'s " + "final grade is an F."); 


    System.out.println ("The final average is "+ finalaverage); 
    System.out.println ("Would you like to continue? Enter 0 to exit and anything else to continue."); 
    again = scan.nextInt(); 


    } 
} 
+0

而不是給出答案,幫助OP瞭解他的問題。他或她甚至不知道字符串和浮點類型之間的區別。 – 2014-08-31 04:27:24

+0

希望它會有所幫助 – 2014-08-31 04:55:58

1

讓我們分解一下需要發生的事情。

N是學生5的數量,

  • 收集最終成績的學生
    • 收集作業的分數
    • 平均得分一起
  • 重複直到你有N學生的成績
  • average是學生的成績平均

打破了這個問題分解成更小的部分。考慮「讓average成爲學生成績的平均值」。我們所知道的是:

  • A級是float,所以我們需要一個方法(或「小部分」,正如我前面把它稱爲),將平均float值。

在Java中,你會表達這樣的方法是這樣的:

static float average(List<Float> terms) { 
    float total = 0;  // Begin with a total 0 - any summation begins here 
    for (float t : terms) { // For every float t in terms 
     total += t;   // add t to the total 
    } 
    return total/terms.size(); // return total divided by the number of terms 
} 

,然後進行測試,並確保這樣做你想要的位置:

public static void main(String[] args) { 
    // `test` is now a list of floats, we would expect average to be 
    // effectively equal to 2.5 
    List<Float> test = new ArrayList<>(Arrays.asList(2.0f, 2.5f, 3.0f)); 
    float actual = average(test); // This calls the average method above 
    System.out.println(actual); // and outputs to console, which prints 2.5 
} 

如何是一種平均的方法,它是測試,以確保它做你期望對你有用嗎?那麼,舉個例子,你現在有辦法收集一個float值的列表(使用List<Float>)。所以我們現在可以更詳細地表達上面的細節。

  • 聲明List<Float>,如List<Float> grades = new ArrayList<Float>
  • 收集了學生的最終成績,由
    • 收集的分配的分數,然後
    • 平均得分在一起,(提示:也許你需要另一種不同average()方法來處理加權平均)
    • 和平均,存儲在grades例如grades.add(average)
  • 重複,直到你有N學生的成績
  • 使用平均得到最終的輸出,像float classAverage = average(grades)

一切有關規劃即將打破問題成其組成成分,然後進一步將它們分解,直到你reasonbly大小(閱讀:)問題這可以簡單地解決,然後放到一個更大的程序中。

List<E> Documentation

ArrayList<E> Documentation