【问题标题】:Create a repetitive loop so the program will ask the user if another entry is desired until the user indicates “No.”创建一个重复循环,这样程序会询问用户是否需要另一个条目,直到用户指示“否”。
【发布时间】:2019-10-23 17:38:38
【问题描述】:

这是作业要求的: 我需要创建一个 Java 程序来接受以下两个变量的输入:(a)学生和(b)分数。 我还需要创建一个重复循环,以便程序会询问用户是否需要另一个条目,直到用户指示“否”。 我需要包含一个变量来计算整个班级的平均分数。 我需要创建一个变量,该变量将使用图表(A=.93,A-=.90 B=.87 等)根据学生的分数分配一个字母等级 我必须创建一个变量,该变量将列出获得以下每个等级范围的学生人数:A、B、C、D 和 F。(请注意,“+”和“-”等级属于一般字母等级范围.)

我能够拼凑出部分代码,但无法将它们组合在一起。这是我的第一堂课,没有关于如何进行的真正指导或指导。以下是我正在尝试集成的代码片段。

package gradeproject;
import java.util.Scanner;
import java.util.InputMismatchException;
public class ComputeGradeTryCatch {

public static void main(String[] args) {

    // This is the first piece of the code.

            int score1;
    Scanner s = new Scanner (System.in);
    System.out.println("Enter Student Name: ");
    String name = s.next();

    boolean done = false;
    while (!done) {

    System.out.println("Enter one test score: ");
    try
    {
    score1 = s.nextInt();
    done = true;

    }
    catch (InputMismatchException e){
    System.out.println("Enter a Numeric Value");
    }
    s.nextLine();


            //This is a second piece of the code

Scanner s = new Scanner (System.in);
    System.out.println("Enter Student Name: ");
    String name = s.next();
    System.out.println("Enter three test scores: ");
    int score1 = s.nextInt();
    int score2 = s.nextInt();
    int score3 = s.nextInt();
char letterGrade;
    double average = (score1 + score2 +score3)/3;
    System.out.println("Name: " + name);
    System.out.println("Scores are: " + score1 + "" + score2 + "" +            score3);
    System.out.println("Average: " + average);
if ((average >=90  && average <=100 ))
letterGrade = 'A';
else if ((average >=80  && average <=90 ))
letterGrade = 'B';
else if ((average >=70  && average <=80 ))
letterGrade = 'C';
else if ((average >=60  && average <=70 ))
letterGrade = 'D';
else letterGrade = 'F';

    System.out.println(" Letter Grade is : " + letterGrade);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\This is the third piece of the code

Scanner scan = new Scanner(System.in);
static String userInput = null;
{
    checkPrime();
    while (true)
    {
        System.out.print("Do You Want to Continue (Y/N) ?");


        if (userInput.equalsIgnoreCase("Y"))
            checkPrime();
        else if (userInput.equalsIgnoreCase("N"))
        {
        System.out.print("Thank you !!");
            break;
        }
        else{
        System.out.print("Try Again With (Y/N) only !");   
        }

\\This has been a rather challenging task. Thank you for your help             and support!

There are errors in all of the three parts of the code when I run    it. My goal is to put together a clean piece of code that will execute the desire outcome in a concise manner.

【问题讨论】:

    标签: java while-loop


    【解决方案1】:

    您没有正确使用函数。

    两次编写 checkPrime() 时,你只是调用了一个名为 checkPrime() 的函数,我认为它不存在(或者至少你没有提供该函数的代码)。

    你应该这样做:

    public static void main(String[] args) {
        Scanner s = new Scanner (System.in);
        do {
            print("What do you want to do? a) Add a new student with one score b) Add a student with three scores");
            String input = s.next()
            if (input.equals("a")) {
                // Do your A stuff
            } else if (input.equals("b")) {
                // Do your B stuff
            }
            print("Do you want to continue? (Y/N)");
            input = s.next()
        } while (!input.equalsIgnoreCase("N"))
    }
    

    请记住,前面的代码还没有完成,只是为了给你一些指导来理解你应该做什么。

    【讨论】:

    • 谢谢罗伯托,我会尽量把它放在一起。
    • 祝你好运!随意提出更多可能出现的疑问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2013-04-18
    相关资源
    最近更新 更多