Java 根据用户指示执行多次的循环

Java 根据用户指示执行多次的循环,java,Java,编写一个程序,根据CSE 1341教学大纲计算当前成绩。程序应该提示用户输入他们的名字和姓氏。然后,它将这些名称作为字符串传递给CSE1341Grade类的第二个方法。第二个方法的名称将是calcGrade。此方法将提示用户输入考试分数计数、测验分数计数和实验室分数计数 然后,它将利用重复结构根据之前输入的计数提示考试成绩、测验成绩和实验室成绩。例如,如果用户输入的考试分数计数为2;然后程序将循环2次以输入两个考试分数;同样,测验和实验成绩的统计也是如此 假设你有100%的出勤记录,你将获得5%

编写一个程序,根据CSE 1341教学大纲计算当前成绩。程序应该提示用户输入他们的名字和姓氏。然后,它将这些名称作为字符串传递给CSE1341Grade类的第二个方法。第二个方法的名称将是calcGrade。此方法将提示用户输入考试分数计数、测验分数计数和实验室分数计数

然后,它将利用重复结构根据之前输入的计数提示考试成绩、测验成绩和实验室成绩。例如,如果用户输入的考试分数计数为2;然后程序将循环2次以输入两个考试分数;同样,测验和实验成绩的统计也是如此

假设你有100%的出勤记录,你将获得5%的出勤分数。 使用教学大纲确定每个类别的权重,如考试、测验和实验室。由于您的出勤率很高,因此总分数增加5%。 假设:所有考试、实验和测验分数都在100分以内。 样本运行: 

java CSE1341Grade

名字:詹姆斯

姓:邦德

你有多少考试成绩?一,

你有多少个测验分数?二,

你有多少实验室成绩?二,

参加考试1分:90分

进入测验1,分数:80

进入测验2,分数:80

进入实验一:90分

进入实验2:90分

总分:84.55

詹姆斯·邦德你的成绩是a:B

^^这是我的家庭作业,这是我到目前为止所做的

import java.util.Scanner;

public class CSE1341Grade
{
    public static void main(String [] args)
    {
        //set up Scanner for user input, prompt for first name, define variable, and print      response
        Scanner s = new Scanner(System.in);
        System.out.print("First name: ");
        String first = s.nextLine();
        System.out.printf(" %s\n", first);

        //prompt user for last name, define variable, and print response
        System.out.print("Last name: ");
        String last = s.nextLine();
        System.out.printf(" %s\n", last);
    }

    public static void calcGrade(String first, String last)
    { 
        //prompt user for number of exam grades, define exam variable, print response
        System.out.print("How many exam grades do you have? ");
        String exam = s.nextLine();
        System.out.printf(" %s\n", exam);

        //prompt user for number of quiz grades, define quiz variable, print response
        System.out.print("How many quiz grades do you have? ");
        String quiz = s.nextLine();
        System.out.printf(" %s\n", quiz);

        //prompt user for number of lab grades, define lab variable, print response
        System.out.print("How many lab grades do you have? ");
        String lab = s.nextLine();
        System.out.printf(" %s\n", lab);

        while (exam != -1)
        {
            System.out.print("Enter " exam 1 " score: ", ++exam)



        //define variables for computations
        int score = 0;
        int grade = 0;

        //if statement to determine the final letter grade
        if(score >= 90 && score <=100){
            grade = 'A'; }
        else if(score >=80 && score < 90){
            grade = 'B'; }
        else if(score >= 70 && score < 80){
            grade = 'C'; }
        else if(score >=60 && score < 70){
            grade = 'D'; }
        else {
            grade = 'F'; }
    }
}
import java.util.Scanner;
公共类CSE1341Grade
{
公共静态void main(字符串[]args)
{
//为用户输入设置扫描仪、提示输入名字、定义变量和打印响应
扫描仪s=新的扫描仪(System.in);
系统输出打印(“名字:”);
字符串first=s.nextLine();
System.out.printf(“%s\n”,第一个);
//提示用户输入姓氏、定义变量和打印响应
系统输出打印(“姓氏:”);
字符串last=s.nextLine();
System.out.printf(“%s\n”,最后一个);
}
公共静态void calcGrade(先字符串,后字符串)
{ 
//提示用户输入考试分数,定义考试变量,打印响应
系统输出打印(“你有多少考试分数?”);
字符串检查=s.nextLine();
System.out.printf(“%s\n”,检查);
//提示用户输入测验分数,定义测验变量,打印回答
System.out.print(“你有多少测验分数?”);
字符串测验=s.nextLine();
System.out.printf(“%s\n”,测验);
//提示用户输入实验室等级数,定义实验室变量,打印响应
System.out.print(“你有多少实验室成绩?”);
字符串lab=s.nextLine();
System.out.printf(“%s\n”,lab);
while(考试!=-1)
{
系统输出打印(“输入“考试1”分数:,++考试)
//定义用于计算的变量
智力得分=0;
int等级=0;
//确定最终字母等级的if语句
如果(分数>=90&&score=80&&score<90){
等级='B';}
否则如果(分数>=70和分数<80){
等级='C';}
否则如果(分数>=60和分数<70){
等级='D';}
否则{
等级='F';}
}
}

我的问题是如何创建一个循环,以提示用户需要多少考试分数。

我喜欢做一个循环,并继续要求用户输入,直到得到我想要的结果:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    boolean shouldContinue = true;

    while(shouldContinue) {

        // do program logic

        System.out.print("Should we continue? [Y or N] >> ");

        if (input.nextLine().equalsIgnoreCase("N")) {

            shouldContinue = false;

        }

    }

}
这是一种相对“结构化”的方式,用于执行交互循环,并根据用户的需要反复运行程序

--更新--

要进行您提前知道的多次迭代,只需将其导入循环计数器:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Number of grades to enter [number] : ");

    int numberOfTimesToIterate = input.nextInt();

    for (int i = 0; i < numberOfTimesToIterate; i++) {

        // do stuff here

    }

}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“要输入的分数[数字]:”);
int numberOfTimesToIterate=input.nextInt();
for(int i=0;i
伪代码:

function(int n) {
    for (i = n, i > 0, i--) {
        \\prompt user
    }
}

这应该会让你大致了解如何做,即每次获取数据并做它需要做的事情时,从中减去1,直到达到0。不要忘记确保
n
永远不小于零(无效),并且
n
是一个整数。否则,最好让函数简单地提示用户输入一个数量,然后将该数量传递给实际使用它的函数。或者,创建一个函数,该函数迭代
n
次,并多次调用作为参数传递给它的函数(更难正确地执行)。

类似的操作应该适合您

System.out.println("Please enter the number of exam grades you want to enter");
int grade_count = s.nextInt();
int [] grades = new int[grade_count];

for(int i =0; i<grade_count; i++){
System.out.println("Enter grade for "+i+1 +"Exam");
grades[i] = s.nextInt();
}
System.out.println(“请输入您想要输入的考试分数”);
int grade_count=s.nextInt();
int[]等级=新的int[等级计数];

对于(int i=0;i我在注释中对代码进行了一些更改

在这里你可以看到差异

注意:它仍然没有完成您的家庭作业:-)

更新:因为foor循环中的索引并不重要,所以可以这样使用它:

 for (int i = 1; i <= examsCount; i++) {
      // using i+1 to print starting from 1 instead of 0
      System.out.print("Enter " + i + " score: ");

for(int i=1;i+1)用于以正确的方式询问家庭作业问题。double+1用于询问hw q's的正确方式将字符串参数转换为int,然后使用以int值为限制的“for”循环。一个正确的家庭作业问题——我今天没有投票权…
while(考试!=-1)
在将字符串与值进行比较时,不应使用
=
!=
方法,而应使用
equals
方法对字符串进行比较
for (int i = 0; i < examsCount; i++) {
      // using i+1 to print starting from 1 instead of 0
      System.out.print("Enter " + (i + 1) + " score: ");