Java代码将不会退出,当使用-1时,数字不会转换为字母成绩。

huangapple go评论78阅读模式
英文:

Java code will not quit when using -1 and numbers do not transfer to alphabetic grades

问题

使用 -1 退出时无效还不能将成绩从数字或字母转换这只是一个用于大学课程的简单成绩记录本

public class Assignment3
{
    public static void main(String[] args)
    {
        // 为最小和最大范围定义常量
        final int minValue = -1;
        final int maxValue = 100;

        promptForInt(String message, int minValue, int maxValue);
    }

    public static void promptForInt(String message, int minValue, int maxValue)
    {
        System.out.println(message);
        convertToLetter(double avg);

        // 声明循环中初始值的变量
        float sum = 0;
        int numStudents = 0;
        float score = 0;
        boolean doneYet = false;

        // 哨兵控制的 do-while 循环,不知道会运行多少次
        do
        {
            System.out.printf("为第 %d 位学生输入分数" 
                    + "(或输入 -1 退出): ", numStudents);
            Scanner keyboard = new Scanner(System.in);
            score = Float.parseFloat(keyboard.nextLine());

            if (score != -1)
            {
                sum += score;
                numStudents += 1;
            }
        } while (score != -1);
        double avgScore = (double) sum / numStudents;
        System.out.println("平均分为: " + avgScore
                + ",相当于一个 " + avg);

        do
        {
            // 输入验证
            if (score > minValue || score < maxValue)
            {
                System.err.printf("无效值。可接受范围为"
                        + " %d 到 %d 之间\n"
                        + "请重试\n", minValue, maxValue);  
            }
            else
            {
                doneYet = true;    
            }
        } while (doneYet == false);
    }

    public static void convertToLetter(double avg) 
    {
        // 确定成绩等级的范围
        if (avg >= 90)
        {
            System.out.println("A");
        }
        else if (avg >= 80)
        {
           System.out.println("B"); 
        }
          else if (avg >= 70)
        {
           System.out.println("C"); 
        }
          else if (avg >= 60)
        {
           System.out.println("D"); 
        }
          else
        {
           System.out.println("F"); 
        }
    }    
}
英文:

When using -1 to quit it does not work and also it does not turn the grades from numerical or alphabetic. This is just a simple grade book for a college class.

public class Assignment3
{
public static void main(String[] args)
{`enter code here`
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt(String message, int minValue,int maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{ 
System.out.println(message);
convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don&#39;t know how many times will run
do
{
System.out.printf(&quot;Enter the score for student #%d&quot; 
+ &quot;(or -1 to quit): &quot;, numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println(&quot;The average score is: &quot; + avgScore
+ &quot; which equates to a &quot; + avg);
do
{
// Input Validation
if (score &gt; minValue || score &lt; maxValue)
{
System.err.printf(&quot;Invalid value. The acceptable range is&quot;
+ &quot;between %d and %d\n&quot;
+ &quot;Please try again\n&quot;,minValue, maxValue);  
}
else
{
doneYet = true;    
}
}while (doneYet == false);
}
public static void convertToLetter(double avg) 
{
// Identifying the ranges for the grade letter
if (avg &gt;= 90)
{
System.out.println(&quot;A&quot;);
}
else if (avg &gt;= 80)
{
System.out.println(&quot;B&quot;); 
}
else if (avg &gt;= 70)
{
System.out.println(&quot;C&quot;); 
}
else if (avg &gt;= 60)
{
System.out.println(&quot;D&quot;); 
}
else
{
System.out.println(&quot;F&quot;); 
}
}    
}  

答案1

得分: 2

以下是翻译好的代码部分:

import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;

public class Assignment3
{
    public static void main(String[] args)
    {
        // 定义最小和最大范围的常量
        final int minValue = -1;
        final int maxValue = 100;
        promptForInt("", minValue, maxValue);
    }
    
    public static void promptForInt(String message,int minValue,int maxValue)
    { 
        System.out.println(message);
        //convertToLetter(double avg);
    
        // 声明循环中的初始值变量
        float sum = 0;
        int numStudents = 0;
        float score = 0;
        boolean doneYet = false;
        
        // 哨兵控制的 do-while 循环,不知道会运行多少次
        do
        {
            System.out.printf("请输入第 %d 位学生的分数" 
                    + "(或者输入 -1 退出): ", numStudents);
            Scanner keyboard = new Scanner(System.in);
            score = Float.parseFloat(keyboard.nextLine());
            
            if (score != -1)
            {
                sum += score;
                numStudents += 1;
            }
        }while (score != -1);
        double avgScore = (double) sum / numStudents;
        convertToLetter(avgScore);
        System.out.println("平均分数是: " + avgScore
                + ",对应等级是: " + avgScore);
        
        do
        {
            // 输入验证
            if (score > minValue || score < maxValue)
            {
                System.err.printf("无效的值。可接受范围在 %d 到 %d 之间\n"
                        + "请重试\n",minValue, maxValue);  
            }
            else
            {
                doneYet = true;    
            }
        }while (doneYet == false);
    }
    
    public static void convertToLetter(double avg) 
    {
        // 识别成绩等级的范围
        if (avg >= 90)
        {
            System.out.println("A");
        }
        else if (avg >= 80)
        {
            System.out.println("B"); 
        }
        else if (avg >= 70)
        {
            System.out.println("C"); 
        }
        else if (avg >= 60)
        {
            System.out.println("D"); 
        }
        else
        {
            System.out.println("F"); 
        }
    }    
}

请注意,我已经根据你的要求,只提供了代码的翻译部分,没有回答问题或添加其他内容。

英文:

You need to fix you function calls and call the convertToLetter after the user inputs scores.

Try this code:

 import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
public class Assignment3
{
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt(&quot;&quot;, minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{ 
System.out.println(message);
//convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don&#39;t know how many times will run
do
{
System.out.printf(&quot;Enter the score for student #%d&quot; 
+ &quot;(or -1 to quit): &quot;, numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
convertToLetter(avgScore);
System.out.println(&quot;The average score is: &quot; + avgScore
+ &quot; which equates to a &quot; + avgScore);
do
{
// Input Validation
if (score &gt; minValue || score &lt; maxValue)
{
System.err.printf(&quot;Invalid value. The acceptable range is&quot;
+ &quot;between %d and %d\n&quot;
+ &quot;Please try again\n&quot;,minValue, maxValue);  
}
else
{
doneYet = true;    
}
}while (doneYet == false);
}
public static void convertToLetter(double avg) 
{
// Identifying the ranges for the grade letter
if (avg &gt;= 90)
{
System.out.println(&quot;A&quot;);
}
else if (avg &gt;= 80)
{
System.out.println(&quot;B&quot;); 
}
else if (avg &gt;= 70)
{
System.out.println(&quot;C&quot;); 
}
else if (avg &gt;= 60)
{
System.out.println(&quot;D&quot;); 
}
else
{
System.out.println(&quot;F&quot;); 
}
}    
}  

答案2

得分: 0

这是一些代码逻辑错误。我已经纠正了代码。

这将会将分数转换为等级,并且不会出现无限循环。如果有任何疑问,请添加注释。

import java.util.Scanner;

public class Assignment3
{
    public static void main(String[] args)
    {
        // 为最小和最大范围定义常量
        final int minValue = -1;
        final int maxValue = 100;
        promptForInt("", minValue, maxValue);
        
    }
    
    public static void promptForInt(String message, int minValue, int maxValue)
    { 
        System.out.println(message);

        // 在循环中的初始值声明变量
        float sum = 0;
        int numStudents = 0;
        float score = 0;
        boolean doneYet = false;
        
      
        // 哨兵控制的 do-while 循环,不知道会运行多少次
        do
        {
            System.out.printf("Enter the score for student #%d" 
                    + "(or -1 to quit): ", numStudents);
            Scanner keyboard = new Scanner(System.in);
            score = Float.parseFloat(keyboard.nextLine());
            
            if (score != -1)
            {
                sum += score;
                numStudents += 1;
            }
        }while (score != -1);
        double avgScore = (double) sum / numStudents;
        
        System.out.println("The average score is: " + avgScore
                + " which equates to a " + convertToLetter(avgScore));
        
        do
        {
            // 输入验证
            if (score < minValue || score > maxValue)
            {
                System.err.printf("Invalid value. The acceptable range is"
                        + " between %d and %d\n"
                        + "Please try again\n", minValue, maxValue);  
            }
            else
            {
                doneYet = true;    
            }
        }while (doneYet == false);
    }
    
    public static String convertToLetter(double avg) 
    {
        // 确定等级字母的范围
        if (avg >= 90)
        {
            return "A";
        }
        else if (avg >= 80)
        {
           return "B";
        }
          else if (avg >= 70)
        {
           return "C"; 
        }
          else if (avg >= 60)
        {
           return "D"; 
        }
          else
        {
           return "F";
        }
    }    
}
英文:

There was some logic error on your part. I have corrected the code.

This will convert the marks into grades and not run into any infinite loops too. Comment if you have any doubts.

import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt(&quot;&quot;, minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{ 
System.out.println(message);
//convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don&#39;t know how many times will run
do
{
System.out.printf(&quot;Enter the score for student #%d&quot; 
+ &quot;(or -1 to quit): &quot;, numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println(&quot;The average score is: &quot; + avgScore
+ &quot; which equates to a &quot; + convertToLetter(avgScore));
do
{
// Input Validation
if (score &lt; minValue || score &gt; maxValue)
{
System.err.printf(&quot;Invalid value. The acceptable range is&quot;
+ &quot;between %d and %d\n&quot;
+ &quot;Please try again\n&quot;,minValue, maxValue);  
}
else
{
doneYet = true;    
}
}while (doneYet == false);
}
public static String convertToLetter(double avg) 
{
// Identifying the ranges for the grade letter
if (avg &gt;= 90)
{
return &quot;A&quot;;
}
else if (avg &gt;= 80)
{
return &quot;B&quot;;
}
else if (avg &gt;= 70)
{
return &quot;C&quot;; 
}
else if (avg &gt;= 60)
{
return &quot;D&quot;; 
}
else
{
return &quot;F&quot;;
}
}    
} 

huangapple
  • 本文由 发表于 2020年10月3日 12:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64180719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定