尝试乘以整数数组时解决错误

时间:2015-11-11 02:21:36

标签: java arrays eclipse

问题发生在第一个if语句中,我收到错误读取“类型不匹配:无法从double转换为String”,我真的不知道这里发生了什么任何帮助将不胜感激。 import java.util.Scanner;

public class MarkmCalculator {


public static double[] modWeights = { 50, 40, 100, 50, 20, 35 };
public static String[] moduleResults = new String[6];
public static int[] moduleMarks = new int [6];
public static int[] courseworkMark = new int [6];
public static int[] examMark = new int [6];




public static void computemarks(){



    double modMark = 0.0;


            for (int i = 0; i < 6; i++) {

                if ((examMark[i] > 35) && (courseworkMark [i] > 35))
                {
                    moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)
                }      

                    else {
                        moduleResults[i] = (((courseworkMark[i] * modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]))) / 100)
                    }
                            if { (moduleMarks < 35) 
                                moduleMarks = modMark;

                            else {
                                modMark = moduleMarks;
                            }

                        if (moduleMark > 40){
                            moduleMark = "Pass";
                        }   
                            else if ((moduleMark) <= 35 && (moduleMark) < 40){
                                moduleMark = "Compensatable Pass";
                            }

                            else{
                                moduleMark = "Fail"
                        }



                    }

                }



    public static void computeResult(){

        for (int i = 0; i < 6; i++)

            if 
    }









public static void main(String[] args) {

    computemarks(); 

}

}

3 个答案:

答案 0 :(得分:2)

错误说明了一切。你不能隐含地将double转换为字符串。在这一行:

except: ['register', 'login', 'about', 'portfolio']

您想要将计算结果存储在字符串中。您需要转换为字符串。类似的东西:

moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100);

或者,只需将结果存储为双精度,将moduleResults[i] = ((courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)) + ""; 更改为moduleResults

答案 1 :(得分:1)

moduleResults [i]是一个字符串。 Java无法将字符串隐式转换为double。有关如何将double转换为字符串,请参阅Converting double to string

答案 2 :(得分:1)

许多地方都会出现问题,我会一步一步地尝试:

public static double[] modWeights = { 50, 40, 100, 50, 20, 35 };
public static String[] moduleResults = new String[6];
public static int[] moduleMarks = new int [6];
public static int[] courseworkMark = new int [6];
public static int[] examMark = new int [6];

除非必须将int数组改为double,然后改为:

for (int i = 0; i < 6; i++) {

     if ((examMark[i] > 35) && (courseworkMark [i] > 35))
            {
       moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)
            }      
       else {
     moduleResults[i] = (((courseworkMark[i] * modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]))) / 100)
                }

Go for:

for (int i = 0; i < 6; i++) {

                    if ((examMark[i] > 35) && (courseworkMark [i] > 35))
                    {
                        moduleResults[i] = Double.toString((courseworkMark[i]) * (modWeights[i]) + (examMark[i] * (100 - modWeights[i]) / 100));
                    }      

                        else {
                            moduleResults[i] = Double.toString((((courseworkMark[i] * modWeights[i]) + (examMark[i] * (100 - modWeights[i]))) / 100));
                        }

我不知道在任务中要做什么但是 - moduleResults[i] = (courseworkMark[i]) * (modWeights[i]) + (examMark[6 + i] * (100 - modWeights[i]) / 100)会因ArrayIndexOutOfBounds

而给你examMark[6 + i]例外

此外:

if { (moduleMarks < 35) 
                            moduleMarks = modMark;

                        else {
                            modMark = moduleMarks;
                        }

ModuleMarks是一个数组,因此您无法在if语句中对此进行比较,特别是{之后的if,可能是这样吗?:

if (moduleMarks[i] < 35) 
                                    moduleMarks[i] = modMark;

                                else {
                                    modMark = moduleMarks[i];
                                }

此外,您正尝试将String放入Double数组:

 if (moduleMark > 40){
                        moduleMark = "Pass";
                    }   
                        else if ((moduleMark) <= 35 && (moduleMark) < 40){
                            moduleMark = "Compensatable Pass";
                        }

                        else{
                            moduleMark = "Fail"
                    }

我认为,必须进入String[] moduleResults

 if (moduleMarks[i] > 40){
                                moduleResults[i] = "Pass";
                            }   
                                else if ((moduleMarks[i]) <= 35 && (moduleMarks[i]) < 40){
                                    moduleResults[i]= "Compensatable Pass";
                                }

                                else{
                                    moduleResults[i] = "Fail";
                            }

我希望它有所帮助。

编辑这将至少修复语法错误 - 后勤内容取决于您。