Java代码中的错误

时间:2011-03-16 16:48:35

标签: java compiler-errors

我正在尝试获取用户输入的最高和最低数字。我懂了。我只是编程新手。有3个错误。

import java.io.*;
public class HighestToLowest
{
    public static void main(String []args)throws IOException{
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));        

        double[] input = new double[8];
        int index;
        int highIndex = 0;
        int lowIndex = 0;
        double sum = 0;

        System.out.println("Enter The Scores Of Judges: ");
        for (index = 0; index<8; index++){
             System.out.print("Enter The Score" + (index + 1) + ": ");
             input[index] = Double.parseDouble(dataIn.readLine()); 
         }

         for (index = 1; index < 8; index++)
             if(input[highIndex] < input[index])
                highIndex = index;


         for (index = 1; index < 8; index++)
             if (input[lowIndex] > input[index])
                 lowIndex = index;

         for (index = 0; index < 8; index++)
             sum = sum + input[index];
         try
            {
                input[index] = Double.parseDouble(dataIn.readLine());
            }
            catch(IOException e)
            {
                System.out.println("error");
            }

            if(sum>index)
            {
                sum=highIndex;
            }

            if(sum>=index)
            {
                index=lowIndex;
            }                        
        }

        System.out.print("Highest is: + highIndex");
        System.out.print("Lowest is: + lowIndex");

         System.out.printf("The Contestant Receives a total of %.2f", (sum - highIndex - lowIndex)); 



    }
}

1 个答案:

答案 0 :(得分:0)

虽然你说只有3个错误,但可以看到更多

public static void main(String []args)throws IOException{ //<- ?
{//<- why two curly brackets?

for for循环

for (index = 0; index < 8; index++){//<- curl bracket missing?
         sum = sum + input[index];
         try {
              input[index] = Double.parseDouble(dataIn.readLine());
          } catch(IOException e) {
              e.printStackTrace();
         }

         if(sum>index) {
              sum=highIndex;
          }
          if(sum>=index){
              index=lowIndex;
           }                        
     } // <- or extra curl bracket?

此行将打印Highest is: + highIndex

  System.out.print("Highest is: + highIndex");

" "中的任何内容都按原样打印。

所以将其改为

 System.out.print("Highest is:" + highIndex);

同样适用于

  System.out.print("Lowest is: + lowIndex");

你是来自C编程,这一行是正确的

System.out.printf("The Contestant Receives a total of %.2f", (sum - highIndex - lowIndex)); 

在Java中,它也可以写成

 System.out.println("The Contestant Receives a total of " +  (sum - highIndex - lowIndex));