【问题标题】:Arrays with for loop and if statements带有 for 循环和 if 语句的数组
【发布时间】:2023-03-30 01:50:01
【问题描述】:

所以当我尝试保存和编译时,一切正常,直到我运行它。我的数组语法似乎有问题。有人可以帮我找到它吗?当我运行这个程序时,grades() 方法会输出 "AAA" 。我在这个程序中尝试做的是从 txt 文件中读取文本并列出每一行,输出学生姓名和分数。现在在 Grades() 方法中,我试图输出计算每个学生成绩的字母成绩,并使其进入循环,直到读取最后一个分数。

public class ReadData 
  {
private static String[] names = new String[3];
private static int line;
private static int[] scores = new int[3];
private static float mean;
private static double stdDeviation;

public static void readData() throws FileNotFoundException 
{
    File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    int l = 0;

    // float sum = 0 ;
    while (scanner.hasNextLine()) { 
        String line = scanner.nextLine();
        String [] words = line.split("\t"); 
        names[l] = words[0]; 
        scores[l] = Integer.parseInt(words[1]);
        // sum+=scores[l];
        System.out.println(" name: " + names[l] + ", score: " + scores[l]);
        l++; 


    }
   //   System.out.println(scores[0]+ " " + scores[1]+ " " + scores[2]);

}

public static void fndMean() 
{

    float mean = ((25+65+89)/3);
    System.out.println(" The mean is: " + mean);
}


public static void fndStandard() throws FileNotFoundException
{
    double stdDeviation = Math.sqrt(((Math.pow(25-59, 2)+(Math.pow(65-59,2))+
            (Math.pow(89-59, 2))))/3);
    System.out.println("The Standard Deviation is: " + stdDeviation);
}

评分法

public static void grades()
    {
        for(int i = 0; i < (scores.length); i++)
        {
            if(mean + stdDeviation <= scores[i])
            {
                System.out.print("A");
            }
            else if( (scores[i] >= mean+(stdDeviation/3)) &&  
           (mean     +stdDeviation)> scores[i])
            {
                System.out.print("B");
            }
            else if( (scores[i] >= mean-(stdDeviation/3)) && 
            (mean +(stdDeviation/3))> scores[i])
            {
                System.out.print("C");
            }
            else if( (scores[i] >= mean-(stdDeviation)) &&  
            (mean -            (stdDeviation/3))> scores[i])
            {
                System.out.print("D");
            }   
            else
            {
                System.out.println("F");
            }
        }
}

【问题讨论】:

  • 对于未来读者对grades() 的位置感到困惑,请向下滚动。
  • 单步执行您的代码,看起来您的问题是您一遍又一遍地为数组中的第二项分配相同的值...
  • 还有:if(mean + stdDeviation &lt;= scores[i])。不需要在else中重复相反的条件...已经是else了!
  • 欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果之后仍有问题,请随时回来提供更多详细信息。

标签: java arrays if-statement for-loop


【解决方案1】:

当您执行以下操作时,您将在 fndMean() 和 fndStandard() 等方法中重新声明变量

double stdDeviation = Math.sqrt(((Math.pow(25-59, 2)+(Math.pow(65-59,2))+
            (Math.pow(89-59, 2))))/3);
float mean = ((25+65+89)/3);

你已经在上面声明了它们,不需要再做一次,否则它只会在方法内部设置局部变量,而不是在你的类内部。你应该这样做

stdDeviation = Math.sqrt(((Math.pow(25-59, 2)+(Math.pow(65-59,2))+
            (Math.pow(89-59, 2))))/3);
mean = ((25+65+89)/3);

当您调用这些方法计算成绩时,这会将这些变量设置为您所期望的。

【讨论】:

    【解决方案2】:

    这是fndMeanfndStandard 方法打印的内容:

     The mean is: 59.0
    The Standard Deviation is: 26.407069760451147
    

    meanstdDeviation 之和为 85.40706976045115。

    现在,条件if(mean + stdDeviation &lt;= scores[i]) 检查该总和是否小于等于score[i],如果是,则打印“A”。在以下两种情况中的任何一种情况下都是正确的:

    • txt文件中第二列(tab)的值都大于85
    • score 数组在两个方法调用之间发生变化

    在这些条件之前打印分数值应该会给您更多的想法。

    【讨论】:

    • 我认为变量及其值在一种方法中是局部的,不能在另一种方法中使用?谢谢你的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多