gpt4 book ai didi

java - 创建一个重复循环,以便程序会询问用户是否需要另一个条目,直到用户指示 “No.”

转载 作者:行者123 更新时间:2023-12-02 09:23:23 26 4
gpt4 key购买 nike

这是作业的要求:我需要创建一个 Java 程序来接受以下两个变量的输入:(a) 学生和 (b) 分数。我还需要创建一个重复循环,以便程序会询问用户是否需要另一个条目,直到用户表示“否”。我需要包含一个变量来计算整个类(class)的平均分数。我需要创建一个变量,该变量将使用图表根据学生的分数分配字母等级(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.

最佳答案

您没有正确使用函数。

两次编写 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"))
}

请记住,前面的代码尚未完成,只是为了给您一些指导,以帮助您了解应该做什么。

关于java - 创建一个重复循环,以便程序会询问用户是否需要另一个条目,直到用户指示 “No.”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58519576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com