循环输入和语句列表

时间:2014-08-31 04:05:29

标签: java

循环这些语句的任何帮助。我是新手,我遇到了一些麻烦。我正在努力使成绩单能够在给出一个班级平均成绩之前取五个孩子的成绩。只需要循环帮助。

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;
        }



    }

4 个答案:

答案 0 :(得分:2)

根据Java文档

  

while语句在a时连续执行一个语句块   特殊情况是真的。其语法可表示为:

     

while(表达式){

 statement(s)
     

}

     

while语句计算表达式,该表达式必须返回一个布尔值   值。如果表达式的计算结果为true,则为while语句   执行while块中的语句。 while语句   继续测试表达式并执行其块直到   expression的计算结果为false。

在上面的示例中,考虑所有计算逻辑并将用户输入作为statement(s)。只要expression返回true布尔值,循环就会执行。因此,您可以将again!=0添加为expression,因为当用户按0程序时应该终止。

同样,应该使用非零值初始化变量,以便第一次满足条件。否则你必须使用do-while循环。

scan.nextFloat()将用户输入视为float。仅在用户输入为浮动时使用它。如果将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();


    }
}

答案 1 :(得分: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类型的输入,那么为什么在这种情况下你需要String。

答案 2 :(得分:1)

您无法使用您之前声明的相同名称重新声明Java中的变量。在String again = scan.nextFloat();行的程序中,您正尝试这样做。而且你也试图将一个浮点值赋给不允许的字符串变量。关于循环问题,您可以使用简单的do 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);

    }
}

答案 3 :(得分:1)

让我们分解为需要发生的事情。

N为学生人数5

  • 收集学生的最终成绩
    • 收集作业的分数
    • 平均分数
  • 重复,直至获得N学生成绩
  • average成为学生成绩的平均值

将此问题分解为更小的部分。考虑“让average成为学生成绩的平均值”。我们所知道的是:

  • 成绩是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)

关于编程的一切都是将问题分解为构成组件,然后进一步将它们分解,直到你有合理的大小(读取:)问题,这些问题可以简单地解决,然后组合成一个更大的计划。

List<E> Documentation

ArrayList<E> Documentation