【问题标题】:I dont know what to do with errors anymore [closed]我不知道如何处理错误了[关闭]
【发布时间】:2021-09-30 04:53:45
【问题描述】:

嗨,我还是编码新手,我每天都在编写这个代码。它有很多错误,以至于我不再理解了。你能帮忙解决代码有什么问题并修改它以使其工作或运行吗?

这是我的代码

import java.util.Scanner;
import java.io.*;
public class GradeDistribution

 {
 public static void main(String[] args)
{
  print_header();

    Scanner kb = new Scanner (System.in);

{
    System.out.println("******************************************************");
    System.out.println("This program will present the number of grades" ); //INTRODUCTION
    System.out.println("you input for grades A-F and turn this information"); //TO
    System.out.println("in to a bar graph for ease."); //PROGRAM
    System.out.println("******************************************************");
    System.out.println(" ");


         for(double loopcounter=0; loopcounter<5; loopcounter++)

         {

         switch(loopcounter)

         {


        case 0:  "\nGrade A: |"; break;

        case 1:  "\Grade B: |"; break;

        case 3:  "\Grade C: |"; break;

        case 4:  "\Grade D: |"; break;

        case 5:  "\Grade F: |"; break;

         }


         //loop to output each asterisk

         for(double asteriskcounter=0; asteriskcounter<array[loopcounter]/2; 
asteriskcounter++)

         System.out.println ('*');

         System.out.println("| " << array[loopcounter] << '%');
  }

    percent=100/14; // 7;
    System.out.println(" ");
    System.out.println(" TOTAL GRADES ENTERED - ");

    char[] line = percent*a/2;//3; Bcent=percent*b/2; Ccent=percent*c/2; Dcent=percent*d/2; 
Ecent=percent*e/2; Fcent=percent*f/2;

    } //end;

    System.out.println(" 0 10 20 30 40 50 60 70 80 90 100");
    System.out.println(" | | | | | | | | | | |");
    System.out.println(" **************************************************");

    double array[6] = {Acent, Bcent, Ccent, Dcent, Ecent, Fcent}; //stores grades as 
percentage
    char[] line = array2[6] = {'A','B','C','D','E','F'}; //stores letters a-f
    // loop for each grade
    for(double loopcounter=0; loopcounter<6; loopcounter++)
{

    System.out.println(" ");
    // loop to output each asterisk
    for(double asteriskcounter=0; asteriskcounter<array[loopcounter]; asteriskcounter++)
{

       System.out.println("*");
}
    // prdouble the grade after the asterisks
    System.out.prdoubleln( " GRADE "[loopcounter]);

请再次帮助我。因为我的任务真的需要它,而且我真的很难赶上。 非常感谢您提前提供的帮助。

【问题讨论】:

  • 它显示的错误是什么?
  • double loopcounter - 为什么要加倍?
  • 您似乎在整个文件上使用了搜索和替换来将 int 替换为 double - 您有双倍的循环计数器(这没有多大意义)并且您有System.out.prdoubleln() 而不是 System.out.println
  • 语句的顺序似乎搞砸了:变量在声明之前就被使用了。 System.out.println("| " &lt;&lt; array[loopcounter] &lt;&lt; '%'); 看起来像是 C++(您可以使用 &lt;&lt; 附加到输出流)和 Java(您可以使用 System.out.println() 打印文本)的混合体。此行应为System.out.println("| " + array[loopcounter] + '%');

标签: java error-handling


【解决方案1】:

您应该检查括号,因为其中一些缺少。

您的代码中没有 print_header() 方法,请为它定义一个方法。

如果你想在 switch-case 中获得输出,你应该使用 System.out.print() 或 System.out.println() 方法,因为你刚刚在那里写了一些字符串。

在 for 循环中,您编写了“array[loopcounter]”,在该循环之前您没有名称为“array”的数组。

另外,“百分比”变量应该有一个数据类型。

在下面,名称为“array”的数组有一些字符串(它们应该有双引号),但您将此数组定义为 char。你应该使用字符串。

最后一个问题是“System.out.prdoubleln()”方法。 System.out 没有这个方法。

  • 在循环中使用 int 变量。

我已经快速检查并看到了这些问题。

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 2021-11-26
    • 2014-02-16
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多