当前位置: 首页 > 知识库问答 >
问题:

不确定在Java代码中使用什么标识符[已关闭]

常宸
2023-03-14

编辑:我确实更改了代码,但在这样做的时候,我创建了更多的问题,我不知道如何修复一些问题。

public class StudentAverage

{//计算学生总数的方法public static int addToCounter(int numberOfStudents){

  ++numberOfStudents;
  
  return numberOfStudents;

}

//计算test one得分公共静态float addToTestOneScoresAccumulator(float testOneScore){float TotalTestonesCores;

  totalTestOneScores += testOneScore;
  
  return totalTestOneScores;

}

  totalTestTwoScores += testTwoScore;
  
  return totalTestTwoScores;
  totalOverallTestScores = totalTestOneScores + totalTestTwoScores;
  
  return totalOverallTestScores;
  if (testOneScore < lowestTestOneScore)
     lowestTestOneScore = testOneScore;
  
  return lowestTestOneScore;

//计算测试一的最高分的方法public static float findHighestTestOneScore(float testOneScore){float highesttestonescore;

  if (testOneScore > highestTestOneScore)
     highestTestOneScore = testOneScore;
                    
  return highestTestOneScore;

}

//计算测试两个分数的最低分的方法公共静态float findLowestTestTwoScore(float testTwoScore){float loWestTestTwosCore;

  if (testTwoScore < lowestTestTwoScore)
     lowestTestTwoScore = testTwoScore;
                    
  return lowestTestTwoScore;
  if (testTwoScore > highestTestTwoScore)
     highestTestTwoScore = testTwoScore;
                    
  return highestTestTwoScore;
  if (numberOfStudents > 0)
  {
     averageScore = totalOverallTestScores/numberOfStudents;
  }
  else
  {
     averageScore = 00.00f;
  }
  
  return averageScore;

}

}//下课

import java.util.Scanner;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class StudentAverageCalculationsTest
{
   public static void main(String[] args)
   {
      //Define the programmer-defined variables
      int optionSelected;
      
      int processRecords;
      
      int numberOfStudents = 0;
      
      int      studentNumber;
      String   studentName;
      
      float testOneScore;
      float testTwoScore;
      
      float totalTestOneScores;
      float totalTestTwoScores;
      float totalOverallTestScores;
      
      float lowestTestOneScore   = 00.00f;
      float highestTestOneScore    = 00.00f;
      float lowestTestTwoScore   = 00.00f;
      float highestTestTwoScore    = 00.00f;
      
      float averageScore;
            
      // Scanner object for keyboard input
      Scanner keyboardInput = new Scanner(System.in);
      
      System.out.print("Would you like to run the program\n" +
                          " Type 1 for Yes or -1 for No ");
        processRecords = keyboardInput.nextInt();
        keyboardInput.nextLine();
        
        while (processRecords != -1)
        {
           studentNumber     = getStudentNumber();
           
           studentName       = getStudentName();
           
           testOneScore      = getTestOneScore();
           
           testTwoScore      = getTestTwoScore();
                                   
           numberOfStudents = StudentAverage.addToCounter(numberOfStudents);
           
           totalTestOneScores   = StudentAverage.addToTestOneScoresAccumulator(testOneScore,
                                                                               totalTestOneScores);
                                                        
           totalTestTwoScores   = StudentAverage.addToTestTwoScoresAccumulator(testTwoScore,
                                                                               totalTestTwoScores);
                                                     
           totalOverallTestScores   = StudentAverage.addToOverallTestScoresAccumulator(totalTestOneScores,
                                                                                      totalTestTwoScores,
                                                                                      totalOverallTestScores);
        }
        
     }
     
   public static int getStudentNumber()
     {
        int studentNumber;
        
        Scanner keyboardInput = new Scanner(System.in);
        
        System.out.print("Enter the Student Number ");
        studentNumber = keyboardInput.nextInt();
        
        while ( studentNumber < 0 || studentNumber > 100000 )
        {
           JOptionPane.showMessageDialog(null,
           "The student number entered is not in an acceptable range.",
           "Invalid Entry", JOptionPane.WARNING_MESSAGE);
           
           studentNumber = JOptionPane.showInputDialog(null,
           "Please re-enter a value from 0 to 100000 only ", "Error",
           JOptionPane.ERROR_MESSAGE);
           studentNumber   = Integer.parseInt();
        }
        
        return studentNumber;
     }
     
     public static String getStudentName()
     {
        String studentName;
       
       Scanner keyboardInput = new Scanner(System.in);
       
       System.out.print("Enter the Student Name ");
       studentName    = keyboardInput.nextLine();
       
       while ( studentName.isEmpty() )
       {
          JOptionPane.showMessageDialog(null,
          "The student name cannot be empty.", "Invalid Entry",
          JOptionPane.WARNING_MESSAGE);
          
          studentName  = JOptionPane.showInputDialog(null,
          "Please re-enter a name for the student ", "Error",
          JOptionPane.ERROR_MESSAGE);
       }
       
       return studentName;
    }
 
    public static Float getTestOneScore()
    {
       float testOneScore;
       
       Scanner keyboardInput = new Scanner(System.in);
       
       System.out.print("Enter Test One's Score ");
       testOneScore    = keyboardInput.nextFloat();
       
      while ( testOneScore < 0 || testOneScore > 100 )
       {
          JOptionPane.showMessageDialog(null,
          "The test score entered is not in an acceptable range.",
          "Invalid Entry", JOptionPane.WARNING_MESSAGE);
          
          testOneScore = JOptionPane.showInputDialog(null,
          "Please re-enter a value from 0 to 100 only ", "Error",
          JOptionPane.ERROR_MESSAGE);
          testOneScore   = Integer.parseInt();
       }
       
       return testOneScore;
    }
    
    public static Float getTestTwoScore()
    {
       float testTwoScore;
       
       Scanner keyboardInput = new Scanner(System.in);
       
       System.out.print("Enter Test Two's Score ");
       testTwoScore    = keyboardInput.nextFloat();
       
       while ( testTwoScore < 0 || testTwoScore > 100 )
       {
          JOptionPane.showMessageDialog(null,
          "The test score entered is not in an acceptable range.",
          "Invalid Entry", JOptionPane.WARNING_MESSAGE);
          
          testTwoScore = JOptionPane.showInputDialog(null,
          "Please re-enter a value from 0 to 100 only ", "Error",
          JOptionPane.ERROR_MESSAGE);
          testTwoScore   = Integer.parseInt();
       }
       
       return testTwoScore;
    }
    
    public static void chooseOption()
    {
    
    // Scanner object for keyboard input
      Scanner keyboardInput = new Scanner(System.in);
      
    // Prompt the computer user to select an option
      System.out.println("\nType an option to perform the operation\n" +
          "   Option 1:  Find the Lowest Test One Score\n" +
          "   Option 2:  Find the highest Test One Score\n" +
          "   Option 3:  Find the lowest Test Two Score\n" + 
          "   Option 4:  Find the highest Test Two Score\n" + 
          "   Option 5:  Calculate the Overall Class Average Test Score\n" + 
          "   Option 6: End the Program\n");
          
      optionSelected = keyboardInput.nextInt();
      keyboardInput.nextLine();   //Consume enter key
      
      while (optionSelected != 6)
      {
         switch(optionSelected)
         {
            // Find the lowest Test One Score
            case 1:
               lowestTestOneScore  = StudentAverage.findLowestTestOneScore
                           (testOneScore);
               System.out.printf("\nThe lowest test one score is %.2f%n", lowestTestOneScore);
               break;
               
            // Find the highest Test One Score
            case 2:
               highestTestOneScore  = StudentAverage.findHighestTestOneScore
                           (testOneScore);
               System.out.printf("\nThe highest test one score is %.2f%n", highestTestOneScore);
               break;
            
            // Find the lowest Test Two Score
            case 3:
               lowestTestTwoScore  = StudentAverage.findLowestTestTwoScore
                           (testTwoScore);
               System.out.printf("\nThe lowest test two score is %.2f%n", lowestTestTwoScore);
               break;
               
            // Find the highest Test Two Score
            case 4:
               highestTestTwoScore  = StudentAverage.findHighestTestTwoScore
                           (testTwoScore);
               System.out.printf("\nThe highest test two score is %.2f%n", highestTestTwoScore);
               break;
               
            // Calculate the average of test scores
            case 5:
               averageScore = StudentAverage.calculateAverage
                               (totalOverallTestScores,
                                numberOfStudents);
               System.out.printf("\nThe Overall Class Average of The Tests Are      " +
                                 (formatOutput.format(averageAnswer)));
               break;
                
            // End of the program run
            case 6:
               break;
               
           // Validation check
           default:
            System.out.println("Enter a number from 1 to 6 only ");
         } // End of the switch statement
         
         // Prompt the computer user to select an option
         System.out.println("\nType an option to perform the operation\n" +
             "   Option 1:  Find the Lowest Test One Score\n" +
             "   Option 2:  Find the highest Test One Score\n" +
             "   Option 3:  Find the lowest Test Two Score\n" + 
             "   Option 4:  Find the highest Test Two Score\n" + 
             "   Option 5:  Calculate the Overall Class Average Test Score\n" + 
             "   Option 6: End the Program\n");
             
         optionSelected = keyboardInput.nextInt();
         keyboardInput.nextLine();  //Consume enter key
         
      } // End of the while statement
         
   } // End of the mainmethod
   
} // End of the class

共有1个答案

卫景明
2023-03-14

这是无效的:

public static void()

那应该是你的主要方法吗?在这种情况下,您将需要:

public static void main(String[] args)

它称为方法签名。如果您需要编写其他方法,您必须问自己是否需要public,private,default,protected。然后你决定是否是静电。然后选择一个返回值或void,然后根据方法要做的事情来命名它。例如,将两个整数相加的方法是

public int add(int a, int b){}
 类似资料:
  • 问题内容: 在精确地探究Java标识符中允许使用哪些字符时,我偶然发现了一件非常奇怪的事情,以至于几乎可以肯定这是一个错误。 我预料地发现,Java标识符合他们开始与拥有的Unicode字符属性的要求,并其次是与物业,以授予领先的下划线和美元符号例外。事实并非如此,我发现与我所听说的那种普通标识符或其他任何想法都存在极大差异。 简短演示 请考虑以下演示,证明Java标识符中允许使用ASCII ES

  • 问题内容: 我正在检查一些功能,并在网站上浏览了一些看起来很有趣的代码: 作为匿名函数的例子之一。 有人知道吗?有文件吗?而且看起来很邪恶,应该使用它吗? 问题答案: 这就是PHP表示闭包的方式。这根本不是邪恶的,实际上它是强大而有用的。 基本上,这意味着您要允许匿名函数在其作用域之外“捕获”局部变量(在这种情况下为),并将其值(或在引用其自身的情况下)保留为内部状态匿名函数本身。

  • 问题内容: 我正在开发一个软件,需要删除用户提供的数字,但是不幸的是,某些列之类的元素(与用户输入无关)也可以用数字表示。因此,我正在寻找一个完整的参考资料,以了解何时在查询中可以用ID表示列名或(也许是表名)。 例如,在下面的查询中建立了这种情况: 问题答案: 对 输出 列的位置引用在语句的 and 子句中有效。 子句 表达式 可以是输入列名称,也可以是输出列的名称 或序号 (SELECT列表项

  • 要求代码的问题必须证明对正在解决的问题有最低限度的理解。包括尝试的解决方案、为什么它们不起作用以及预期的结果。另请参阅:堆栈溢出问题清单 我刚开始学习Java。 我的朋友正在帮我学习,他刚刚给我发了这个,说“把这个弄明白”。 不幸的是,我无法阅读这个。对我来说,它看起来像Perl。 这是什么意思?

  • 我试图使java编译类文件,但它抛出了一个错误,如何修复它。

  • 主要内容:标识符,关键字任何一种计算机语言都离不开标识符和关键字,因此下面将详细介绍 Java 的标识符、关键字和保留字。 标识符 Java 中标识符是为方法、变量或其他用户定义项所定义的名称。标识符可以有一个或多个字符。在 Java 语言中,标识符的构成规则如下。 标识符由数字(0~9)和字母(A~Z 和 a~z)、美元符号($)、下划线(_)以及 Unicode 字符集中符号大于 0xC0 的所有符号组合构成(各符号